Jump to content

[SOLVED] help with difference between while and for loops with use of mysql_fetch_array


Darkmatter5
 Share

Recommended Posts

Here's my code:

 

$query="SELECT CONCAT_WS(' ', dpc.forename, dpc.surname) AS name, dc.class, dpc.class_level AS level
        FROM d_player_characters AS dpc
        INNER JOIN d_classes AS dc ON dc.class_id=dpc.class_id
        WHERE dpc.owner_id='$id'
        ORDER BY dpc.class_id ASC, surname ASC";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) { echo "<th width='25%'>$row[name]</th>"; }
/*$row=mysql_fetch_array($result);
for($i=0;$i<=3;$i++) {
  if(!is_null($row[$i])) { echo "<th width='25%' height='60'>$row[name]<br>a $row[level] level<br>$row[class] character</th>"; }
  else { echo "<th width='25%' height='60'><span class='large_text'><a href='$parent_url/create_pc.php'>Create a new Player Character</a></span></th>"; }
}*/

 

The above code produces the list of names, but the below code only lists the first array element, but does in fact list it 4 times, so how can I use the for loop to get all the data like the while loop does?

 

$query="SELECT CONCAT_WS(' ', dpc.forename, dpc.surname) AS name, dc.class, dpc.class_level AS level
        FROM d_player_characters AS dpc
        INNER JOIN d_classes AS dc ON dc.class_id=dpc.class_id
        WHERE dpc.owner_id='$id'
        ORDER BY dpc.class_id ASC, surname ASC";
$result=mysql_query($query) or die(mysql_error());
//while($row=mysql_fetch_array($result)) { echo "<th width='25%'>$row[name]</th>"; }
$row=mysql_fetch_array($result);
for($i=0;$i<=3;$i++) {
  if(!is_null($row[$i])) { echo "<th width='25%' height='60'>$row[name]<br>a $row[level] level<br>$row[class] character</th>"; }
  else { echo "<th width='25%' height='60'><span class='large_text'><a href='$parent_url/create_pc.php'>Create a new Player Character</a></span></th>"; }
}

 

So the while loop will produce this:

<th width='25%'>John Smith</th>

<th width='25%'>Joe Blow</th>

<th width='25%'>Jane Smith</th>

 

The for loop produces this:

<th width='25%' height='60'>John Smith<br>a 1 level<br>Strong character</th>

<th width='25%' height='60'>John Smith<br>a 1 level<br>Strong character</th>

<th width='25%' height='60'>John Smith<br>a 1 level<br>Strong character</th>

<th width='25%' height='60'><span class='large_text'><a href='$parent_url/create_pc.php'>Create a new Player Character</a></span></th>

 

Why doesn't the for loop produce this:

<th width='25%' height='60'>John Smith<br>a 1 level<br>Strong character</th>

<th width='25%' height='60'>Joe Blow<br>a 1 level<br>Strong character</th>

<th width='25%' height='60'>Jane Smith<br>a 1 level<br>Strong character</th>

<th width='25%' height='60'><span class='large_text'><a href='$parent_url/create_pc.php'>Create a new Player Character</a></span></th>

Link to comment
Share on other sites

while($row=mysql_fetch_array($result))

This action fetches the row every time it loops because it evaluates to true. Your for loop is only setting it once. To fix that, you can put the $row definition inside the loop:

for($i=0;$i<=3;$i++) {
$row=mysql_fetch_array($result);
[...]

 

The while loop method is more efficient and I find to be more elegant as well.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.