plugnz Posted May 18, 2010 Share Posted May 18, 2010 Ok, This is probably php 101 but I just cannot get the for loop to do what I want it to do. I've been trying to display images from the database horizontally on the page. I keep losing the image so I've decided to go back to basics to try it with some text. What I want is this.... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 but i get this... 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 the code looks like this... $y=0; $x=1; $i=2; for ($y=1;$y<=1;$y++) { echo "<tr>"; for ($x=1;$x<=5;$x++) { echo "<td>$image_id </td>"; } echo "</tr>"; } Ant ideas how to make this work??? Quote Link to comment https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/ Share on other sites More sharing options...
BillyBoB Posted May 18, 2010 Share Posted May 18, 2010 I need to see more of the code.. where is $image_id set? Quote Link to comment https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/#findComment-1059827 Share on other sites More sharing options...
plugnz Posted May 18, 2010 Author Share Posted May 18, 2010 Hi BillyBoB, $image_id is set in the database echo "<table>"; echo "<tr ><td >"; echo "<h1>Category: " . $category_name . " </h1>\n"; echo "</td></tr>"; $ImageDir = "img/category/"; $ImageThumb = $ImageDir . "thumbnails/"; $getpic = "SELECT image_id, image_group, image_name, location_no " . //$getpic = "SELECT image_id " . "FROM cms_images_category " . "WHERE category_name = '" . $_GET['category'] . "'"; "ORDER BY image_group "; $results = mysql_query($getpic,$conn) or die(mysql_error()); while ($row = mysql_fetch_array($results)) { extract($row); $images = $ImageThumb . $image_id . ".jpg"; $y=0; $x=1; $i=2; for ($y=1;$y<=1;$y++) { echo "<tr>"; for ($x=1;$x<=5;$x++) { echo "<td>$image_id </td>"; } echo "</tr>"; } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/#findComment-1059829 Share on other sites More sharing options...
teamatomic Posted May 18, 2010 Share Posted May 18, 2010 You're thinking is way more complicated than it really is <table><tr> <?php //$ct=count(images); $ct=100; for($i=1;$i<=$ct;$i++) { echo "<td>$i</td>\n"; if($i%7==0)echo "</tr><tr>\n"; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/#findComment-1059832 Share on other sites More sharing options...
plugnz Posted May 18, 2010 Author Share Posted May 18, 2010 Hey thanks, tried it and works great for numbers but when I apply it to the images i get 100 times each image unless I change $ct to 1 and then the images don't wrap they just keep going to the right. $ct=100; for($i=1;$i<=$ct;$i++) { echo "<td><a href =\"".$ImageDir . $image_id . ".jpg\" target='top'>"; echo "<img src=\"".$images . "\"></a></td>"; if($i%10==0)echo "</tr>\n"; } if I try to attach [$i] to end of $images i lose the image path and still get the repeats. Quote Link to comment https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/#findComment-1059839 Share on other sites More sharing options...
teamatomic Posted May 18, 2010 Share Posted May 18, 2010 count had to be the number of images you have and you have to put your while inside the loop Quote Link to comment https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/#findComment-1059841 Share on other sites More sharing options...
.josh Posted May 18, 2010 Share Posted May 18, 2010 // database query stuff here echo "<tr>"; while ($row = mysql_fetch_array($results)) { extract($row); $images = $ImageThumb . $image_id . ".jpg"; echo "<td><a href =\"".$ImageDir . $image_id . ".jpg\" target='top'>"; echo "<img src=\"".$images . "\"></a></td>"; echo ($x % 7 == 0)? "</tr><tr>" : ""; $x++; } echo "</tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/#findComment-1059851 Share on other sites More sharing options...
plugnz Posted May 18, 2010 Author Share Posted May 18, 2010 Cor thats the closest so far... just a few tweaks needed. for some reason the first image seems to want to occupy the whole row by itself, with the rest doing as they're told and then how do I make it so the images only fill the available screen width rather than a select number wide? I want it to fill the screen as much as possible. But thanks every one Quote Link to comment https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/#findComment-1059861 Share on other sites More sharing options...
kenrbnsn Posted May 18, 2010 Share Posted May 18, 2010 If you want to fill the whole screen, you have to stop using tables and learn how to use floating divs in css. Here's one tutorial: http://www.w3schools.com/css/css_image_gallery.asp Another tutorial can be found at http://css.maxdesign.com.au/floatutorial/ Ken Quote Link to comment https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/#findComment-1059862 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.