Darkmatter5 Posted August 28, 2009 Share Posted August 28, 2009 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 https://forums.phpfreaks.com/topic/172322-solved-help-with-difference-between-while-and-for-loops-with-use-of-mysql_fetch_array/ Share on other sites More sharing options...
lemmin Posted August 28, 2009 Share Posted August 28, 2009 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 https://forums.phpfreaks.com/topic/172322-solved-help-with-difference-between-while-and-for-loops-with-use-of-mysql_fetch_array/#findComment-908585 Share on other sites More sharing options...
Darkmatter5 Posted August 28, 2009 Author Share Posted August 28, 2009 Thanks! Link to comment https://forums.phpfreaks.com/topic/172322-solved-help-with-difference-between-while-and-for-loops-with-use-of-mysql_fetch_array/#findComment-908591 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.