Organizing Loop Output Horizontally

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
dflynn
Forum Newbie
Posts: 1
Joined: Tue Jun 17, 2008 6:15 pm

Organizing Loop Output Horizontally

Post by dflynn »

Hi there,
I've been searching google for some time now, and I scanned through the tutorials looking for answers. I can't figure it out.

I have the following code:

Code: Select all

<?php
 
if (isset($_GET['image'])) {
 
    echo "<a href=\"http://www.nnjoi.com/digital/websites.php\" onClick=\"history.go(-1)\">Back to gallery</a><hr />";
    echo "<img src=\"../images/websites/image(" . $_GET['image'] . ").png\" width=\"400\" height=\"300\" alt=\"\"/>";
    
 
} else {
 
    for($n = 1; $n<8; $n++) {
 
        echo "<a href=\"websites.php?image=" . $n . "\"><img src=\"../images/websites/image(" . $n . ").png\" width=\"100\" height=\"75\" alt=\"\"/></a>";
        echo "<br /><br />";
 
    }
 
}
 
?>
 
It's for a simple image gallery which pulls images from a folder. It resizes the images for Thumbnails on one page, when that image is clicked it reformats the page to display the full size image.

The Loop displaying the thumbs only displays them vertically so I will eventually have a column of 100+ images.

Is there a way to get the code to do two things:

1) Sort the images into a table of 4 across at a time,so the first 4 are in the first row, the next 4 are in the the second row etc.

And
2) if it reaches 20 images in length it will add a "next" link to the next page of 20 images?


the second part is not as important right now but i think that the 4 across is needed soon.


Thankyou for your help.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Organizing Loop Output Horizontally

Post by califdon »

1) Use an HTML table. When you loop through the results of your query, just keep outputting another <td> ... </td> until you have 4 of them, then reset your counter, output </tr><tr><td> ... </td> and continue.

2) This is called "pagination". There are dozens and dozens of tutorials and examples in this forum and all over the web. Just google on php pagination.
Post Reply