[SOLVED] loading records into array and viewing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lynx2003
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2004 12:57 pm

[SOLVED] loading records into array and viewing

Post by lynx2003 »

I do a simple query, and would like to load the records into an array, if it meets a conditon. The condition is not in the query.
I have tried loading the records and viewing the array, but it does not seem to work. Am i loading the array properly, or should i be using an array_push. I could not get that working either. Any help would be appreciated. Thanks.

Code: Select all

$result= mysql_query("Select id, first_name, last_name, city From students");

$ids= array(); 
while($row = mysql_fetch_array($result)) 
{ 
   //an if condition, if it meets condition, we will store record into array
  { 
      $ids[] = $row[id]; 
      $student[id] = $row[id]; 
      $student[id][first_name] = $row[first_name]; 
      $student[id][last_name] = $row[last_name]; 
      $student[id][city] = $row[city]; 
  } 
} 


for($i=0;$i<count($ids);$i++){ 
$id=$ids[$i]; 
  echo "Student ID:".$student[$id]."\n"; 
  echo "First Name:".$student[$id][first_name]."\n"; 
  echo "Last Name:".$student[$id][last_name]."\n"; 
  echo "City:".        $student[$id][city]."\n"; 
}
[Edit: Added PHP tags for eyecandy. --JAM]
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

Instead of mysql_fetch_array, you should be using mysql_fetch_assoc. Then your code should work. mysql_fetch_array returns an array with numbers as indexs. So insted of $row['id'], it would be $row[0]. mysql_fetch_assoc returns arrays the way you are using them.

Also, insted of the for statement to loop through the array, you could do a foreach instead (i just love foreach :))

Code: Select all

foreach( $student as $key => $value ) {
    echo "Student Id: " . $value['id'];
    echo "First name: " . $value['first_name'];
    echo "Last Name: " . $value['last_name'];
    echo "City: " . $value['city'];
}
This way you don't have to keep an extra array of ids.

and you should really be using quotes in your index's, like in my post.
lynx2003
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2004 12:57 pm

Post by lynx2003 »

Sevengraff,
it actually only displays the last reocrd found. Is it overwriting the $student array each time?
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

I think the problem is that

Code: Select all

$ids[] = $row[id];
$student[id] = $row[id];
$student[id][first_name] = $row[first_name];
$student[id][last_name] = $row[last_name];
$student[id][city] = $row[city];
should be

Code: Select all

$student[$row['id']]['first_name'] = $row['first_name'];
$student[$row['id']]['last_name'] = $row['last_name'];
$student[$row['id']]['city'] = $row['city'];
The index is not changing, and so only one record is in $student.

Also, it seems like your code could be shortend by doing this:

Code: Select all

$result= mysql_query("Select id, first_name, last_name, city From students");

$student = array();
while($row = mysql_fetch_assoc($result))
{
	if( /* condition */ )	
	{
		$student[$row['id']] = $row;	// store data for possible later user
		
		// print student info for those who met condition
		echo "Student ID: " . $row['id'];
		echo "First Name: " . $row['first_name'];
		echo "Last Name: " . $row['last_name'];
		echo "City: " . $row['city'];
	} // end if
} // end while
Rater than build the array and loop through it again, we can print the info when we make the array. And since it seemed that you were just copying all the elements of the $row array into $student, it would be easier to just copy the whole array into $student.
lynx2003
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2004 12:57 pm

Post by lynx2003 »

it works, thank you very much Sevengraff!!
Post Reply