Jump to content

Little PHP paging help


pankirk

Recommended Posts

So I wrote part of this code, and found other parts of it on the net. Basically it's a PHP paging script and it lets me break up my queries into multiple pages. Basically right now all it does is shows the pages at the bottom, and shows a number for every single page it can show. Example:

 

2qau71x.png

 

So, basically what I want to do is be able to maybe tone it down a little by maybe only showing only 4 numbers before the current number and 4 after it? And have some sort of "..." and then show the final page number? Anyway I just want to clean this code up a bit. Can anyone help? The ideal result will be something like digg.com's (scroll to the bottom of the page). you can see how they have a "..." and then show a button for the last page...

 

Something like this (say i'm on page 45)

 

[previous] [1] ... 41 42 43 44 [45] 46 47 48 49 ... [143] [next]

 

 

Do you understand? If not then please ask me questions. I'm not sure how to get this rolling! Thank you so much in advance I appreciate it!

 

 

		$max = 10;
		$num = $page * $max - $max;
		$totalpage = ceil($lines / $max) + 1;
                        $page = 1;

		if ($page > 1) 
		{
     			   echo "<a href=\"?action=index&p=".($page-1)."\"><span id=\"paging\">Previous</span></a> \n";
    			}
		else if($page <= 1)
		{
		   echo "<span id=\"paging\">Previous</span> \n";
		}

    			for($i = 1; $i < $totalpage; $i++) 
		{
        			if($i == $page) 
			{
           				echo "<span id=\"paging\">$i</span> \n";
       				}
			else 
			{
            				echo "<a href=\"?action=index&p=".$i."\"><span id=\"paging\">$i</span></a> \n";
        			}
    			}

    			if ($page < $totalpage - 1) 
		{
        			echo "<a href=\"?action=index&p=".($page + 1)."\"><span id=\"paging\">Next</span></a>\n";
    			}
		else if($page >= $totalpage - 1)
		{
			echo "<span id=\"paging\">Next</span>\n";
		}	

Link to comment
https://forums.phpfreaks.com/topic/142736-little-php-paging-help/
Share on other sites

         $max = 10;
         $num = $page * $max - $max;
         $totalpage = ceil($lines / $max) + 1;
                       // $page = 1;
$show = 9;
$start = (int) max(1, min($page - floor(($show - 1)/2), $totalpage - $show + 1));
$end = min($totalpage, $start + $show - 1);


         if ($page > 1)
         {
                 echo "<a href=\"?action=index&p=".($page-1)."\"><span id=\"paging\">Previous</span></a> \n";
             }
         else if($page <= 1)
         {
            echo "<span id=\"paging\">Previous</span> \n";
         }
             for($i = $start; $i <= $end; $i++)
         {
                 if($i == $page)
            {
                       echo "<span id=\"paging\">$i</span> \n";
                   }
            else
            {
                        echo "<a href=\"?action=index&p=".$i."\"><span id=\"paging\">$i</span></a> \n";
                 }
             }

             if ($page < $totalpage - 1)
         {
                 echo "<a href=\"?action=index&p=".($page + 1)."\"><span id=\"paging\">Next</span></a>\n";
             }
         else if($page >= $totalpage - 1)
         {
            echo "<span id=\"paging\">Next</span>\n";
         }   

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.