Jump to content

Php Loop Through Database And Limit Number Of Entries


gilbertsavier
 Share

Recommended Posts

Hi,Going to try to explain this as best I can. I have a bunch of stuff in a database and I need to display this info within a table. This isn't a problem at all but, I need it to only display 3 td tags per line and then move on to the row.so i need it to do :code:<table><tr><td>My First Row</td><td>My First Row 2</td><td>My First Row 3</td></tr><tr><td>My Second Row</td><td>My Second Row 2</td><td>My Second Row 3</td></tr></table>and continue to do this through every entry in the database table.so I have something like this that will display the info:code:for ($i = 1; $i <= $num; $i++){ $chairArray = mysql_fetch_array($chairResult); $chairName = $chairArray['name']; $chairDescription = $chairArray['description']; $chairImage = $chairArray['image']; echo "<tr>"; echo "<td>"; echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />"; echo "<br />"; echo $chairName; echo "</td>"; echo "</tr>"; }

Link to comment
Share on other sites

You can use the modulus operator to divide the results into rows of 3:

$i = 0;echo "<table>";echo "<tr>";$start = false;while($chairArray = mysql_fetch_array($chairResult)) {  if($i%3 == 0) {	if(!$start) {	  $start = true;	} else {	  echo "</tr><tr>";	}  }  $chairName = $chairArray['name'];  $chairImage = $chairArray['image'];  echo "<td>";  echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />";  echo "<br />";  echo $chairName;  echo "</td>";  $i++;}echo "</tr>";echo "</table>";

Edited by Ingolme
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...