Jump to content

[SOLVED] <br> after every certain amount of entry's


DeanWhitehouse

Recommended Posts

How can i put a < br > after every 4 amount of entry's

What i need is to start a new line every after four entry's are echoed, this is my code

<?php
$query = "SELECT image_id, image_name, image_link, folder_name,img_time FROM `darkflame_gallery`";
$result = mysql_query($query) or die("Error:" . mysql_error());

if (mysql_num_rows($result) > 0) 
{
echo "<table class='news'><tr><th>Image Gallery</th></tr><tr><td id='new'><br><br />";
	while ($row = mysql_fetch_assoc($result)) {
	echo "<b>".$row['img_time']."</b>"; ?> - <a href="?image_id=<?php echo $row['image_id']; ?>"><?php echo $row['image_name']; ?></a><br><?php echo nl2br(stripslashes($cont));; ?><br><br>
	<?php
	}
}
?>

Link to comment
Share on other sites

ok, slightly lost on how to do it,

but if i do that how will it know when to start a new line?

 

someone gave me this code, but i can't get it to work properly now

<?php
$query = "SELECT image_id, image_name, image_link, folder_name,img_time FROM `darkflame_gallery`";
$result = mysql_query($query) or die("Error:" . mysql_error());
if (mysql_num_rows($result) > 0) {
?>
<table border="1">
<?php
               $loop = 4;
		   $loop1 = 4;
	while($row=mysql_fetch_array($result)){ // Start looping table row

	if ($loop == 4) {
                        echo "<tr>";
                }
	$toecho1 .= '<td><a href="?image_id='.$row['image_id'].'">Details</a></td><td>    </td>';
	$toecho .= '<td><a href="'.$row['image_link'].'" rel="lightbox [main]" title="'.$row['image_caption']. '" ><img src="'.$row['image_link'].'"width="150px" /></a></td><td>    </td>';

                
                if ($loop == 4) 
			{
                        echo "</tr>";
                        $loop=0;
                }   


	}
               if (substr($toecho, -5) != "</tr>") {
                  $toecho .= "</tr>";
               }
               echo $toecho;
               echo $toecho1;
}

?>		
</table>
<?php
		if (mysql_num_rows($result) < 1) {
		echo "No Images To Display";
		}
?>

Link to comment
Share on other sites

Throw in a variable, such as $i.

 

Increase it every time through the loop.

 

If it is 4, reset the variable and break the line.

 

$i = 0;
while ($something) {
     if ($i == 4) {
          $i = 0; // reset variable
           echo "<BR>"; // break the line
     }

     // something here...


     $i++; // increase $i each time through the loop
}

Link to comment
Share on other sites

OR

 

<?php
   $array = range("a","z");
   for ( $i = 0; $i < count($array); ++$i )
   {

         print ($i + 1) % 4 == 0 ?  "$i -- Matched Fourth!!!<br />" : '';

   }
?>

 

I use ($i + 1) because array is zero based.

Link to comment
Share on other sites

ok, i tried this

<?php
$i = 0;
while (mysql_num_rows($result) > 0) {
     if ($i == 4) {
          $i = 0; // reset variable
           echo "<BR>"; // break the line
     }

	echo "<b>".$row['img_time']."</b>"; ?> - <a href="?image_id=<?php echo $row['image_id']; ?>"><?php echo $row['image_name']; ?></a><br><?php echo nl2br(stripslashes($cont));; ?><br><br>
	<?php

     $i++; // increase $i each time through the loop
}
?>

 

and this doesn't show my results, and also keeps loading, i have had to stop the script twice as it crashed my browser.

 

this is the code surrounding it

<?php
$query = "SELECT image_id, image_name, image_link, folder_name,img_time FROM `darkflame_gallery`";
$result = mysql_query($query) or die("Error:" . mysql_error());
$i = 0;
while (mysql_num_rows($result) > 0) {
     if ($i == 4) {
          $i = 0; // reset variable
           echo "<BR>"; // break the line
     }

	echo "<b>".$row['img_time']."</b>"; ?> - <a href="?image_id=<?php echo $row['image_id']; ?>"><?php echo $row['image_name']; ?></a><br><?php echo nl2br(stripslashes($cont));; ?><br><br>
	<?php

     $i++; // increase $i each time through the loop
}
?>

 

Link to comment
Share on other sites

how can i get it to show line like this

 

|title|

name1|name2|name3|

image1|image2|image3|

 

 

i tried this

//No ID passed to page, display user list:
$query = "SELECT image_id, image_name, image_link, folder_name,img_time FROM `darkflame_gallery`";
$result = mysql_query($query) or die("Error:" . mysql_error());
$i = 0;
?>
<table><tr><td>
<?php
while ($row = mysql_fetch_assoc($result))  {
     if ($i == 4) {
          $i = 0; // reset variable
           echo "<BR>"; // break the line
     }

	echo "<b>".$row['img_time']."</b></td></tr><tr><td>"; ?><a href="?image_id=<?php echo $row['image_id']; ?>"><?php echo $row['image_name']; ?></a>
	<?php

     $i++; // increase $i each time through the loop
}
?>
</td></tr></table>

 

do i need to have two seperate echo's? or two while loops?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.