Jump to content

Displaying Results in a table


mhnewmedia

Recommended Posts

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.

[code]      <?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);
?>
[/code]

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??
Link to comment
Share on other sites

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

[code]      <?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);
?>
[/code]

Link to comment
Share on other sites

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

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>

Link to comment
Share on other sites

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)
[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>}
[/code]
You get the idea...

Let me know how it works out!
Link to comment
Share on other sites

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!!

[code]<?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);
?>[/code]
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.