Jump to content

[SOLVED] adding pagination to search


kartul

Recommended Posts

ok. i need to add pagination to search but i have no idea how to do it.

i have form at top of the page - work perfectly

i have working search - it selects from mysql and displays results fine

i also have perfectly working pagination - divides results into different pages

 

my problem is i dont know how do build this all up. i mean, how do bind search and pagination.

right now, when i search for something, then it displays first 25 results ( all works so far ). but when i click on next page or choose any page from pagination, it gives me empty page (not blank, just empty, no results taken from db).  altho, it gives me right amount of pages as well. i just dont know what i've made wrong that the other pages dont work. i've tried google for help but nothing that helps me so far.

 

heres my code:

<form action="search.php" method="post">
   <input type="text" name="name" />
   <select name="type">
   <option value="all">All</option>
   <?php
   // there is array for those
   foreach($sort_by_s AS $num => $type) {
     echo "<option value=\"".$num."\">".$type."</option>\n";
   }
   ?>
   </select>
   <select name="encoder">
   <option value="all">All</option>
   <?php
   // there is array for those
   foreach($encoders AS $e) {
     echo "<option value=\"".$e."\">".$e."</option>\n";
   }
   ?>
   </select>
   <input type="submit" name="search" value="Search"/>
</form>
<?php
if(isset($_POST['search'])) {
    //gets user input from form
    $name = mss($_POST['name']);
    $type = mss($_POST['type']);
    $encoder = mss($_POST['encoder']);
    
    //checks if name and type and encoder are selected
    if($name && $type && $encoder) {
        //name must be more than 2 chars
        if(strlen($name) < 2) {
            echo $name . " " . $type . " " . $encoder . "<br />shorts";
        }else {
            // if type isn't selected then make it empty
            if($type == "all") {
                $type = "";
            //otherwise search from db for selected type
            }else {
                $type = " AND `type`='".$type."'";
            }
            
            //if encoder isn't selected make it empty
            if($encoder == "all") {
                $encoder = "";
            //otherwise search from db for selected encoder
            }else {
                $encoder = " AND `encoder`='".$encoder."'";
            }
            
            // search query based on the checking on top
            $searchSql = "SELECT * FROM `mov_index` WHERE `display`='1' AND `title` LIKE '%".$name."%' ".$type.$encoder;
            #echo $sql;
            
            //pagination security
            $page =(!$_GET['page'] || $_GET['page'] < 0) ? "1" : $_GET['page'];
            $page = ceil($page);
            
            $limit = 25;
            $start = $limit;
            $end = $page*$limit-($limit);
            
            if(isset($_GET['page'])) {
                if(!is_int($start) && !is_int($end)) {
                    echo "ERRRORR";
                    die;
                }elseif($start < 0 || $end < 0) {
                    echo "ERRRORR";
                    die;
                }
            }
            //end of pagination security
            
            //selects first 25 results from search query
            $select_res = mysql_query($searchSql." ORDER BY title DESC LIMIT ".$end.",".$start."") or die(mysql_error());
            while($row = mysql_fetch_assoc($select_res)) {
                //display results here
            }
            
            //pagination starts
            //amount check is search query
                $amount_check = $searchSql;
                $amount_check_res = mysql_query($amount_check) or die(mysql_error());
                $amount_count = mysql_num_rows($amount_check_res);
                $pages = ceil($amount_count/$limit);
                $pag = $page - 1;
                
                $pagesystem = "";
                if($pages == 0) {
                    $pagesystem .= "No pages";
                }else {
                    if($pages == 1) {
                        $pagesystem .= "Page 1";
                    }else {
                        if($pag == 0) {
                            $pagesystem .= "« First ";
                        }else {
                            $pagesystem .= "<a href=\"search.php?page=1\">« First</a> ";
                        }
                        if($pages > 1) {
                            if($page > 1) {
                                $pagesystem .= " <a href=\"search.php?page=".($page - 1)."\">Prev.</a> ";
                            }else {
                                $pagesystem .= " Prev. ";
                            }
                            if(($page - 2) >= 1) {
                                if(($page -2) > 1) {
                                    $pagesystem .= "...";
                                }
                                $pagesystem .= " <a href=\"search.php?page=".($page - 2)."\">".($page - 2)."</a> <a href=\"search.php?page=".($page - 1)."\">".($page - 1)."</a> ";
                            }else {
                                if(($page - 1) >= 1) {
                                    $pagesystem .= " <a href=\"search.php?page=".($page - 1)."\">".($page - 1). "</a> ";
                                }
                            }
                        }
                        $pagesystem .= $page . " ";
                        
                        if($pages > 1) {
                            if(($page + 2) <= $pages) {
                                $pagesystem .= " <a href=\"search.php?page=".($page + 1)."\">".($page + 1)."</a> <a href=\"search.php?page=".($page + 2)."\">".($page + 2)."</a> ";
                                if(($page + 2) < $pages) {
                                    $pagesystem .= "...";
                                }
                            }else {
                                if(($page + 1) <= $pages) {
                                    $pagesystem .= " <a href=\"search.php?page=".($page + 1)."\">".($page + 1)."</a> ";
                                }
                            }
                            if($page < $pages) {
                                $pagesystem .= " <a href=\"search.php?page=".($page + 1)."\">Next</a> ";
                            }else {
                                $pagesystem .= " Next ";
                            }
                        }
                        
                        if($page == $pages) {
                            $pagesystem .= " Last »";
                        }else {
                            $pagesystem .= " <a href=\"search.php?page=".($pages)."\">Last »</a> ";
                        }
                    }
                }
            //display pagination
            echo $pagesystem;
        }
    }// end of if($name && $type && $encoder)
}//end of if(isset[post[search]])
?>

