Jump to content

[SOLVED] every fifth entry create a break


dennismonsewicz

Recommended Posts

I need a way of checking to see if something equals a particular number, in my case 5, and every 5th entry create a new line. Any way to do this?

 

echo '<table align=center border=0 width=650 cellpadding=4 cellspacing=0 class=imgtable>
			<tr>
				<td><u><b>Image</b></u></td>
			</tr>
			<tr>
				<td valign="top">';

	//And we display the results
	while($result = mysql_fetch_array( $data ))
	{
		$kbsize = $result['size'] / 1024;

		//$name = str_replace($find, "<span class='yellowbg'>$find</span>", $result['name']);
		$size = str_replace($find, "<span class='yellowbg'>$find</span>", $kbsize);
		$type = str_replace($find, "<span class='yellowbg'>$find</span>", $result['type']);
		$categories = str_replace($find, "<span class='yellowbg'>$find</span>", $result['categories']);
		$id = $result['id'];

		$filecount = count($result['name']);

		$typenoimage = str_replace("image/", "", $type);

			if(count($result['name'] == 5))
			{	
				$br = "<br />";
			} else {
				$br = "";
			}

		echo '<img src="imageuploads/thumbs/' . $result['name'] . '" alt=' . $result['name'] . ' border=0>';
		echo $br;
	}

Link to comment
https://forums.phpfreaks.com/topic/99037-solved-every-fifth-entry-create-a-break/
Share on other sites

Setup a counter, increment the counter in the loop. Add an if statement which checks to see if the counter is at 5, if it is reset the counter and echo the a newline. Otherwise increment counter:

 

echo '<table align=center border=0 width=650 cellpadding=4 cellspacing=0 class=imgtable>
			<tr>
				<td><u><b>Image</b></u></td>
			</tr>
			<tr>
				<td valign="top">';

	//And we display the results
        $i = 0; // initialise counter
	while($result = mysql_fetch_array( $data ))
	{
		$kbsize = $result['size'] / 1024;

		//$name = str_replace($find, "<span class='yellowbg'>$find</span>", $result['name']);
		$size = str_replace($find, "<span class='yellowbg'>$find</span>", $kbsize);
		$type = str_replace($find, "<span class='yellowbg'>$find</span>", $result['type']);
		$categories = str_replace($find, "<span class='yellowbg'>$find</span>", $result['categories']);
		$id = $result['id'];

		$filecount = count($result['name']);

		$typenoimage = str_replace("image/", "", $type);

		echo '<img src="imageuploads/thumbs/' . $result['name'] . '" alt=' . $result['name'] . ' border=0>';

            // check if $i is equal to 5, if it is echo line break and reset counter
            if($i == 5)
		{
		    $br = "<br />";
                $i = 0; // reset counter
		}
            else
                $i++; // increment counter
	}

this is what it looks like in FF:

 

14mvvgx.jpg

 

In IE:

 

11rrurq.jpg

 

And here is my code :

 

//And we display the results
        $i = 0; // initialise counter
	while($result = mysql_fetch_array( $data ))
	{
		$kbsize = $result['size'] / 1024;

		//$name = str_replace($find, "<span class='yellowbg'>$find</span>", $result['name']);
		$size = str_replace($find, "<span class='yellowbg'>$find</span>", $kbsize);
		$type = str_replace($find, "<span class='yellowbg'>$find</span>", $result['type']);
		$categories = str_replace($find, "<span class='yellowbg'>$find</span>", $result['categories']);
		$id = $result['id'];

		$filecount = count($result['name']);

		$typenoimage = str_replace("image/", "", $type);

		echo '<img src="imageuploads/thumbs/' . $result['name'] . '" alt=' . $result['name'] . ' border=0>';

		if($i == 5)
		{
		    echo "<br />";
                $i = 0; // reset counter
		} else {
                $i++; // increment counter
		}

            // check if $i is equal to 5, if it is echo line break and reset counter
	}

	echo '</td>
		</tr>
		</table>';

Archived

This topic is now archived and is closed to further replies.

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