Jump to content

An array question


bnther

Recommended Posts

I have a database with 1 table and 2 records. I want to fill the array with those two records -- and it is -- but when I loop through the array, instead of outputting 1 complete record at a time it's outputting everything twice.

 

For example, if I had 2 business names of Apple Computers and Windows, then I would output Apple ComputersWindows (because they both have the same variable) on the same line and it would do it twice because there are 2 records. What I need is for Apple Computer and all of its info to be displayed by itself and then loop through to output Windows and the relative info.

 

Here's my array code:

 

// set up for accessing the database 'select'

 

$query = "SELECT * FROM members";

$result = mysql_query($query);

$my_business_title = "";

$my_contact_name = "";

$my_street_address = "";

$my_city_address = "";

$my_phone = "";

$my_fax = "";

$my_email = "";

$my_website = "";

$my_comments = "";

// record type -- accounting -> retail

$my_type = "";

 

//loop stuff

$num = mysql_numrows($result);

$i=0;

 

//fill the array from the database 'members'

while($row = mysql_fetch_array($result, MYSQL_ASSOC))// I think this is where I'm screwing up

{

$my_business_title = $my_business_title . "{$row['b_title']}";

$my_contact_name = $my_contact_name . "{$row['c_name']}";

$my_street_address = $my_street_address . "{$row['st_address']}";

$my_city_address = $my_city_address . "{$row['city_address']}";

$my_phone = $my_phone . "{$row['phone']}";

$my_fax = $my_fax . "{$row['fax']}";

$my_email = $my_email . "{$row['email']}";

$my_website = $my_website . "{$row['website']}";

$my_comments = $my_comments . "{$row['comments']}";

// record type

$my_type = $my_type . "{$row['type']}";

 

}

Link to comment
Share on other sites

I think what's happening here, is every entry in the database under a certain field name 'c_name' (contact name) for example, is being displayed simultaneously. What I need is to have the field data for its corresponding row to be displayed and nothing else.

 

Any thoughts?

Edited by bnther
Link to comment
Share on other sites

As far as I can tell, the code you've shown is working correctly. However, it seems like you have a logical flaw -- specifically, you're trying to use the same variables to hold multiple sets of data. Your post title says "An array question" but I don't actually see you using any arrays.

 

Right now, this is the way things work in your loop... You start the while loop, and at the end of the first loop, all the variables hold the data from the first result in the database. So, for example, the $my_business_title variable holds the title of the first business. So far, so good. However, since there are more records in the database, it loops through again, adding the second business's data to the existing data being held by your variables. Now, the $my_business_title holds both the first business title, and the second business's title ("Business1Business2"). As long as the while loop continues, the variables within the loop will hold data for all the businesses, rather than holding information for each business one at a time.

 

Exactly how you change this depends on what you are intending to do with this information. Are you simply trying to display it? Is this information being stored and used elsewhere throughout the application?

 

One way to approach this would be to use a 2D associative array.

 

$total = 0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
   $data[$total]['b_title'] = $row['b_title'];
   $data[$total]['c_name'] = $row['c_name'];
   $data[$total]['st_address'] = $row['st_address'];
   $data[$total]['city_address'] = $row['city_address'];
   $data[$total]['phone'] = $row['phone'];
   $data[$total]['fax'] = $row['fax'];
   $data[$total]['email'] = $row['email'];
   $data[$total]['website'] = $row['website'];
   $data[$total]['comments'] = $row['comments'];
   $data[$total]['type'] = $row['type'];
   $total++;
}

 

Basically, this will create a giant $data array that holds all the contents, and you can easily loop through it to display this information. At the end of the loop, the $total variable would hold the total number of clients (in your case, it would be "2"), and you could easily access each bit of information. For example,

 

To get client #1's title, you could use:

echo $data[0]['b_title'];

 

To get client #2's type, you could use:

echo $data[1]['type'];

 

Keep in mind that loops usually start at 0, rather than 1, so that's why I use $data[0] for client #1, rather than $data[1].

 

Since this is just a large array, we could easily loop through it to display all of the information (you'd obviously need to add line breaks, styling, etc...

 

for($i = 0; $i {
   echo $data[$i]['b_title'];
   echo $data[$i]['c_name'];
   echo $data[$i]['st_address'];
   echo $data[$i]['city_address'];
   echo $data[$i]['phone'];
   echo $data[$i]['fax'];
   echo $data[$i]['email'];
   echo $data[$i]['website'];
   echo $data[$i]['comments'];
   echo $data[$i]['type'];
}

 

More info:

http://www.tizag.com/phpT/forloop.php

http://us.php.net/manual/en/function.count.php

http://www.tizag.com/phpT/arrays.php

Link to comment
Share on other sites

Thanks for the reply.

 

You're right, I am storing ALL of the field data under 1 variable - hence the problem.

 

With regards to what I'm using the data for, it will only be for show. I will at some point (hopefully) be able to select 'type' (attorneys, manufacturing, Realtor, ect) from a drop down and sort through the database that way. I have a specific field for that.

 

Thanks for the links. Ironically, I had been to all 3, but I could cipher things out until you replied.

 

This has been a big help.

Thanks again for the reply

Link to comment
Share on other sites

I have a couple of questions on that code (which works perfectly by the way ;) )

 

$data - I didn't declare this anywhere and yet nothing error'd out. Why?

 

'b_title' is the name of the field and you used it directly. I'm used to naming variables for everything (a practice I've always that was extremely redundant). I think it would be great if I use it directly as opposed to creating 'handles' for everything. Just call it what it is, you know?!

 

At any rate, this was very educational.

Thank you

Link to comment
Share on other sites

To answer the question why the data array was not declared prior to the loop:

There?s no need to declare in advance how big an array is or take any special action to increase its size once you start using it. And PHP supports both numerically-indexed arrays and associative arrays ? that is, arrays that have strings for keys.

 

Thus you can do this with arrays and not with variables.

Link to comment
Share on other sites

$data - I didn't declare this anywhere and yet nothing error'd out. Why?

PHP doesn't always require you to instantiate your variables before you assign them values. There are a couple exceptions to this... For example, if you have global variables that you want accessible to all functions in a class file (http://www.php.net/manual/en/language.variables.scope.php) or if you do something like this:

 

$data = $data . "test";

 

Basically, this line reads: $data equals the current value of $data, with the text "test" added onto the end. If you don't assign $data a value before this line, it will generate a "Undefined variable" error since you are trying to access the value of the variable before it's been instantiated.

 

'b_title' is the name of the field and you used it directly. I'm used to naming variables for everything (a practice I've always that was extremely redundant). I think it would be great if I use it directly as opposed to creating 'handles' for everything. Just call it what it is, you know?!

I'm not sure if this was a question or just a comment... I do suggest keeping your code as simple and straightforward as possible. The shorter the code is and the less unnecessary variables you have, the more readable it tends to be. For example, if this code you are working with is simply intended to query the database and then display the results, perhaps placing everything in an array is completely unnecessary.

 

You could do something like this (obviously with line breaks and additional styling added):

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

echo $row['b_title'];

echo $row['c_name'];

echo $row['st_address'];

echo $row['city_address'];

echo $row['phone'];

echo $row['fax'];

echo $row['email'];

echo $row['website'];

echo $row['comments'];

echo $row['type'];

echo "
"; // this just separates one business from the next by adding a line break

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...