DeanWhitehouse Posted May 8, 2008 Share Posted May 8, 2008 how can i start a new line after every three database entry's ? here is my code <div id="body"> <?php if (isset($_GET['image_id'])) { if ((int) $_GET['image_id'] > 0) { $imageid = $_GET['image_id']; $sql = "SELECT * FROM hayleyimages WHERE image_id=$imageid"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); $imageprofname = $row['image_name']; $imageprofcaption = $row['image_caption']; $imageproflink = $row['image_link']; if(mysql_num_rows($result) > 0) { ?> <table align="center" border="1"><th><?php echo "$imageprofname"; ?></th> <tr><td><?php echo '<a href="'.$row['image_link'].'" rel="lightbox" title="'.$row['image_caption']. '" ><img src="'.$row['image_link'].'"width="200px" /></a>'; ?></td><td width="200px"><?php echo 'Image ID :'.$row['image_id'].''; ?></td></tr><tr><td><?php echo ' '.$row['image_caption']. ' </a>'; ?></td></tr></table> <?php } if (mysql_num_rows($result) < 1) { echo 'This ID does not exist in the database<br>'; echo "<a href=\"index.php\">Return to Image Page</a>"; exit(); } exit(); } else { echo "Unknown Image ID! <br />"; echo "<a href=\"index.php\">Return to Image Page</a>"; exit(); } } //No ID passed to page, display user list: $query = "SELECT image_id, image_name, image_link, image_caption FROM `hayleyimages`"; $result = mysql_query($query) or die("Error:" . mysql_error()); if (mysql_num_rows($result) > 0) { ?> <table border="1"><th>Images</th> <?php while($row=mysql_fetch_array($result)){ // Start looping table row ?> <tr><td><?php echo ' <a href="?image_id='.$row['image_id'].'">'.$row['image_name']. ' Details</a>'; ?></td><td><?php echo ' <a href="'.$row['image_link'].'" rel="lightbox [main]" title="'.$row['image_caption']. '" >'.$row['image_name']. ' </a>'; ?></td><td><?php echo ' <img src="'.$row['image_link'].'"width="150px" /></a>'; ?></td></tr> <?php } ?> </table> <?php if (mysql_num_rows($result) < 1) { echo "No Images To Display"; } } mysql_close(); ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/104750-solved-limit-mysql_query-amount/ Share on other sites More sharing options...
psychowolvesbane Posted May 8, 2008 Share Posted May 8, 2008 Use a counter variable in your while loop and increment it for each loop: if($counter%3==0&&$counter!=0) echo"</tr><tr>"; Use this for the incrementing: ++$counter; Quote Link to comment https://forums.phpfreaks.com/topic/104750-solved-limit-mysql_query-amount/#findComment-536213 Share on other sites More sharing options...
DeanWhitehouse Posted May 8, 2008 Author Share Posted May 8, 2008 ok, thanks, can you give me an example of how to use this, as i am not very good with loops Quote Link to comment https://forums.phpfreaks.com/topic/104750-solved-limit-mysql_query-amount/#findComment-536214 Share on other sites More sharing options...
DarkWater Posted May 8, 2008 Share Posted May 8, 2008 <table border="1"><th>Images</th> <?php $loop = 3; while($row=mysql_fetch_array($result)){ // Start looping table row if ($loop == 3 { echo "<tr>"; } $toecho .= '<td><a href="?image_id='.$row['image_id'].'">'.$row['image_name']. ' Details</a>; </td><td> <a href="'.$row['image_link'].'" rel="lightbox [main]" title="'.$row['image_caption']. '" >'.$row['image_name']. ' </a></td><td> <img src="'.$row['image_link'].'"width="150px" /></a></td>; <?php if ($loop == 3 { echo "</tr>"; $loop=0; } } if (substr($toecho, -5) != "</tr>") { $toecho .= "</tr>"; } echo $toecho; </table> Try it. Didn't test it, but it should work. Quote Link to comment https://forums.phpfreaks.com/topic/104750-solved-limit-mysql_query-amount/#findComment-536219 Share on other sites More sharing options...
DeanWhitehouse Posted May 8, 2008 Author Share Posted May 8, 2008 thanks, had to change some things, but it works Quote Link to comment https://forums.phpfreaks.com/topic/104750-solved-limit-mysql_query-amount/#findComment-536224 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.