SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    search results pagination problem !

    Hi

    I have problem with results pagination. When search is applied browser is showing me first 10 results, and links to other pages, but there is a problem with these links (passing variables i think). When i press on one of the links browser is showing me info that search input must be longer then 2 char. (so it clear that search input has not been passed thru the link.)

    Here is the code:
    Code PHP:
    else if(isset($_POST['submit'])||isset($_POST['search'])||isset($_GET['search'])) // when search button is pressed 
    { 
     
    $PHP_SELF = $_SERVER['HTTP_REFERER']; 
            $search=$_POST["search"]; 
     
            $minchar = 2; 
            if (strlen($search) < $minchar) 
            { 
                    echo    "<span class='info1'>Search must be longer than 2 characters.</span> 
     
            } 
     
            else 
                    {        
                    if(!isset($_GET['page'])){ 
            $page = 1; 
                    } else { 
            $page = $_GET['page']; 
                    } 
                    $max_results = 10; 
                    $from = (($page * $max_results) - $max_results); 
     
                    $result = mysql_query("SELECT * FROM movies WHERE name LIKE '%$search%' 
                OR cast LIKE '%$search%' OR director LIKE '%$search%'  OR year LIKE '%$search%' OR production LIKE '%$search%' OR genere LIKE '%$search%' OR language LIKE '%$search%' ORDER BY name LIMIT $from, $max_results"); 
     
                $numrows=mysql_num_rows($result); 
                if ($numrows == 0) 
                { 
                            echo "<p>Sorry, your search returned 0 results</p> 
                 } 
                else 
                { 
                    while($row=mysql_fetch_array($result)) 
                    {        
                    include "display_rec.php"; 
                    } 
     
    $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM movies WHERE name LIKE '%$search%' OR cast LIKE '%$search%' OR director LIKE '%$search%'  OR year LIKE '%$search%' OR production LIKE '%$search%' OR genere LIKE '%$search%' OR language LIKE '%$search%' ORDER BY name"),0); 
     
    // Figure out the total number of pages. 
    $total_pages = ceil($total_results / $max_results); 
     
     
    // Build Page Number Hyperlinks 
     
    // Build Previous Link 
    if($page > 1){ 
        $prev = ($page - 1); 
        echo " 
            <td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$prev\">Previous</a></td> 
            <td width='2'></td> 
     "; 
    } 
     
    for($i = 1; $i <= $total_pages; $i++){ 
        if(($page) == $i){ 
            echo "<td class='nav'>$i</td> 
                    <td width='2'></td>"; 
            } else {                        
                echo "<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$i\">$i</a></td> 
                            <td width='2'></td>"; 
        } 
    } 
     
    // Build Next Link 
    if($page < $total_pages){ 
        $next = ($page + 1);                
        echo "<td class='nav'><a href=\"".$_SERVER['PHP_SELF']."?search=$search&page=$next\">Next</a><td>"; 
    } 
                    } 
    } 
     
    }

    go to http://www.webdev.nita-on-line.com/new/movies.php
    to see this script in action - (search words that will give you more than 10 results - james, red - for example)

    I need some help with this one ....

    Thank you in advance.

    Nita

  2. #2
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to pass the search word in links. To not having to mess up with $_POST first and $_GET for the next result, use method="get" in your search form.


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
  •