Jump to content

Putting images in rows and columns


music_lp90

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...