Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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