Jump to content

php pagination


adamjblakey

Recommended Posts

I have implemented this but i seem to be having problems. When i go to the next page it changes this statement:

 

select * from sellproperty WHERE buylet='Buy' limit -10, 10

 

to this when i click on e.g. page 2.

 

select * from sellproperty WHERE buylet='Buy' limit 0, 10

 

but it does not like this statement and throws an error because it should be 10, 10 not -10, 10 i assume .

 

Now this is not the only problem i am having. I would have thought that when i go through the pages it would go up like so 10, 10 - 20, 10 - 30, 10 etc but page 1, 2 and 3 all say -10, 10.

 

My first question is how do i remove the - and also why is my code not going up in 10's?

 

Here is the code:

 

 

 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; 
       } 
   } 



// get the pager input values 
    $page = $_GET['page']; 
    $limit = 10; 
    $result = mysql_query("select count(*) FROM sellproperty WHERE $stored");
    $total = mysql_result($result, 0, 0);


    // work out the pager values 
    $pager  = Pager::getPagerData($total, $limit, $page); 
    $offset = $pager->offset; 
    $limit  = $pager->limit; 
    $page   = $pager->page; 



// output paging system (could also do it before we output the page content) 
    if ($page == 1) // this is the first page - there is no previous page 
        echo "Previous"; 
    else            // not the first page, link to the previous page 
        echo "<a href=\"property-search.php?set=$setquery&querypage=" . ($page - 1) . "\">Previous</a>"; 

    for ($i = 1; $i <= $pager->numPages; $i++) { 
        echo " | "; 
        if ($i == $pager->page) 
            echo "Page $i"; 
        else 
            echo "<a href=\"property-search.php?set=$setquery&page=$i\">Page $i</a>"; 
    } 

    if ($page == $pager->numPages) // this is the last page - there is no next page 
        echo " | Next"; 
    else            // not the last page, link to the next page 
        echo "<a href=\"property-search.php?set=$setquery&page=" . ($page + 1) . "\"> | Next</a>";

Link to comment
https://forums.phpfreaks.com/topic/64633-php-pagination/
Share on other sites

That is a different problem they solved.

 

It looks like the problem is here: 

 

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

 

Say i click on page 1 i get -10, 10 but i should be getting 10, 10

And if i click on page 2 i should get 20, 10 not -10, 10 like i am doing at the moment.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/64633-php-pagination/#findComment-322539
Share on other sites

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.