Link to comment
Share on other sites

I would gess that you need to try and store the query string or the values you use in the query in a session variable that you can pass to the next page. The pagination query is going to run again, but the data being searched for is not being sent with it, so you're running a query that has no matches.

Link to comment
Share on other sites

I would gess that you need to try and store the query string or the values you use in the query in a session variable that you can pass to the next page. The pagination query is going to run again, but the data being searched for is not being sent with it, so you're running a query that has no matches.

ok, but how? when i assign name, type and encoder before query then doesn't it get's over written on next page cause then the form is empty?

Link to comment
Share on other sites

of course!

ok, I made following changes:

 

// search query based on the checking on top
$searchSql = "SELECT * FROM `mov_index` WHERE `display`='1' AND `title` LIKE '%".$name."%' ".$type.$encoder;
#echo $sql;

TO

// search query based on the checking on top
$searchSql = "SELECT * FROM `mov_index` WHERE `display`='1' AND `title` LIKE '%".$name."%' ".$type.$encoder;
#echo $sql;
$_SESSION['searchSql'] = $searchSql;
//echo $_SESSION['searchSql'];

 

before displaying

//selects first 25 results from search query
$select_res = mysql_query($searchSql." ORDER BY title DESC LIMIT ".$end.",".$start."") or die(mysql_error());

TO

//selects first 25 results from search query
$select_res = mysql_query($_SESSION['searchSql']." ORDER BY title DESC LIMIT ".$end.",".$start."") or die(mysql_error());

 

and in pagination

$amount_check = $searchSql;

TO

$amount_check = $_SESSION['searchSql'];

 

ok, basicly. i've changed all querys to use query that is inside the session but still same. all other pages are empty.

Link to comment
Share on other sites

kartul, have you made your script also use that $_SESSION query when page is changed? Means about this..

 

if (isset($_GET['page']) && isset($_SESSION['searchSql']))
{
    $sql = $_SESSION['searchSql']; // plus any additional info to query that you may have..
}
else
{
   $sql = $your_normal_query_here;
}

// And then get the results and show them.

Link to comment
Share on other sites

there is a tutorial on this site for it:

http://www.phpfreaks.com/tutorial/basic-pagination

 

hope that helps

like I said, I have working pagination and search. I just don't know how do make them work together.

kartul, have you made your script also use that $_SESSION query when page is changed? Means about this..

 

if (isset($_GET['page']) && isset($_SESSION['searchSql']))
{
    $sql = $_SESSION['searchSql']; // plus any additional info to query that you may have..
}
else
{
   $sql = $your_normal_query_here;
}

// And then get the results and show them.

YES! that's what I needed. now everything works perfectly.

 

Thank You guys.!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.