SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post How do I add pagination to my search script

    i have a search script to search a database of books... some results become too long so i thought of adding a pagination but dont understand how to go about it.
    below is my search script can anyone help me on how to add a functional paination?


    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    if (!isset(
    $_GET['Submit'])) { 
    // form not submitted 
    }
    else {

    //Server Variables 
    $host "localhost"
    $user "userName"
    $pass "passWord"
    $db "databaseName";


    $search = empty($_GET['search'])? die ("<font size='5'font color='#FF0E0E'>ERROR: Enter Search Criteria</font>") : mysql_real_escape_string($_GET['search']); 
    $dropdown = empty($_GET['dropdown'])? die ("<font size='5'font color='#FF0E0E'>ERROR: Select from Dropdown</font>") : mysql_real_escape_string($_GET['dropdown']);

    if(
    strlen($search)<=2)
    echo 
    "<font size='5'font color='#FF0E0E'><strong>Search term too short!</strong></font>";
    else{
    echo 
    "<font size='5'font color='#FF0E0E'>You searched for <strong>$search</strong></font></br></br>";

    //Open Connection
    $connect mysql_connect($host$user$pass) or die ("Unable to connect to host");

    //Select Database
    mysql_select_db($db) or die ("Unable to connect to database");

    //Create Query
    $query "SELECT DISTINCT book_id,call_no,main_title,author1,itm_type,publdate,image,subj1 FROM records WHERE $dropdown LIKE '%$search%' order by publdate" or die (mysql_error());

    $result mysql_query($query) or die (mysql_error());

    $num=mysql_num_rows($result);

    require_once
    'paginator_class.php';

    mysql_close($connect);

    if (
    $num==0)
    echo 
    "Sorry, there are no matching results for <b>$search</b>.</br></br>1. 
    Try more general words. </br>2. Please check your spelling"
    ;
    else
    {
    echo 
    "<font size='4'font color='#FF0E0E'><strong>$num</strong> results found!</font><p>";
    }

    $i=0

    while (
    $i $num) {

    $book_id=mysql_result($result,$i,"book_id");
    $image=mysql_result($result,$i,"image"); 
    $main_title=mysql_result($result,$i,"main_title"); 
    $call_no=mysql_result($result,$i,"call_no"); 
    $publdate=mysql_result($result,$i,"publdate"); 
    $author1=mysql_result($result,$i,"author1"); 
    $itm_type=mysql_result($result,$i,"itm_type"); 
    $subj1=mysql_result($result,$i,"subj1"); 


    echo 
    "<table id='results'>";
    echo 
    "
    <tr><td><img src='image1/
    $image' alt='Image not found' onError=this.onerror=null;this.src='image1/default.jpg' height='110px' width='90px'></td><td valign='top'><b><a href=details.php?id=$book_id>
    <b>
    $main_title</b></a></b><br>
    <b>By:</b> 
    $author1<br>
    <b>Call no:</b> 
    $call_no<br>
    <b>Type:</b> 
    $itm_type<br>
    <b>Year:</b> 
    $publdate</tr><p>
    <hr color='#7dd5f5' size='1'></br>
    "
    ;

    $i++;

    }


    ?>

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    455
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Order your query and use a counter to keep track of where you are in the results.

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try ready made class for php pagination,
    https://www.google.com/search?q=php+...hrome&ie=UTF-8
    Quality codes are optimized and tested...
    Click here for inspiration..

  4. #4
    SitePoint Member belansus's Avatar
    Join Date
    Aug 2013
    Location
    Newcastle
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where you have your query string, use LIMIT

    "$query = "SELECT DISTINCT book_id,call_n...." LIMIT 0,10"; -- see if a page is set or not to display the first 10 rows, then you can get the page number from the URL such as (website_url?page=2) "LIMIT ".$_GET['page']-1."0 10"; this will print "LIMIT 10 10" thus getting the next 10 rows.

    Best finding yourself a decent guide on the interweb to help with validation though...


Tags for this Thread

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
  •