Jump to content


Photo

Displaying Results in a table


  • Please log in to reply
5 replies to this topic

#1 mhnewmedia

mhnewmedia
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 February 2006 - 10:22 AM

I have written a simple script that acts as a very basic image gallery that basically just reads what files are contained within a directory and displays them as clickable thumbnails which when rolled over display a full size image.

      <?php


while($file = readdir($dh))
{
if($file != "." && $file != "..")
{
$FileName = $file;
$NoJPG = str_replace(".jpg","",$FileName);
$NoUS = str_replace("_"," ",$NoJPG);

print "<td><a href=\"javascript:;\" onmouseover=\"MM_swapImage('MainImage','','gallery/$file',1)\"><img src=\"gallery/thumbs/$file\" border=\"0\" alt=\"$NoUS\"></a></td>";
$sep = ",\n";
}
}
closedir($dh);
?>

At the moment the images are dispolyed in table cells that spread across the page horizontally, what i would like to be able to do is display the results in a four column table with as many rows as is needed.

Any ideas??

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 28 February 2006 - 04:21 PM

you might need to tweek this a little, but it creats a new row after each forth picture...

      <?php
$i=1;
while($file = readdir($dh))
{
if($file != "." && $file != "..")
{
$FileName = $file;
$NoJPG = str_replace(".jpg","",$FileName);
$NoUS = str_replace("_"," ",$NoJPG);
if ($i==1){print "<tr>";}
print "<td><a href=\"javascript:;\" onmouseover=\"MM_swapImage('MainImage','','gallery/$file',1)\"><img src=\"gallery/thumbs/$file\" border=\"0\" alt=\"$NoUS\"></a></td>";
if ($i==4){print "<\tr>";
     $i=0;
     }
$i++;
$sep = ",\n";
}
}
closedir($dh);
?>



#3 mhnewmedia

mhnewmedia
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 01 March 2006 - 01:05 PM

That Works Like a dream, slight typo on the closing table row tag
if ($i==4){print "<\tr>"
should be a forward slash
if ($i==4){print "</tr>";

The only other issue is if the number of images is not divisible by 4 then the last table row does not have a closing </tr> tag, I was wondering if there is a way of checking if the number of files is divisible by four and if it isn't then printing a final </tr>



#4 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 01 March 2006 - 01:29 PM

Yes, the way I do that is a series of clauses at the end.

When you end the loop, do something like this: (this is concept, not hard code)
if ($i==1){echo "<td></td><td></td><td></td></tr></table>}
if ($i==2){echo "<td></td><td></td></tr></table>}
if ($i==3){echo "<td></td></tr></table>}
if ($i==4){echo "</tr></table>}
You get the idea...

Let me know how it works out!

#5 mhnewmedia

mhnewmedia
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 01 March 2006 - 01:46 PM

Same result but different method I have set two variables $rowstart and $rowend then when a row is started $rowstart is increased by 1 and when a row is ended $rowend is increased by one then at the end if $rowstart isn't equal to $rowend we print a </tr>

I love PHP but sometimes you need a little poke in the right direction!!

<?php
$i=1;
$rowstart = 0;
$rowend = 0;
while($file = readdir($dh))
{
if($file != "." && $file != "..")
{
$FileName = $file;
$NoJPG = str_replace(".jpg","",$FileName);
$NoUS = str_replace("_"," ",$NoJPG);

if ($i==1){echo "<tr>";
    $rowstart++;
}

print "<td><a href=\"javascript:;\" onmouseover=\"MM_swapImage('MainImage','','gallery/$file',1)\"><img src=\"gallery/thumbs/$file\" border=\"0\" alt=\"$NoUS\"></a></td>";
if ($i==5){echo "</tr>";
     $i=0;
     $rowend++;
     }
 
$i++;

$sep = ",\n";
}
}

if ($rowstart != $rowend){echo "</tr>";
}
closedir($dh);
?>


#6 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 02 March 2006 - 10:10 PM

With PHP, there are literally a hundred way to skin a cat... You are limited only by your imagination... I LOVE PHP!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users