Jump to content

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


asmith6

Recommended Posts

<?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]}\" />" : ' ';

        $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 current code provides an easy way of uploading pictures and having the other pictures move automatically without the need of a database. But I must wonder, if doing this on a database would be faster. Would it be? I mean the example adobe 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, the code above, which I took from a phpfreak member, has the pagination not working. Anyone care to do a diagnosis?

 

Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.