Page 1 of 1

Database or no database? If no, how to paginate?

Posted: Sun Mar 13, 2011 2:09 pm
by asmith6

Code: Select all

<?php
$image_url = 'images/';
 
//User defined variables for page settings
$rows_per_page = 2;
$cols_per_page = 4;
 
//Master array of ALL the images in the order to be displayed
$images = array(
        'image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg',
        'image5.jpg', 'image6.jpg', 'image7.jpg', 'image8.jpg',
        'image9.jpg', 'image10.jpg', 'image11.jpg', 'image12.jpg',
        'image13.jpg', 'image14.jpg', 'image15.jpg', 'image16.jpg',
        'image17.jpg', 'image18.jpg', 'image19.jpg'
    );

//END USER DEFINED VARIABLES
 
//System defined variable
$records_per_page = $rows_per_page * $cols_per_page;
$total_records = count($images);
$total_pages = ceil($total_records / $records_per_page);
 
//Get/define current page
$current_page = (int) $_GET['page'];
if($current_page<1 || $current_page>$total_pages)
{
    $current_page = 1;
}
 
//Get records for the current page
$page_images = array_splice($images, ($current_page-1)*$records_per_page, $records_per_page);
 
//Create ouput for the records of the current page
$ouput = "<table border=\"1\">\n";
for($row=0; $row<$rows_per_page; $row++)
{
    $ouput .= "<tr>\n";
    for($col=0; $col<$cols_per_page; $col++)
    {
        $imgIdx = ($row * $rows_per_page) + $col;
        $img = (isset($page_images[$imgIdx])) ? "<img src=\"{$image_url}{$page_images[$imgIdx]}\" />" : '&nbsp;';
        $ouput .= "<td>$img</td>\n";
    }
    $ouput .= "</tr>\n";
}
$ouput .= "</table>";
 
//Create pagination links
$first = "First";
$prev  = "Prev";
$next  = "Next";
$last  = "Last";
if($current_page>1)
{
    $prevPage = $current_page - 1;
    $first = "<a href=\"test.php?page=1\">First</a>";
    $prev  = "<a href=\"test.php?page={$prevPage}\">Prev</a>";
}
if($current_page<$total_pages)
{
    $nextPage = $current_page + 1;
    $next = "<a href=\"test.php?page={$nextPage}\">Next</a>";
    $last = "<a href=\"test.php?page={$total_pages}\">Last</a>";
}
 
?>
<html>
<body>
<h2>Here are the records for page <?php echo $current_page; ?></h2>
  <ul>
    <?php echo $ouput; ?>
  </ul>
Page <?php echo $current_page; ?> of <?php echo $total_pages; ?>
<br />
<?php echo "{$first} | {$prev} | {$next} | {$last}"; ?>
</body>
</html>
This code adobe provides an easy way of putting up pictures to my site, whilst having the other pictures move automatically without the need of a database. But I must wonder: Would it be faster to move my pictures to a database? I mean the example above shows I'm only using 19 pictures, but if I ever reach say 1000 pictures, would the php file be too big? Should I just make a database now or it doesn't matter?

Also, for some reason, pagination is not working. Anyone care to do a diagnosis?

Thanks!

Re: Database or no database? If no, how to paginate?

Posted: Sun Mar 13, 2011 5:55 pm
by litebearer
Definitely suggest using a database. If you do, here is an excellent and easy to use pagination class
http://phpsense.com/php/php-pagination-script.html

Re: Database or no database? If no, how to paginate?

Posted: Mon Mar 14, 2011 11:55 am
by social_experiment
Pagination without a database is still possible should you need that option.