dflow Posted November 23, 2009 Share Posted November 23, 2009 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> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 23, 2009 Share Posted November 23, 2009 <?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; ?> Quote Link to comment Share on other sites More sharing options...
dflow Posted November 23, 2009 Author Share Posted November 23, 2009 thanks the script works but 2 things go wrong firstit only creates a table with 5 images ?? and doesnt continue to get all images Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 23, 2009 Share Posted November 23, 2009 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? Quote Link to comment Share on other sites More sharing options...
dflow Posted November 24, 2009 Author Share Posted November 24, 2009 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 ; Quote Link to comment Share on other sites More sharing options...
dflow Posted November 24, 2009 Author Share Posted November 24, 2009 help with this riddle why is this script skipping the first image in the loop? Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 24, 2009 Share Posted November 24, 2009 Need to see your PHP code. I suspect you may has misimplemented the code I provided. Perhaps you are doing a mysql_fetch_assoc() before the while loop, which would "use up" the first record before the loop starts. Quote Link to comment Share on other sites More sharing options...
dflow Posted November 24, 2009 Author Share Posted November 24, 2009 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 24, 2009 Share Posted November 24, 2009 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!!! Quote Link to comment Share on other sites More sharing options...
dflow Posted November 25, 2009 Author Share Posted November 25, 2009 thanks works like a charm Quote Link to comment Share on other sites More sharing options...
dflow Posted November 25, 2009 Author Share Posted November 25, 2009 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? Quote Link to comment 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.