Jump to content

do while conditions


dflow

Recommended Posts

i want to display 6 images in a 2x3 table

now what would be the correct logic to

count that there are 6 images results

and if there are more than 6 images loop and echo the rest but not in the table

but as links?

 

the idea is to have 6 thumbnails and then a large images in a gallery

 

this is what i played with it gives me the table i want , how should i add the additional condition - echo the remaining inages after the 6th??

 

<table>
              <tr>
                <?php
$RsImage_list_endRow = 0;
$RsImage_list_columns = 2; // number of columns
$RsImage_list_hloopRow1 = 0; // first row flag
do {
    if($RsImage_list_endRow == 0  && $RsImage_list_hloopRow1++ != 0) echo "<tr>";
   ?>
                <td width="85"><a href="/images/<?php echo $row_RsImage_list['imagename']; ?>" rel="lightbox[<?php echo $row_RsImage_list['ProductID']; ?>]"><img src="<?php echo $row_RsImage_list['imagename']; ?>" width="85" height="64" border="0"/></a></td>
                <?php  $RsImage_list_endRow++;
if($RsImage_list_endRow >= $RsImage_list_columns) {
  ?>
              </tr>
              <?php
$RsImage_list_endRow = 0;
  }
} while ($row_RsImage_list = mysql_fetch_assoc($RsImage_list));
if($RsImage_list_endRow != 0) {
while ($RsImage_list_endRow < $RsImage_list_columns) {
    echo("<td> </td>");
    $RsImage_list_endRow++;
}
echo("</tr>");
}?>
            </table>

Link to comment
Share on other sites

<?php

$RsImage_list_rows = 3; // number of rows
$RsImage_list_cols = 2; // number of columns

$RsImage_list_max = $RsImage_list_rows * $RsImage_list_cols;
$RsImage_list_current = 0; // counter
$RsImage_list_tableHTML = '';
$RsImage_list_linksHTML = '';

while ($row_RsImage_image = mysql_fetch_assoc($RsImage_list))
{
    $RsImage_list_current++;

    if ($RsImage_list_current<=($RsImage_list_max))
    {
        //Add to table output

        //Create new row (if needed)
        if ($RsImage_list_current % $RsImage_list_cols == 1)
        {
            $RsImage_list_tableHTML .= "  <tr>\n";
        }

        //Add image
        $RsImage_list_tableHTML .= "    <td width=\"85\">";
        $RsImage_list_tableHTML .= "<a href=\"/images/{$row_RsImage_image['imagename']}\"";
        $RsImage_list_tableHTML .= " rel=\"lightbox[{$row_RsImage_image['ProductID']}]\">";
        $RsImage_list_tableHTML .= "<img src=\"{$row_RsImage_image['imagename']}\" width=\"85\" height=\"64\" border=\"0\"/>";
        $RsImage_list_tableHTML .= "</a>";
        $RsImage_list_tableHTML .= "</td>\n";

        //Close row (if needed)
        if ($RsImage_list_current % $RsImage_list_cols == 0)
        {
            $RsImage_list_tableHTML .= "  </tr>\n";
        }
    }
    else
    {
        //Add to links output
        $RsImage_list_linksHTML .= "<a href=\"/images/{$row_RsImage_image['imagename']}\"";
        $RsImage_list_linksHTML .= " rel=\"lightbox[{$row_RsImage_image['ProductID']}]\">{$row_RsImage_image['imagename']}</a><br />\n";
    }
}

//Close last row if needed
if ($RsImage_list_current % $RsImage_list_cols != 0)
{
    do
    {
        $RsImage_list_tableHTML .= "<td> </td>\n";
        $RsImage_list_current++;
    } while ($RsImage_list_current % $RsImage_list_cols != 0);

    $RsImage_list_tableHTML .= "</tr>\n";
}

?>
                

<table>
<?php echo $RsImage_list_tableHTML; ?>
</table>
<br />
<?php echo $RsImage_list_linksHTML; ?>

Link to comment
Share on other sites

Since I don't have your database, I tested the script using an array. I tested with more than six records, exactly six records, and with less than six records. All of them worked fine. Are you sure there are more than five records returned from the query? Have you checked the rendered HTML to ensure there isn't a character in the query results that might be bungling the HTML?

Link to comment
Share on other sites

Since I don't have your database, I tested the script using an array. I tested with more than six records, exactly six records, and with less than six records. All of them worked fine. Are you sure there are more than five records returned from the query? Have you checked the rendered HTML to ensure there isn't a character in the query results that might be bungling the HTML?

 

html looks fine

 

it's skipping the first image in the series for some reason

 

most of my products have more than 6 images to it in the DB

i get with my previous code 6 in the table and 6 in the gallery

my table looks like this

