Jump to content

I Need PHP Paging Help


ethan89

Recommended Posts

I see several great PHP/MySQL paging systems on google, but how can I make a paging system when I'm just showing all images in a directory, not showing all images from a database?  I have no idea how I could convert one of those mysql systems to work for my needs.

 

Here is my code:

 

<?php

$columns = "4";

$count = "0";

echo '<div style="overflow:auto; width:500px; height:266px;"><table cellpadding="0" cellspacing="10">';

if ($handle = opendir('.')) {

while (false !== ($image = readdir($handle))) {

if ($image != "." && $image != ".." && $image != "index.php") {

if (strlen($image) > 11)

{

$short = substr($image,0,11);

echo '<td align="center"><a href="'.$image.'" title="'.$image.'"><img src="'.$image.'" width="100" height="100"><br><div style="overflow:auto; width:100px;">'.$short.'...</div></a></td>';

}

else

{

echo '<td align="center"><a href="'.$image.'" title="'.$image.'"><img src="'.$image.'" width="100" height="100"><br><div style="overflow:auto; width:100px;">'.$image.'</div></a></td>';

}

$count++;

if ($count == 4)

{

echo "</tr><tr>";

$count = 0;

}

}

}

closedir($handle);

}

echo "</table></div>";

?>

 

This code shows all images in a directory, 4 per row.  I'd really appreciate some help with adding a paging feature to this.  Thank you so much for the help!

Link to comment
Share on other sites

The best way I know to do this is to use num_rows to calculate your items in a database then do a simple math to calculate how many to display on each page, you can see by the code ive written for you below, you will need to add to it to make it work like the starting number and amount to limit your resualts by befor you have created a query with a dynamic URL (?page=number).

 

//state how many to display on each page
$per_page = 4; 

//calculating no of pages
$pageresult = mysql_query("SELECT * FROM your_table");
$count = mysql_num_rows($pageresult);
$pages = ceil($count/$per_page);

// display items for this page
$start = ($page-1)*$per_page;
$result = mysql_query("SELECT * FROM your_table LIMIT $start,$per_page");

 

Then using CSS to create your page numbers you could try doing somthing with jQuery to funck it up

 

<ul id="pagination">
            <?
            //pagination Numbers
            for($i=1; $i<=$pages; $i++)
            {?>
            <li><a href="your-page.php?page=<? echo $i;?>"><? echo $i;?></a></li>
            <? }?>
      </ul>

 

Hope this helps!

 

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.