The Little Guy Posted May 16, 2007 Share Posted May 16, 2007 For some reason the last while loop isn't working <?php while($row2 = mysql_fetch_array($sql)){ $file_amount += $row2['size']; } $file_space = round($file_amount/1024,2); if($file_space <= 1024){ echo "<b>$file_space"."KB</b>"; }elseif($file_space >= 1024 && $file_space < 1073741824){ $file_space = round($file_space/1024,2); echo "<b>$file_space"."MB</b>"; }elseif($file_space >= 1073741824){ $file_space = round($file_space/1073741824,2); echo "<b>$file_space"."GB</b>"; } while($row = mysql_fetch_array($sql)){ echo '<p>'.$row['file_name'].'</p>'; } ?> but... if i remove everything except for the last while loop, then it works. Anyone know why? There are no error messages at any point and time. Quote Link to comment https://forums.phpfreaks.com/topic/51679-while-not-working-correctly/ Share on other sites More sharing options...
trq Posted May 16, 2007 Share Posted May 16, 2007 Because you have already looped through all the results in your first loop. You will need to use mysql_data_seek to move the pointer back to the start. Quote Link to comment https://forums.phpfreaks.com/topic/51679-while-not-working-correctly/#findComment-254575 Share on other sites More sharing options...
ph2007 Posted May 16, 2007 Share Posted May 16, 2007 It doesn't look as though the contents of your second while loop require any of the previous code to already be completed. Why don't you put the echo string in the first while loop? Quote Link to comment https://forums.phpfreaks.com/topic/51679-while-not-working-correctly/#findComment-254584 Share on other sites More sharing options...
The Little Guy Posted May 16, 2007 Author Share Posted May 16, 2007 It doesn't look as though the contents of your second while loop require any of the previous code to already be completed. Why don't you put the echo string in the first while loop? Then the content will echo out incorrectly. Quote Link to comment https://forums.phpfreaks.com/topic/51679-while-not-working-correctly/#findComment-254592 Share on other sites More sharing options...
The Little Guy Posted May 16, 2007 Author Share Posted May 16, 2007 Because you have already looped through all the results in your first loop. You will need to use mysql_data_seek to move the pointer back to the start. But the two row variables are different, it shouldn't matter. Shouldn't it? Quote Link to comment https://forums.phpfreaks.com/topic/51679-while-not-working-correctly/#findComment-254597 Share on other sites More sharing options...
lee20 Posted May 16, 2007 Share Posted May 16, 2007 Try building an array while looping the results, and then loop the array in your second loop like so: <?php $rows = array(); while($row2 = mysql_fetch_array($sql)){ $rows[] = $row2; $file_amount += $row2['size']; } $file_space = round($file_amount/1024,2); if($file_space <= 1024){ echo "<b>$file_space"."KB</b>"; }elseif($file_space >= 1024 && $file_space < 1073741824){ $file_space = round($file_space/1024,2); echo "<b>$file_space"."MB</b>"; }elseif($file_space >= 1073741824){ $file_space = round($file_space/1073741824,2); echo "<b>$file_space"."GB</b>"; } foreach ($rows AS $row) { echo '<p>'.$row['file_name'].'</p>'; } ?> Note: You may want to use a for loop instead of a foreach loop because if there are no results you will get an error in this case. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/51679-while-not-working-correctly/#findComment-254600 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.