GalaxyTramp Posted October 26, 2010 Share Posted October 26, 2010 Hi I have a page which returns 2 rows of images max 5 in each. Code is: <?php $query = mysql_query("SELECT COUNT(id) FROM " . $prev . "pics where id=" . $id . ""); list($number_records) = mysql_fetch_row($query); if ($number_records >= 10 ) { $number =10; //LIMIT IMAGES TO 10 } else { $number =$number_records; } echo "<br />"; echo "Displaying $number of $number_records images"; ?> <div class="thumb-right"> <dl> <?php for ($i = 0; $i < 5; $i++): ?> <dt> <img src="../pics/<?php echo $images[$i]; ?>" alt="" width="120" height="120" class="border1" /><br /><br /> </dt> <?php endfor; ?> </dl> </div> <div class="thumb-left"> <dl> <?php for ($i = 5; $i < $number; $i++): ?> <dt> <img src="../pics/<?php echo $images[$i]; ?>" alt="" width="120" height="120" border="0" class="border1" /><br /><br /> </dt> <?php endfor; ?> </dl> </div> This works fine if there are 10 images to display, but on occasions there may only be 7 for instance. This then puts 5 in the right column and only 2 in the left column which looks ugly. Can someone suggest a way of calculating the total number of images and then displaying them say 4 in the left column and 3 in the right column for this example? Thanks for your time GT Quote Link to comment Share on other sites More sharing options...
vichu.1985 Posted October 26, 2010 Share Posted October 26, 2010 Dude, Its a small logic as follows: <?php $query = mysql_query("SELECT COUNT(id) FROM " . $prev . "pics where id=" . $id . ""); list($number_records) = mysql_fetch_row($query); if ($number_records >= 10 ) { $number =10; //LIMIT IMAGES TO 10 } else { $number =$number_records; } echo "<br />"; echo "Displaying $number of $number_records images"; $left_side = ''; $right_side = ''; for($i=0;$i<$number;$i++){ if($i%2 == 0){ $left_side = '<dt> <img src="../pics/'.$images[$i].'" alt="" width="120" height="120" class="border1" /><br /><br /> </dt>'; } else{ $right_side = '<dt> <img src="../pics/'.$images[$i].'" alt="" width="120" height="120" border="0" class="border1" /><br /><br /> </dt>'; } } ?> <div class="thumb-right"> <dl> <?php print $right_side; ?> </dl> </div> <div class="thumb-left"> <dl> <?php print $left_side; ?> </dl> </div> Quote Link to comment Share on other sites More sharing options...
GalaxyTramp Posted October 26, 2010 Author Share Posted October 26, 2010 Thanks for your response. Your code results in only one image being displayed in each of the columns! GT Quote Link to comment Share on other sites More sharing options...
vichu.1985 Posted October 26, 2010 Share Posted October 26, 2010 Dude, Just replace the for loop with this for($i=0;$i<$number;$i++){ if($i%2 == 0){ $left_side .= '<dt> <img src="../pics/'.$images[$i].'" alt="" width="120" height="120" class="border1" /><br /><br /> </dt>'; } else{ $right_side .= '<dt> <img src="../pics/'.$images[$i].'" alt="" width="120" height="120" border="0" class="border1" /><br /><br /> </dt>'; } } Quote Link to comment Share on other sites More sharing options...
GalaxyTramp Posted October 26, 2010 Author Share Posted October 26, 2010 Yeh got there at the same time as you with the loop Thanks for your help GT 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.