Limit paging in search results

Hello,

I have been struggling in making this script work but no luck… I searched many PHP forums, googled as well… but didnt find what I need T_T

Simply is, I need to limit my search results like this 1-10 then 11-20 … whenever the user click on 1-10, the pages links 1,2,3 etc will appear at the buttom… now my sctipt outputs the 1,2,3 pages …and I have MANY results… limiting them into sets will be proper :frowning:

Note: $x is a string of values;

Here’s the script:


$page = $_GET['page'];  
$limit = 10;  
$result = mysql_query("select count(*) from $table $x;");  
$total = mysql_result($result, 0, 0);  

$pager  = Pager::getPagerData($total, $limit, $page);  
$offset = $pager->offset;  
$limit  = $pager->limit;  
$page   = $pager->page; 
   
$sql = mysql_query("SELECT * FROM $table $x LIMIT $offset, $limit");

 $number1 = mysql_numrows($sql);
  if ($number1 == 0){ echo "<font face=verdana size=1 color=red><br><center>No Matchs Found";
  exit(); }
  else {
  
echo "<font face=verdana size=1><b><u>Search Results:</u></b><br><br>";


  while($row=mysql_fetch_array($sql)) {
            echo "Date:</b>".$row['date'];
            echo "<br>";
            echo "<b>Report #: </b>".$row['report'];
            echo "<br>";
            echo "<b>Name: </b>".$row['name'];
            echo "<br>";
                      }
}
  
echo "<br>";

if ($page == 1)
        echo "Previous ";  
    else         
        echo "<a href=\"$PHP_SELF?page=" . ($page - 1) .\"> Previous </a>";  

    for ($i = 1; $i <= $pager->numPages; $i++) {  
        echo " | ";  
        if ($i == $pager->page)  
           echo "Page $i";
			
        else  
            echo "<a href=\"$PHP_SELF?page=$i\"> Page $i </a>";  
}
    if ($page == $pager->numPages) 
        echo " | Next ";
		  
   else            
       echo " | <a href=\"$PHP_SELF?page=" . ($page + 1) . "\"> Next </a>"; 
    

//I got this Class from a paging tutorial >_>
class Pager  
   {  
       function getPagerData($numHits, $limit, $page)  
       {  
           $numHits  = (int) $numHits;  
           $limit    = max((int) $limit, 1);  
           $page     = (int) $page;  
           $numPages = ceil($numHits / $limit);  

           $page = max($page, 1);  
           $page = min($page, $numPages);  

           $offset = ($page - 1) * $limit;  

           $ret = new stdClass;  

           $ret->offset         = $offset;  
           $ret->limit           = $limit;  
           $ret->numPages = $numPages;  
           $ret->page          = $page;  

           return $ret;  
       }  
   } 


Thank you so much for your help T_T

OniHime

well, what is this script doing for you? Is it not working at all? are you getting some output, but not the right output? More information please.

:slight_smile:

Hello Jubba,

This script works really fine… It prints the matching results over pages… Like 10 matchs per page… And where it says



if ($page == 1)
      if ($page == 1)
        echo "Previous ";  
    else         
        echo "<a href=\"$PHP_SELF?page=" . ($page - 1) ."> Previous </a>";  

    for ($i = 1; $i <= $pager->numPages; $i++) {  
        echo " | ";  
        if ($i == $pager->page)  
           echo "Page $i";
            
        else  
            echo "<a href=\"$PHP_SELF?page=$i\"> Page $i </a>";  
}
    if ($page == $pager->numPages)  echo " | Next ";
          
   else            
       echo " | <a href=\"$PHP_SELF?page=" . ($page + 1) . "\">Next </a>";


it starts printing page numbers with Next and Previous in this form “Previous | Page 1 | Page 2 | Page 3 | Next” …Sometimes I have many matchs, since I have like over 300 items in the database; the script will print out many page numbers and this looks really ugly ~_~… Was thinking to make Page sets or anything that minimize this mess… like Kirupa’s forum paging is sweet… :slight_smile: Any suggestions??

Hope this explains :slight_smile:

Many Thanks

OniHime

yeah you have to change that if statement:


for ($i = 1; $i <= $pager->numPages; $i++) {  
        echo " | ";  
        if ($i == $pager->page)  
           echo "Page $i";
            
        else  
            echo "<a href=\"$PHP_SELF?page=$i\"> Page $i </a>";  
}

should be something like this:


$num_of_page_links = 3 + ($pager->page);

for ($i = $pager->page; $i <= $num_of_page_links; $i++) {  
        echo " | ";  
        if ($i == $pager->page) { 
           echo "Page $i";
        }
        else  {
            echo "<a href=\"$PHP_SELF?page=$i\"> Page </a>";  
        }
}

something like that might work, its not tested but if you understand PHP fairly well you should be able to see what I’m trying to do… :slight_smile:

wow~ thumbs up

Thank you soo much Jubba! though my reply is late ^_^;; but your suggestion worked perfectly!!! :flower: now my days of suffering is gone =D

Thanks :slight_smile:

OniHime