CREATE TABLE `image_list` (
  `imageID` int(11) NOT NULL auto_increment,
  `ProductID` int(11) NOT NULL,
  `imagename` varchar(255) collate utf8_unicode_ci NOT NULL,
  `imagelocation` varchar(255) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`imageID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=12144 ;

Link to comment
Share on other sites

this is the query:

<?php
mysql_select_db($database_international, $international);
$query_RsImage_list = sprintf("SELECT * FROM image_list WHERE ProductID = %s", GetSQLValueString($colname_RsImage_list, "int"));
$query_limit_RsImage_list = sprintf("%s LIMIT %d, %d", $query_RsImage_list, $startRow_RsImage_list, $maxRows_RsImage_list);
$RsImage_list = mysql_query($query_limit_RsImage_list, $international) or die(mysql_error());
$row_RsImage_list = mysql_fetch_assoc($RsImage_list);

if (isset($_GET['totalRows_RsImage_list'])) {
  $totalRows_RsImage_list = $_GET['totalRows_RsImage_list'];
} else {
  $all_RsImage_list = mysql_query($query_RsImage_list);
  $totalRows_RsImage_list = mysql_num_rows($all_RsImage_list);
}
$totalPages_RsImage_list = ceil($totalRows_RsImage_list/$maxRows_RsImage_list)-1;
?>
//and now the code copy and pasted into a div from what you did
<div id="product_main_image_container"><img src="<?php echo $row_RsProductInfo['ProductImage_1']; ?>" width="280" height="205"/></div>

    <div id="imagelist_container">
    
  <?php

$RsImage_list_rows = 3; // number of rows
$RsImage_list_cols = 2; // number of columns

$RsImage_list_max = $RsImage_list_rows * $RsImage_list_cols;
$RsImage_list_current = '0'; // counter
$RsImage_list_tableHTML = '';
$RsImage_list_linksHTML = '';

while ($row_RsImage_image = mysql_fetch_assoc($RsImage_list))
{
    $RsImage_list_current++;

    if ($RsImage_list_current<=($RsImage_list_max))
    {
        //Add to table output

        //Create new row (if needed)
        if ($RsImage_list_current % $RsImage_list_cols == 1)
        {
            $RsImage_list_tableHTML .= "  <tr valign=\"top\">\n";
        }

        //Add image
        $RsImage_list_tableHTML .= "    <td width=\"85\">";
        $RsImage_list_tableHTML .= "<a href=\"images/{$row_RsImage_image['imagename']}\"";
        $RsImage_list_tableHTML .= " rel=\"lightbox[{$row_RsImage_image['ProductID']}]\">";
        $RsImage_list_tableHTML .= "<img src=\"images/{$row_RsImage_image['imagename']}\" width=\"85\" height=\"64\" border=\"0\"/>";
        $RsImage_list_tableHTML .= "</a>";
        $RsImage_list_tableHTML .= "</td>\n";

        //Close row (if needed)
        if ($RsImage_list_current % $RsImage_list_cols == 0)
        {
            $RsImage_list_tableHTML .= "  </tr>\n";
        }
    }
    else
    {
        //Add to links output
        $RsImage_list_linksHTML .= "<a href=\"images/{$row_RsImage_image['imagename']}\"";
        $RsImage_list_linksHTML .= " rel=\"lightbox[{$row_RsImage_image['ProductID']}]\">{$row_RsImage_image['imagename']}</a><br />\n";
    }
}

//Close last row if needed
if ($RsImage_list_current % $RsImage_list_cols != 0)
{
    do
    {
        $RsImage_list_tableHTML .= "<td> </td>\n";
        $RsImage_list_current++;
    } while ($RsImage_list_current % $RsImage_list_cols != 0);

    $RsImage_list_tableHTML .= "</tr>\n";
}

?>
               

<table>
<?php echo $RsImage_list_tableHTML; ?>
</table>
<br />
<?php echo $RsImage_list_linksHTML; ?>  
    
    
    
    
    
    
    
  </div>
<?php
mysql_free_result($RsImage_list);
?>


thanks

hope this will help, i used DW for the query  i appologise in advance ;D

Link to comment
Share on other sites

As I suspected you are running a mysql_fetch_assoc() on the query results before the loop starts. So, the first record is always lost. And the variable you assign it to isn't even used anywhere in the script.

 

This is line 6 of your code:

$row_RsImage_list = mysql_fetch_assoc($RsImage_list);
// ^^^ - That line extracts the first record and does nothing with it!!!

 

Link to comment
Share on other sites

As I suspected you are running a mysql_fetch_assoc() on the query results before the loop starts. So, the first record is always lost. And the variable you assign it to isn't even used anywhere in the script.

 

This is line 6 of your code:

$row_RsImage_list = mysql_fetch_assoc($RsImage_list);
// ^^^ - That line extracts the first record and does nothing with it!!!

 

 

how would you start from the second image in the list?

Link to comment
Share on other sites

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.