0

I'm new at web page coding, and I'm having problems...

Right now I could addapt a code that lists and echoes images from a directory, displaying them with lightbox.

Right now the code displays ALL images from the directory, in rows of 5 images, but WITHOUT limit...

How could I change my code to page results, lets say for example: Just ONE row, with 5 images per PAGE. Then display the usual "Page: 1, 2, 3 ... Next ..." I really don't know how to do it... I've been trying many things without success...

The code goes as follows:

<?php 

$page = $_SERVER['PHP_SELF'];

// How many images per row
$maxCols = 5;

// Directory where the albums are stored
$base = "albums";

// Get album title
$get_album = $_GET['album'];


if (!$get_album)
{
    $handle = opendir($base);

    echo "<div id='albums' class='imageRow'>";

    while (($file = readdir($handle))!==FALSE)
    {       
        if (is_dir($base."/".$file) && $file != "." && $file != "..")
        {
            //$list []= $file;
            $img = $file.".jpg"; //Main image from each album.

            echo "<div class='image'><a href='$page?album=$file'><img src='thumbnails.php?img=$base/$img' alt='$file' /></a><div class='album'><i>$file</i></div></div>";
        }       
    }

    echo "</div>";

    closedir($handle);

    //$total = count($list);

}
else
{
    if (!is_dir($base."/".$get_album) || strstr($get_album,".")!=NULL || strstr($get_album,"/")!=NULL || strstr($get_album,"\\")!=NULL)
    {
        echo "Album doesn't exist.";
        echo "<p /><a href='$page'>Back to albums</a>";
    }
    else
    {
        $count = 0;
        $handle = opendir($base."/".$get_album);

        echo "<div id='images' class='imageRow'>";

        while (($file = readdir($handle)) !== FALSE)
        {
            if ($file != "." && $file !== "..")
            {
                echo "<div class='image'><a href='$base/$get_album/$file' rel='lightbox[1]' title='$file'><img src='thumbnails.php?img=$base/$get_album/$file' alt='$file' /></a></div>";

                $count++;

                if($count == $maxCols) 
                {
                    echo "</div>";
                    echo "<div id='images' class='imageRow'>";
                    $count = 0;
                }

                $list[] = $file; //Assign the images to a list to allow count.
            }
        }

        echo "</div>";

        closedir($handle);

        $total = count($list); //Total elements at the album.

        //echo "<a href='$page'>Back to albums</a>";
    }
}

?>

Any help will be much appreciated!

Many thanks in advance!!

qalbiol
  • 475
  • 7
  • 21
  • Try this http://www.codehive.net/PHP-Array-Pagination-10.html – Cups Aug 13 '12 at 17:17
  • Thanks for answering, I can't seem to use that code for my purpose, I can make the pagination with this, but still gets showing every file inside my dir. – qalbiol Aug 13 '12 at 19:34

3 Answers3

0

The general approach would be to count the total number of available files (which you are currently doing). Then decide how may files per page you are going to show (in your case 5). Divide the count by that value to show per page to determine how many pages are needed (i.e. what "1,2,3, next" output should be). Then have each link for 1,2,3, next link back to the same page with a parameter passed. So for example the link for page 2 might be /yourscript.php?page=2

You would then use the page offset to know where within your file array to being output. So from the example if page = 2, you would start you count on array offset 5 (because files 0-4 are on the first page).

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • Hi, thanks for answering, I know the idea of paging, in fact I have coded a paging system that gets MySQL registers, and works perfectly. The problem right now is that I am getting results by scanning a directory, and outputting every file inside that dir. I'm trying to figure how to addapt the paging system I have, but I can't seem to achieve it – qalbiol Aug 13 '12 at 17:47
0

Try this:--

Output

enter image description here

<?php

$maindir = "img" ;
$mydir = opendir($maindir) ;
$limit = 5;
$offset = ((int)$_GET['offset']) ? $_GET['offset'] : 0; 
$files = array();
$page='';
$exclude = array( ".", "..", "index.php",".htaccess","guarantee.gif") ;
while($fn = readdir($mydir))
{
    if (!in_array($fn, $exclude)) 
    {
        $files[] = $fn;;
    }
}
closedir($mydir);
sort($files);
$newICounter = (($offset + $limit) <= sizeof($files)) ? ($offset + $limit) : sizeof($files);

for($i=$offset;$i<$newICounter;$i++) {  
?>
    <a href="<?php print $files[$i]; ?>"><?php print $files[$i]; ?></a><br>
<?php
}
freddyShowNav($offset,$limit,sizeof($files),"");

function freddyShowNav($offset, $limit, $totalnum, $query) {
    global $PHP_SELF;
    if ($totalnum > $limit) {
            // calculate number of pages needing links 
            $pages = intval($totalnum/$limit);

            // $pages now contains int of pages needed unless there is a remainder from division 
            if ($totalnum%$limit) $pages++;

            if (($offset + $limit) > $totalnum) {
                $lastnum = $totalnum;
                }
            else {
                $lastnum = ($offset + $limit);
                }
            ?>
                <table cellpadding="4"><tr><td>Page </td>
            <?php
            for ($i=1; $i <= $pages; $i++) {  // loop thru 
                $newoffset=$limit*($i-1);
                if ($newoffset != $offset) {
            ?>
                    <td>
                        <a href="<?php print  $PHP_SELF; ?>?offset=<?php print $newoffset; ?><?php print $query; ?>"><?php print $i; ?>
                        </a>
                    </td>
            <?php
                    }     
                else {
            ?>
                    <td><?php print $i; ?></td>
            <?php
                    }
                }
            ?>
                    </tr></table>
            <?php
        }
    return;
    }



echo $page;
?>
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • Thanks, but too long and complex to adapt to my code plus it does something different that what I need. ;) – qalbiol Aug 13 '12 at 18:02
0

DirectoryIterator and LimitIterator are my new best friends, although glob seems to prefilter more easily. You could also write a custom FilterIterator. Needs PHP > 5.1, I think.

No prefilter:

$dir_iterator = new DirectoryIterator($dir);
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage);

Glob prefilter:

$dir_glob = $dir . '/*.{jpg,gif,png}';

$dir_iterator = new ArrayObject(glob($dir_glob, GLOB_BRACE));
$dir_iterator = $dir_iterator->getIterator();
$paginated = new LimitIterator($dir_iterator, $page * $perpage, $perpage);

Then, do your thing:

foreach ($paginated as $file) { ... }

Note that in the case of the DirectoryIterator example, $file will be an instance of SplFileInfo, whereas glob example is just the disk path.

drzaus
  • 24,171
  • 16
  • 142
  • 201