Jump to content

building a table


MinusMyThoughts
 Share

Recommended Posts

hey, everyone!...i've got a whole new problem: i've built a photo album that allows a user to create a photo album and add photos to it......the problem i'm having is that when i attempt to show the thumbnails in a table that is 5x[whatever], i get stuck at my first row......i'm looking for a way to loop through the contents of my table, grabbing the first five thumbs, then printing a "</tr><tr>" before continuing with the next five. i'd like to be able to allow my users to theoretically upload as many photos as they want to, so if anyone's got an idea on how to set the loop up to be potentially infinite, i'd really appreciate it......this is the code that i'm using (that only works for the first five thumbs and fails when i attempt to add the "</tr><tr>"):

				$query = 'SELECT * FROM aKa_photo WHERE albumID="' . $albumID . '"';				$result = mysql_query($query);				$i = 1;			if(mysql_num_rows($result) >= 5)			  {				while($photo = mysql_fetch_array($result))				  {				  if($i <= 5)					{					echo '<td width="85px" height="85px" align="center">'					  . '<a href="'					  . $photo['photoPath'] . $photo['photoName']					  . '" target="_blank">'					  . '<img src="'					  . $photo['photoPath'] . 'thumbs/' . $photo['photoThumb']					  . '">'					  . '</a>'					  . '</td>';					$i++;					}				  echo '</tr><tr>';				  if($i <= 10)					{					echo '<td width="85px" height="85px" align="center">'					  . '<a href="'					  . $photo['photoPath'] . $photo['photoName']					  . '" target="_blank">'					  . '<img src="'					  . $photo['photoPath'] . 'thumbs/' . $photo['photoThumb']					  . '">'					  . '</a>'					  . '</td>';					$i++;					}				  }			  }			else			  {				while($photo = mysql_fetch_array($result))				  {				  if($i <= mysql_num_rows($result))					{					echo '<td width="85px" height="85px" align="center">'					  . '<a href="'					  . $photo['photoPath'] . $photo['photoName']					  . '" target="_blank">'					  . '<img src="'					  . $photo['photoPath'] . 'thumbs/' . $photo['photoThumb']					  . '">'					  . '</a>'					  . '</td>';					$i++;					}				  }				  $diff = 5 - mysql_num_rows($result);				while($i <= $diff)				  {				  echo '<td width="85px" height="85px" align="center">'					. '</td>';				  $i++;				  }			  }			  				echo '</tr>';			  ?>

...i'm aware that this code has issues all over it. i wasn't really sure how to troubleshoot the while() loops, which is why i'm here again......thanks, guys!love,jason

Link to comment
Share on other sites

This is a pretty common problem, when you know the answer it's obvious.You'll want to have a while loop print out your entire table, keeping track of which item it is on. After every 5th item, it closes the current row and starts a new one. After the loop finishes, you check if the last row has not been closed yet and close it if not. To do all this, you have your counter that you increment in the loop, and use the modulus operator (%) to calculate if the remainder is 0 (if the counter is a multiple of 5). If the remainder is 0, then it's time for a new row.

<?php$nr_per_row = 5;$nr = 0;echo "<tr>"; //start it offwhile (!$finished)  //whatever your condition is{  echo "<td>";  ...  echo "</td>";    if (++$nr % $nr_per_row == 0) //increment the counter, then check if it is a multiple of $nr_per_row  {	echo "</tr><tr>";  }}echo "</tr>"; //the last row will never be closed in this case?>

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...