Jump to content

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


Darkmatter5

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>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.