# Limit paging in search results

**URL:** <https://forum.kirupa.com/t/limit-paging-in-search-results/28760>\
**Category:** Uncategorized\
**Created:** [August 18, 2003, 6:18am UTC](https://forum.kirupa.com/t/limit-paging-in-search-results/28760 "2003-08-18T06:18:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![OniHime](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@OniHime](https://forum.kirupa.com/u/OniHime)\
**Post date:** [August 18, 2003, 6:18am UTC](https://forum.kirupa.com/t/limit-paging-in-search-results/28760/1 "2003-08-18T06:18:44Z")

</div>

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 ☹

Note: $x is a string of values;

Here’s the script:

```php

$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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 18, 2003, 2:36pm UTC](https://forum.kirupa.com/t/limit-paging-in-search-results/28760/2 "2003-08-18T14:36:56Z")

</div>

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.

🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 18, 2003, 3:22pm UTC](https://forum.kirupa.com/t/limit-paging-in-search-results/28760/3 "2003-08-18T15:22:44Z")

</div>

Hello Jubba,

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

```php

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… 🙂 Any suggestions??

Hope this explains 🙂

Many Thanks

OniHime

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 18, 2003, 3:32pm UTC](https://forum.kirupa.com/t/limit-paging-in-search-results/28760/4 "2003-08-18T15:32:47Z")

</div>

yeah you have to change that if statement:

```php

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:

```php

$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… 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 23, 2003, 6:54am UTC](https://forum.kirupa.com/t/limit-paging-in-search-results/28760/5 "2003-08-23T06:54:49Z")

</div>

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 🙂

OniHime
