Dan Zucker
Dan Zucker

Reputation: 33

Do while loop first iteration not working?

I have a nested do while loop. The first loop in the options inputs comes in blank with no options in the drop down menu. All other loops after the first one work perfectly. I am at a loss as to why the first one comes in blank and any help would be appreciated.

<div class="col-md-12">
<h1 align="center"><a href="images.php">&lt;Back</a> - Current Photos  in <?
echo $row_titles['title']; ?></h1>
<?php if ($totalRows_photos > 0)
{
    do
    { ?>
  <div class="col-md-3" style="height:350px;overflow:hidden;"><img 
src="../contentimages/<?php echo $row_photos['filename']; ?>" class="img- 
responsive" style="max-height:225px;" /><br />
<? if ($row_photos['catid'] == 1)
        {
            echo "Commercial";
        }
        else
        {
            echo "Residential";
        } ?> 
  |  <a href="images.php?id=<?php echo $row_photos['id']; ? > & amp;
        action = remove">Delete</a><br />
<form name="moveit" action=" < ? phpecho $editFormAction; ?>" method="POST" 
id="moveit"><select name="folder">
  <?php
        do
        {
?>
  <option value="<?php echo $row_folders['id'] ?>"<?php if (!(strcmp($row_folders['id'], $row_photos['project_id'])))
            {
                echo "selected=\"selected\"";
            } ?>><?php echo $row_folders['title'] ?></option>
  <?php
        }
        while ($row_folders = mysql_fetch_assoc($folders));
        $rows = mysql_num_rows($folders);
        mysql_data_seek($folders, 0);
        $row_folders = mysql_fetch_assoc($folders);
?>
</select><input name="Move" type="submit" value="Move" />
<input name="hiddenField" type="hidden" id="hiddenField" value="<?php echo $row_photos['id']; ?>" />
<input type="hidden" name="MM_update" value="moveit" />
</form></div>
  <?php
    }
    while ($row_photos = mysql_fetch_assoc($photos));
}
else
{
    echo "<h3 
align=\"center\">No photos are found!</h3>";
} ?></div>

Upvotes: 1

Views: 760

Answers (2)

Amr Berag
Amr Berag

Reputation: 1110

In a do while loop the condition is tested AFTER executing the statements within the loop. You should change it to WHILE only moving the condition to the top to fetch the first row notice that in the second iteration you are getting the first row becase in the first iteration execution the first row is not fetched yet.

Upvotes: 1

RToyo
RToyo

Reputation: 2877

The reason that the first iteration of your do while loop is returning blank values from $row_folders and $row_photos is because no value has been assigned to those variables until after the first iteration has completed (in the while statement)

To fix it, you can either assign the variable first, or (as is more commonly done) just use a while loop.

Define the variable first:

$row_folders = mysql_fetch_assoc($folders);
do
{
    // Your code here
}
while ($row_folders = mysql_fetch_assoc($folders));

Or Just use a while loop:

while ($row_folders = mysql_fetch_assoc($folders))
{
    // Your code here
}

Upvotes: 1

Related Questions