Results 1 to 5 of 5

Thread: Making a page with limited results

  1. #1
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default Making a page with limited results

    Hi, just as many search engines will say "Showing 1 - 10 results of x", I'm trying to implement this on one of my pages but am stumped as to how I could go around it.

    I was thinking by using the page number they're on and then putting that into the query like if they're on page 2, "SELECT * FROM appointments LIMIT $pagenum, 10";

    $pagenum would already have 9 added to it so it would basically get that page number, +9 to it and then look in the database for it.

    page = 2 (page = 11) LIMIT 11, 10 - Show 10 records starting with row #11.

    I'm just trying to figure out how to figure out how many pages will need to be generated and how to code it all.

    Here's what I have:

    PHP Code:
            if (mysql_num_rows($query) > 10)
            {
                echo 
    "<div class=\"pagesel\">";
                
    $i 1;
                
    $page 2;
                echo 
    "Page: ";
                while (
    $i mysql_num_rows($query))
                {
                    echo 
    "<a href=\"" $_SERVER['PHP_SELF'] . "?page=" $page ."\">" $page "</a> ";
                    
    $i+= 10;
                    
    $page++;
                }
                echo 
    "</div>";
            } 
    This code works fine but it's the part where I have to only show the first 1 - 10 results and have the code above work also. If I use LIMIT 10 then the mysql_num_rows evaluates to 10, meaning that there are no more pages to put.

    I just sort of got lost around here, anyone who can help?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Hi,

    I'm looking at something I previously worked on where it counted the total number of results in the database and created the pagination for it. This particular code didn't give the 1 to 10 out of 25 records but I have something else that might help you.

    As far as getting the total number of rows I used...
    PHP Code:
    // Figure out the total number of results in DB:
    $total_results mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tbl_name"),0); 
    The rest of it is building the pagination links and it displays the total number of pages.

    This I think is more what you're after...look down towards the bottom at the $a variable (most of the other stuff you may not need, but I posted so you could see it all together)
    PHP Code:
    // Build SQL Query  
    $query "select * from your_table_name where..." //tailor to fit your needs 

     
    $numresults=mysql_query($query);
     
    $numrows=mysql_num_rows($numresults);

    // $trimmed is just a variable to trim the whitespace from my search query like   $var = @$_GET['q'] ;  $trimmed = trim($var);
    if ($numrows == 0)
      { 
      echo 
    "Results<br>";
      echo 
    "<p>Your search for: &quot;" $trimmed "&quot; returned zero results</p>";
      }  

    // next determine if s has been passed to script, if not use 0
      
    if (empty($s)) {
      
    $s=0;
      }

    // get results
      
    $query .= " limit $s,$limit";
      
    $result mysql_query($query) or die("Couldn't execute query");
      
      
    $query2 .= " limit $s,$limit";
      
    $result2 mysql_query($query2) or die("Couldn't execute query");

    // display what the person searched for
    echo "<p>You searched for: &quot;" $trimmed "&quot;</p>";

    // begin to show results set
    echo "Results<br>";
    $count $s ;

    // now you can display the results returned
      
    while ($rowmysql_fetch_array($result)) {
      
    $id $row["id"];
      
    $item1 $row["item1"];
      
    $item2 $row["item2"];
        
            
      echo 
    "Display results here - use variables if you want to." ;

              
    //  THIS IS THE PART YOU WILL NEED 
      
    $count++ ;
      }

    $currPage = (($s/$limit) + 1);

    //break before paging
      
    echo "<br />";

      
    // next we need to do the links to other results
      
    if ($s>=1) { // bypass PREV link if s is 0
      
    $prevs=($s-$limit);
      print 
    "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
      Prev 10</a>&nbsp&nbsp;"
    ;
      }

    // calculate number of pages needing links
      
    $pages=intval($numrows/$limit);

    // $pages now contains int of pages needed unless there is a remainder from division

      
    if ($numrows%$limit) {
      
    // has remainder so add one page
      
    $pages++;
      }

    // check to see if last page
      
    if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

      
    // not last page so give NEXT link
      
    $news=$s+$limit;

      echo 
    "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
      }

    $a $s + ($limit) ;
      if (
    $a $numrows) { $a $numrows ; }
      
    $b $s ;
    echo 
    "<p>Showing results $b to $a of $numrows</p>"
    That may give you a little direction to go in. Hope it helps.

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Just what I was looking for

  5. #5
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Which is essentially what I posted just in another format...(used for search results)
    PHP Code:
    echo "<p>Showing results $b to $a of $numrows</p>"
    first part of code posted...same idea as paging tutorial...uses COUNT ceil and all
    PHP Code:
    // Figure out the total number of results in DB:
    $total_results mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tbl_name"),0); 

    // Figure out the total number of pages. Always round up using ceil()
    $total_pages ceil($total_results $max_results); 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •