music_lp90 Posted April 27, 2007 Share Posted April 27, 2007 Hi, I'm trying to get my images put into rows and columns. Right now I'm trying 3 columns. Here's the code. <?php $dir='garages/Ranch'; $open=opendir($dir); $allow=array('jpg','jpeg', 'JPEG', 'JPG'); $i = 0; while (($file=readdir($open))!==false) { $ext=str_replace('.', '', strrchr($file, '.')); if (in_array($ext, $allow)) { if ($i > 3) $i=1; else $i++; if ($i = 1) echo '<tr><td><a target="_new" href="'.$dir.'/'.$file.'"><img style="border-color:#000000 " border="4" width="20%" height="20%" src="'.$dir.'/'.$file.'"></a> </td>'; elseif ($i = 2) echo '<td><a target="_new" href="'.$dir.'/'.$file.'"><img style="border-color:#000000 " border="4" width="20%" height="20%" src="'.$dir.'/'.$file.'"></a> </td>'; else echo '<td><a target="_new" href="'.$dir.'/'.$file.'"><img style="border-color:#000000 " border="4" width="20%" height="20%" src="'.$dir.'/'.$file.'"></a> </td></tr>'; } } closedir($open);?> I'm trying to have a counter that when $i = 1 then a new <tr> will be started as well as <td></td> tags around the image. Then when the $i = 2 only <td></td> will be inserted, and finally when $i = 3 <td></td> tags will be around the image followed by a </tr> tag to close the fir <tr> that was made. and it will continue to loop through this for as many images there are, creating a new row after every three images. Can you tell me if my code is close to being right? I'm really just going through trial and error right now as I don't really know what I'm doing. Thanks for any help! Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted April 27, 2007 Share Posted April 27, 2007 Hi!First: = is an assignment operator (so what you're doing in each loop is that you assign 1 to $i [which eveluates to true]).You need to use the comparisionoperator == ("is equal") instead, then your code should work.Second: You have the same text in all three "options" (<td><a ...</a></td>.), to shorten your code, or atleast make it cleaner do someing like this: <?php $dir = 'garages/Ranch'; $allow = array('jpg','jpeg', 'JPEG', 'JPG'); $open = opendir($dir); $i = 0; while (($file=readdir($open))!==false) { $ext=str_replace('.', '', strrchr($file, '.')); if (in_array($ext, $allow)) { if ($i == 0) echo "<tr>\n"; echo '<td><a target="_new" href="'.$dir.'/'.$file.'"><img style="border-color:#000000 " border="4" width="20%" height="20%" src="'.$dir.'/'.$file.'"></a> </td>'; if ($i >= 3) { echo '</tr>'; $i = 0; } else $i++; } } closedir($open);?> Hope that helped!Good Luck and Don't Panic! Link to comment Share on other sites More sharing options...
music_lp90 Posted April 28, 2007 Author Share Posted April 28, 2007 Thanks Mr. Chisol, I added the == to my code and it began adding the columns and rows. I also tried your cleaner version of the code and that worked as well. The problem I'm having now with both of them is that for some reason it is creaiting huge amount of space between each column. I can't figure out where the space is coming from, do you know? I put the php code between a table like this <table width="760" cellpadding="0" cellspacing="0"><?php code here? followed by </table> Link to comment Share on other sites More sharing options...
music_lp90 Posted April 28, 2007 Author Share Posted April 28, 2007 Nevermind, I went and made thumbnail images of each image instead of resizing it with the code and it works great now. It takes less time to load too. Thanks for your help! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.