Jump to content

table loop


astralaaron
 Share

Recommended Posts

<table width="100%" height="100%" border="1"><tr><td><a href="main.php">click</a></td></tr><?php while ($rep = mysql_fetch_array($result)) { ?><tr><td><?php echo "<a href=\"container/" . $name . "/" . $rep['name'] . " \"><IMG SRC=\"container/" . $name ."/". $rep['name'] . "\" width=\"100\"></a>"; ?></td></tr><?php } ?></table

I have it working where I can upload pictures to a folder and then pull the names from a database to make a table with the pictures..problem is i dont want my table to be just 1 <td>then</tr>i want my tables of thumbnailed pictures to be something like this<table><tr><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td></tr></table>how can I loop the pictures into a table like that?

Link to comment
Share on other sites

Keep a counter of how many pictures you've shown and use the modulus operator to figure out when you need to start another row.http://www.php.net/manual/en/language.oper....arithmetic.phpThe modulus returns the remainder of a division, so if you mod the counter by 4 the result will be 0 if the counter is a multiple of 4, and you can output </tr><tr> before you output the next <td>.

Link to comment
Share on other sites

Keep a counter of how many pictures you've shown and use the modulus operator to figure out when you need to start another row.http://www.php.net/manual/en/language.oper....arithmetic.phpThe modulus returns the remainder of a division, so if you mod the counter by 4 the result will be 0 if the counter is a multiple of 4, and you can output </tr><tr> before you output the next <td>.
i figured there is a way to count how many times it has looped..thanks I will check the link
Link to comment
Share on other sites

Here's the loop I use in my gallery. % is the mod operator. This is around line 1150 from the index.php script in the gallery.

$nr = 0;	while (($row = @mysql_fetch_assoc($result)) && $nr < $num)	{	  if ($nr % 4 == 0)		echo "<tr class=\"text_dark_content\">";	  echo "<td width=25% align=\"center\" valign=\"top\">";	  echo "<a href=\"index.php?mode=viewimg&imgID=" . $row['imgID'] . "&sort=$sort&desc=$desc&ord=$ord&galID=$galID\" target=\"_blank\"><img src=\"view_img.php?imgID={$row['imgID']}&mode=th\" border=0><br>{$row['name']}</a>";	  if ($desc == 1)		echo "<br>" . $row['desc'];	  echo "</td>";	  if ($nr % 4 == 3)		echo "</tr>";	  $nr++;	}	if ($nr % 4 != 1 && $nr % 4 != 0)	  echo "</tr>";

Link to comment
Share on other sites

<?php $name = $_GET['name']; //holds the gallery namemysql_connect("localhost","root","astralaaron")or die("connection error");mysql_select_db("galleries")or die("database 1 error");$sql = "SELECT * FROM $name ORDER BY id DESC";$result = mysql_query($sql)or die("no sql result");?><html><body><table width="100%" height="100%" border="1"><tr><td><a href="main.php">click</a></td></tr><tr><?php $nr = 0;while ($rep = mysql_fetch_array($result)) && $nr < $num  {	if ($no % 4 == 0)	echo "<tr>";	echo "<td width=25% align=\"center\" valign=\"top\">";	echo "<a href=\"container/" . $name . "/" . $rep['name'] . " \"><IMG SRC=\"container/" . $name ."/". $rep['name'] . "\" width=\"100\"></a>"; 	echo "</td>";		if ($nr % 4 == 3)	echo "</tr>";		$nr++;} if ($nr % 4 != 1 %% $nr % 4 1= 0)	echo "</tr>";	?></tr></table></body></html>

Parse error: syntax error, unexpected T_BOOLEAN_AND in C:\wamp\www\picture\gallery.php on line 23not sure whats going on here

Link to comment
Share on other sites

ok i made the changes.. there are no errors but no pictures are showing up.by the way where does that $num variable come from?EDIT~

<?php $name = $_GET['name']; //holds the gallery namemysql_connect("localhost","root","astralaaron")or die("connection error");mysql_select_db("galleries")or die("database 1 error");$sql = "SELECT * FROM $name ORDER BY id DESC";$result = mysql_query($sql)or die("no sql result");?><html><body><table width="100%" height="100%" border="1"><tr><td><a href="main.php">click</a></td></tr><tr><?php $nr = 0;while (($rep = mysql_fetch_array($result)) && $nr < $num)  {	if ($nr % 4 == 0)	echo "<tr>";	echo "<td>";	echo "<a href=\"container/" . $name . "/" . $rep['name'] . " \"><IMG SRC=\"container/" . $name ."/". $rep['name'] . "\" width=\"100\"></a>"; 	echo "</td>";		if ($nr % 4 == 3)	echo "</tr>";		$nr++;} if ($nr % 4 != 1 && $nr % 4 != 0)	echo "</tr>";	?></tr></table></body></html>

**EDIT ~if anyone can help with this please do

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...