473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

foreach headaches

TheServant
1,168 Expert 1GB
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?
Jan 31 '09 #1
5 1273
Dormilich
8,658 Expert Mod 8TB
$raw_user_rankings is a resource, you probably meant
Expand|Select|Wrap|Line Numbers
  1. foreach ($user_rankings as $rank)
Jan 31 '09 #2
TheServant
1,168 Expert 1GB
@Dormilich

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...
Jan 31 '09 #3
TheServant
1,168 Expert 1GB
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.
Jan 31 '09 #4
Dormilich
8,658 Expert Mod 8TB
@TheServant
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).
Jan 31 '09 #5
TheServant
1,168 Expert 1GB
That makes sense. Thanks for clearing that up for me.
Feb 1 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
3
by: Wiktor Zychla [C# MVP] | last post by:
since generics allow us to implement several IEnumerable<T> interfaces on a single class, I think that "foreach" should somehow reflect that. suppose we have a enumerable class class C :...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.