Page 1 of 1

while() function.... new lines after every 5 entries?

Posted: Sun Feb 26, 2006 6:44 am
by goobywaj
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi guys,

I'm using the while() function to create a picture gallery. The code gets the image titles from a database and if they are in the selected gallery they display them. However, I would like them to display 5 images together and then insert a < br >.

How is this possible?

Heres the code I have so far (note: the database connection is already open)

Code: Select all

<?php
if (!$_GET[gallery]) {
$sel_gal = "1";
} else {
$sel_gal = $_GET['gallery'];
}

$getpics = "select * from gallery where gallery = $sel_gal";
$getpics_res = mysql_query($getpics, $conn) or die(mysql_error());
$num_pic_in_gal = mysql_num_rows($getpics_res);
if ($num_pic_in_gal = "0") {
$display = "There are no pictures in this gallery";
} else {
$display = "<DIV 
      style=\"padding:0px; MARGIN: 0px; OVERFLOW: auto; BORDER-LEFT: 1px solid gray; WIDTH: 852; BORDER-BOTTOM: 1px solid gray; HEIGHT: 299\">
      <TABLE cellSpacing=0 cellPadding=0 border=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\">
        <COLGROUP>
        <COL width=100%>
        <TBODY>
        <TR>
          <TD class=description vAlign=top noWrap width=841>
          <table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\" id=\"AutoNumber1\">
            <tr>";
while ($picarray = mysql_fetch_array($getpics_res)) {
    $thumb = $picarray['picID'];
if (mb_strlen($thumb) == "1") {
$thumb1 = "00";
$thumb1 .= $thumb;
} else if (mb_strlen($thumb) == "2") {
$thumb1 = "0";
$thumb1 .= $thumb;
} else {
$thumb1 = $thumb;
}
$maxpics = "5";
$display .= "<td><img src=\"http://www.spartacusband.com/gallery/thumbnails/".$thumb1.".jpg\"></td>";
   }
$display .="</tr>
          </table>
          </TD>
          </TR>
        </TBODY></TABLE></DIV>";
}
echo $display;
?>

You can view what is currently happening at http://www.spartacusband.com/gallery.php

Thanks


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sun Feb 26, 2006 7:33 am
by Planplan

Code: Select all

//outside of the loop
$nbimg = 1;

Code: Select all

//in your loop
if(($nbimg++)%$maxpics == 0) echo '<br />';
"%" give the modulo of a number (5 in your example).
Here how it work:

Code: Select all

1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
(...)
9 % 5 = 4
10 % 5 = 0
11 % 5 = 1
(...)
It represent the number resting after a division.

Code: Select all

2 / 5 = 0 [2 are resting]
24 / 5 = 4 [4 are resting]
Not sure I'm very clear but I'm not using my native language ;)
Wish it help anyway ^^

Posted: Sun Feb 26, 2006 8:24 am
by goobywaj
Thanks Planplan, that script helps a little bit. It adds in the correct amount of line breaks, but before the picture array... it doesnt actually break the pictures up at all.

Posted: Sun Feb 26, 2006 8:51 am
by muckyrabbit
Because your using a table to display the images, instead of adding a <br /> tag after every five pictures, try adding a new <tr> tag.Obviously you need to close the old <tr> before.

edit the previously posted code to look something like this.

Code: Select all

//in your loop 
if(($nbimg++)%$maxpics == 0) echo '</tr><tr>';
Something like that should work.

Posted: Sun Feb 26, 2006 9:53 am
by feyd
There's a thread where I posted an arbitrary length row table display referenced in Useful Posts (look in the Forum Tour for a link to that if need be)