foreach headaches

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    foreach headaches

    Hey guys,
    I am trying to generate a table automatically, but I am having headaches using foreach loops:

    Code:
        $raw_user_rankings = mysql_query("SELECT user_id, user_name, user_clan_id, user_pop, user_race
                                        FROM users
                                        LIMIT 120, 30
                                        ");
        $user_rankings = mysql_fetch_array($raw_user_rankings);
    
        foreach ( $raw_user_rankings as $rank ) {
            echo("    <tr>
                        <td>" . ($i/5) . "</td>
                        <td><a href=\"profile.php?id=" . $rank['user_id'] . "\">" . $rank['user_name'] . "</a></td>
                        <td>" . $rank['user_clan_id'] . "</td>
                        <td>" . $rank['user_pop'] . "</td>
                        <td>" . ucwords($rank['user_race']) . "</td>
                    </tr>
                ");
        }
    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:
    Code:
    Invalid argument supplied for foreach()
    Can anyone explain this to me?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    $raw_user_ranki ngs is a resource, you probably meant
    Code:
    foreach ($user_rankings as $rank)
    Last edited by Dormilich; Jan 31 '09, 10:34 AM. Reason: completely new

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Originally posted by Dormilich
      $raw_user_ranki ngs is a resource, you probably meant
      Code:
      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:
      Code:
      Rank         Name         Clan         Population         Race                               
      0                     [URL="http://supremewarfare.com/alpha/profile.php?id=0"]0[/URL]                     0                     0                     1                                            
      0 [URL="http://supremewarfare.com/alpha/profile.php?id=0"]0[/URL]                     0                     0                     1                                                   
      0                     [URL="http://supremewarfare.com/alpha/profile.php?id=u"]s[/URL]                     e                     r _
      Which is really weird?

      Also, printing the arrays I get for example:
      Code:
      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:
      Code:
      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...

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        OK I tried:
        Code:
        foreach ( $user_rankings as $rank ) {
        print_r($rank);
        }
        And got:
        Code:
        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.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by TheServant
          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_ass oc() 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).

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            That makes sense. Thanks for clearing that up for me.

            Comment

            Working...