Show 3 columns per row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Show 3 columns per row

Post by Nay »

i've been programming for the past 12 hours......er......on differnet scripts.....heh....and am stuck on this one. it puzzles me, the tr and td comes out all wierd. I looked around for tutorials on this. bumped into one at spoono too but didn't really help O.o.

anyhow, it'd be great if anyone can point me out. the script works fine, technicly. no parse errors or anything but the display part is just wack. right now it displays 1 image then, 2 images and then 1 image. there's a total of four images. 3 rows right now. should be 2 rows with 3 images on the first and one on the last.

Code: Select all

$query = "SELECT image_id, image_url, image_thumb_url, image_caption FROM hnhs_gallery ORDER BY image_id DESC";
$result = $mysql->queryFetch($query);

$content = '';

for($i = 0; $i < count($result); $i++) {

   $rows = $result[$i];

   $caption = stripslashes($rows['image_caption']);

   // get image information
   $thumb = $thumbs_dir . $rows['image_thumb_url'];
   $big = $big_dir . $rows['image_url'];

   $thumbImg = $thumbs_url . $rows['image_thumb_url'];
   $bigImg = $big_url . $rows['image_url'];

   $thumbInfo = getimagesize($thumb);
   $bigInfo = getimagesize($big);

   if($thumbInfo[0] == 170 && $thumbInfo[1] == 120) {
      $type = 1; // if the image is landscape
   } elseif($thumbInfo[0] == 120 && $thumbInfo[1] == 170) {
      $type = 2; // if the image is portrait
   } else {
      $config->FEDieError("Image: {$rows['image_thumb_url']} is not valid");
   }

   if($i % 3) {

      // start a new row
      
      $content .= <<< TR
</tr>
<tr>
TR;

   } else {

      // start a new column
      $content .= <<< TD
      <td class="gallery">
         <a href="#{$rows['image_id']}" name="{$rows['image_id']}" onclick="window.open('$bigImg', '{$rows['image_id']}', 'width={$bigInfo[0]}, height={$bigInfo[1]}')" title="{$caption}">
            <img src="{$thumbImg}" alt="{$caption}" />
         </a>
      </td>
TD;
   }

}

$config->addFEHeader();
print <<< CONTENT

   <h1 class="content_title">Gallery</h1>

   <div style="width:100%;text-align:center;">

      <table cellpadding="5" cellspacing="0" border="0" class="gallery">
         $content
      </table>
      
      <div style="text-align:right;width:100%">{$links}</div>

   </div>

CONTENT;
$config->addFEFooter();
anyone?

-Nay
Sarok
Forum Newbie
Posts: 9
Joined: Fri Jan 02, 2004 12:20 pm
Location: Oslo, Norway

Post by Sarok »

if($i % 3) {

// start a new row

$content .= <<< TR
</tr>
<tr>
TR;

} else {
Dont know if its intentional or if it works or anything.. But the If statement seems to be wrong. % is modulus, as in remainder of $a divided by $b.
Try changing to if($i == 3) {
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

It has to do with the math involved, I believe.

This is what you're using:

if ($i % 3) {
}

See how this works:

if $i is 0:
0 % 3 = 0 = false

$i = 1:
1 % 3 = 1 = true

$i = 2:
2 % 3 = 2 = false // or maybe true. can't remember.

$i = 3:
3 % 3 = 0 = false


Using:


if ($i + 1 % 3) {
}

Might work better
Last edited by microthick on Fri Jan 02, 2004 12:56 pm, edited 1 time in total.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Code: Select all

if($i % 3) {
should be...

Code: Select all

if(!($i % 3)) {
or...

Code: Select all

if($i % 3 == 0) {
Although, in the first pass $i = 0, an as a result 0 % 3 = 0 so you would end up with blank <tr></tr> tags at the top so perhaps....

Code: Select all

if(($i > 0) && (($i % 3) == 0)) {
Also, I would assume that that particular if else statment should just be an if statment. Otherwise it will either add a new row or stick the image in the table, I would assume you would want to add a new row and stick the image in to the table.
Post Reply