How to put 1, 2, 3 etc pages on php results..

Discussion in 'PHP' started by adamjblakey, Aug 9, 2007.

  1. #1
    Hi,

    I have a php form which displays results from a database but i want to basically display 15 results then add a number 2 for a next page of results.. How do i go about that?

    This is the code
    
    
    $selects = "SELECT * FROM sellproperty WHERE $stored";
    				$result = mysql_query($selects);		
    					
    				$countrowssa = mysql_num_rows($result);
    		
    				 if ($countrowssa < 1) {
    				
    				print "Sorry.";
    				
    				 }else {
    
    						while ($res = mysql_fetch_array($result)) {
    						$resu[] = $res;	
    					}
    					
    				 mysql_free_result($result);
    				 foreach ($resu AS $row) {
    echo "".$row['house'].""";
    }
    
    
    
    Code (markup):
    Cheers,
    Adam
     
    adamjblakey, Aug 9, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thanks for that nico.

    I have implimented 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
    Code (markup):
    to this when i click on e.g. page 2.

    select * from sellproperty WHERE buylet='Buy' limit 0, 10
    Code (markup):
    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>";  
    
    
    
    Code (markup):

    Any ideas?
     
    adamjblakey, Aug 10, 2007 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Any ideas anyone, i almost have this one cracked.
     
    adamjblakey, Aug 13, 2007 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Anyone else help me?

    It looks like it is down to this:

    offset = ($page - 1) * $limit; 
    Code (markup):
     
    adamjblakey, Aug 14, 2007 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    There must be someone who can help with this?
     
    adamjblakey, Aug 15, 2007 IP