Jump to content

While Not Working Correctly


The Little Guy

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/51679-while-not-working-correctly/
Share on other sites

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

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.