Connecting Tech Pros Worldwide Forums | Help | Site Map

foreach headaches

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#1: Jan 31 '09
Hey guys,
I am trying to generate a table automatically, but I am having headaches using foreach loops:

Expand|Select|Wrap|Line Numbers
  1.     $raw_user_rankings = mysql_query("SELECT user_id, user_name, user_clan_id, user_pop, user_race
  2.                                     FROM users
  3.                                     LIMIT 120, 30
  4.                                     ");
  5.     $user_rankings = mysql_fetch_array($raw_user_rankings);
  6.  
  7.     foreach ( $raw_user_rankings as $rank ) {
  8.         echo("    <tr>
  9.                     <td>" . ($i/5) . "</td>
  10.                     <td><a href=\"profile.php?id=" . $rank['user_id'] . "\">" . $rank['user_name'] . "</a></td>
  11.                     <td>" . $rank['user_clan_id'] . "</td>
  12.                     <td>" . $rank['user_pop'] . "</td>
  13.                     <td>" . ucwords($rank['user_race']) . "</td>
  14.                 </tr>
  15.             ");
  16.     }
  17.  
This is probably one of the first times I've actually used a foreach loop in php and I can't figure out why I keep getting errors including:
Expand|Select|Wrap|Line Numbers
  1. Invalid argument supplied for foreach()
Can anyone explain this to me?

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#2: Jan 31 '09

re: foreach headaches


$raw_user_rankings is a resource, you probably meant
Expand|Select|Wrap|Line Numbers
  1. foreach ($user_rankings as $rank)
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#3: Jan 31 '09

re: foreach headaches


Quote:

Originally Posted by Dormilich View Post

$raw_user_rankings is a resource, you probably meant

Expand|Select|Wrap|Line Numbers
  1. foreach ($user_rankings as $rank)


Sorry, I forgot to mention it, but going on that path I do get a result. It seems to be printing a table like:
Expand|Select|Wrap|Line Numbers
  1. Rank         Name         Clan         Population         Race                               
  2. 0                     0                     0                     0                     1                                            
  3. 0                     0                     0                     1                                                   
  4. 0                     s                     e                     r _
Which is really weird?

Also, printing the arrays I get for example:
Expand|Select|Wrap|Line Numbers
  1. Array (     [0] => 00001     [user_id] => 00001     [1] => user_1     [user_name] => user_1     [2] =>      [user_clan_id] =>      [3] => 0     [user_pop] => 0     [4] => human     [user_race] => human )
Which is all correct, but when I use:
Expand|Select|Wrap|Line Numbers
  1. echo($rank[0]);
I get "0"?

I think it's also not collecting the next row, so it is just repeating the first called one, which is why I tried to use the resource...
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#4: Jan 31 '09

re: foreach headaches


OK I tried:
Expand|Select|Wrap|Line Numbers
  1. foreach ( $user_rankings as $rank ) {
  2. print_r($rank);
  3. }
And got:
Expand|Select|Wrap|Line Numbers
  1. 0000100001user_1user_100humanhuman
Which suggests that the table I printed before is from that line and it was simply taking the character at position x where I had $rank[x]...

So what am I doing wrong?

[edit]***
Just started using a while loop instead which is the way I am used to, but I just thought a foreach loop would work. Anyway, problem solved by using another method. Thanks for your help.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#5: Jan 31 '09

re: foreach headaches


Quote:

Originally Posted by TheServant View Post

I think it's also not collecting the next row, so it is just repeating the first called one, which is why I tried to use the resource...

mysql_fetch_assoc() only fetches one row at a time, that's why usually a while loop is used to get all the results.

if you want to fetch all results at once, have a look at PHP's database extensions (PDO, MySQLi, MDB2).
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#6: Feb 1 '09

re: foreach headaches


That makes sense. Thanks for clearing that up for me.
Reply