Jump to content

pagination php without mysql (or any other database)


Baldry

Recommended Posts

Hi

 

I am pretty new at PhP so its pretty hard for me to writh a code but i am trying my best.

i have a folder with 200+ images and i would like to show 10 at a time for 1 page and so on

 

everything i got is

  $iPicturesPerSite = 10;

    $output = '';
    $files = glob('../images/'.$sSearchFor.'*.{jpg,png,gif}',GLOB_BRACE);

    $iSite = 1;
    if(isset($_GET['iSite']) && is_numeric($_GET['iSite']) && $_GET['iSite'] > 0){
        $iSite = $_GET['iSite'];
    }
  $iMaxBildNummer = $iPicturesPerSite * $iSite;
    if($iMaxBildNummer > sizeof($files)){
        $iMaxBildNummer = sizeof($files);
    }
    for($i=(($iPicturesPerSite*$iSite)-$iPicturesPerSite); $i<$iMaxBildNummer; $i++){
        $size = getimagesize($files[$i]);
        $output = $output.$this->_simpleTpl("tpl/item.tpl",array(
                'showName'=> basename($files[$i]),
                'imagewidth' => $this->width,
                'fileName'=>'images/'.basename($files[$i]),
                'resolution'=>$size[0]."x".$size[1],
                'fileSize' => round(filesize($files[$i])/1024),
                'link' => '.link to file='.$files[$i]));
  }
    return $output;
}

it shows the first 10 images already but i dont knopw how to do more. i cant get past the "put 10 in the first page than 10 in the next and so on" i tried

 $iMaxPageNummer = $iMaxBildNummer / $iPicturesPerSite;


           $this->$iMaxPageNummer = ceil($iMaxBildNummer / $iPicturesPerSite);

           for($p = 1; $p <= $iSite; $p++){
               if($p == $iSite){
                  $tmp_pages[] = "{$p}";
               
               }else{
                  $tmp_pages[] = "<a href='?page={$p}'>{$p}</a>";
                  
               }
               echo "<br />" . implode(" - ", $tmp_pages);
           }

but this only showed a whole lot of 1-1-1-1-1- :D

maybe i am just to new to the php stuff.

some help would be really nice

(this is not the full code i already have a search option for images)

 

Thanks

 

EDIT: ahm most of the variables are german am sry about that but this is how i do it at the moment but you should see what is for what and if not just ask.

Edited by Baldry
Link to comment
Share on other sites

Why not use your glob call to get a list of image filenames and store it in a SESSION variable?  Then display the first x images by getting their names from the session var array?  When done also save the index of the last item displayed as a session var. 

 

On subsequent executions of your script - if the session vars are set, then use them to pull up the next x number of images until there are no more.

Link to comment
Share on other sites

Change to your image folder locations and should see images 10 per page paginated.

Modify to suit your needs or merely look over how it's done and implement to your needs.

 

array_slice() was used

 

 

<html>
<head>
<style type="text/css">
    body {
        margin: auto;
        padding: auto;
        width: 100%;
        color: #000000;
        font: normal 80%/120% Georgia, "Times New Roman", Times, serif;
        position: relative;
    }
    a
    {
        color: #000000;
    }

    .gallery {
        list-style: none;
        margin: 0;
        padding: 0;
        right: -100px;
    }
    .gallery li {
        margin: 5px;
        padding: 0;
        float: left;
        height: 100px;


    }
    .gallery img {
        background: #fff;
        border: solid 1px #ccc;
        padding: 4px;
        width: 100px;
        right: -100px;


    }
    .gallery span {
        width: 100px;
        height: 100px;
        display: block;
        position: absolute;
        top: 50%;
    }
    .gallery a:hover img {    
    border: 1px solid #0000ff;
    width:500px;
    overflow: -100px auto;
    position: relative;
    z-index: 10;
    top: 0%;
    left: 0;    
    }
    </style>
</head>
<body>
<div class="gallery">

<?php
function is_dir_empty($dir)
{
    if (!is_readable($dir))
        return NULL;
    return (count(scandir($dir)) == 2);
}

function paginate($display, $pg, $total)
{
    
    if (isset($_SERVER['QUERY_STRING']) && trim($_SERVER['QUERY_STRING']) != '') {
        if (stristr($_SERVER['QUERY_STRING'], 'pg='))
            $query_str = '?' . preg_replace('/pg=\d+/', 'pg=', $_SERVER['QUERY_STRING']);
        else
            $query_str = '?' . $_SERVER['QUERY_STRING'] . '&pg=';
    } else
        $query_str = '?pg=';
    
    $pages = ($total <= $display) ? 1 : ceil($total / $display);
    
    $first = '<a href="' . $_SERVER['PHP_SELF'] . $query_str . '1">«
</a>';
    $prev  = '<a href="' . $_SERVER['PHP_SELF'] . $query_str . ($pg - 1) . '">
</a>';
    $next  = '<a href="' . $_SERVER['PHP_SELF'] . $query_str . ($pg + 1) . '">
</a>';
    $last  = '<a href="' . $_SERVER['PHP_SELF'] . $query_str . $pages . '">
»</a>';
    
    
    echo '<div><p align="center">';
    echo ($pg > 1) ? "$first : $prev :" : '« :  :';
    
    $begin = $pg - 4;
    while ($begin < 1)
        $begin++;
    $end = $pg + 4;
    while ($end > $pages)
        $end--;
    for ($i = $begin; $i <= $end; $i++)
        echo ($i == $pg) ? ' [' . $i . '] ' : ' <a href="' . $_SERVER['PHP_SELF'] . $query_str . $i . '">' . $i . '</a> ';
    
    echo ($pg < $pages) ? ": $next : $last" : ':  : »';
    echo '</p></div>';
}

$display = 10; //how many items to display
$pg      = (isset($_REQUEST['pg']) && ctype_digit($_REQUEST['pg'])) ? $_REQUEST['pg'] : 1;
$start   = $display * $pg - $display;



$dir          = 'images/'; //images folder location
$file_display = array(
    'jpg',
    'jpeg',
    'png',
    'gif',
    'bmp'
); //image types to display

if (file_exists($dir) == true) {
    
    if (is_dir_empty($dir)) {
        echo "No Images";
    } else {
        $dir_contents = scandir($dir);
        $total        = count($dir_contents);
        echo "Total count: $total<br />";
        $slice = array_slice($dir_contents, $start, $display);
        paginate($display, $pg, $total);
        
        foreach ($slice as $file) {
            $file_type = strtolower(end(explode('.', $file)));
            
            if (in_array($file_type, $file_display) == true) {
                echo '<li><a href="', $dir, '/', $file, '" target="_blank" ><img src="', $dir, '/', $file, '" alt="', $file, '" /></li>';
            }
        }
    }
} else {
    echo "$dir not found";
}


?>
</div>
</body>

</html>
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.