Question

im trying to implement a search for a project I'm currently working on. problem that im facing is when i go to the 2nd page seach result will be wrong.

here is the search form that is in my header.php

<form name="search" id="search" method="post" action="search.php">
<input type="text" tabindex="1" class="input" id="term" name="term" value=""/>
<input type="submit" tabindex="2" id="submit" value="SEARCH" />

Here is my search page php code

<?php include ('header.php'); 

$term = $_POST['term'];

    $queryn = mysql_query("select * from scripts where title like '%$term%' or discription like '%$term%' and approved='1'") or die (mysql_error());
    $search_num_rows = mysql_num_rows($queryn);
    ?>  
    <div class="cat_script_container">
    <div class="nresults">Found <?php echo $search_num_rows;?> results for "<?php echo $term;?>"</div>
    <?php   
    // How many adjacent pages should be shown on each side?
    $adjacents = 3;

    /* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    //$query = "SELECT COUNT(*) as num FROM $tbl_name";
    $query = "select COUNT(*) as num from scripts where title like '%$term%' or discription like '%$term%' and approved='1' order by script_id  desc";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages['num'];

    /* Setup vars for query. */
    //$targetpage = "category-$cr[cname]-$cid.html";    //your file name  (the name of this file)
    $catn = strtolower($cr['cname']);   //Turn category name in to lowercase
    $catn = preg_replace("![^a-z0-9]+!i", "-", $catn);
    $limit = 2;                                 //how many items to show per page
    $page = $_GET['page'];

    if($page) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else
        $start = 0;                             //if no page var is given, set start to 0
    /* Get data. */
    $sql = "select * from scripts where title like '%$term%' or discription like '%$term%' and approved='1' order by script_id  desc LIMIT $start, $limit";

    $result = mysql_query($sql);

    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                      //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"search-$prev.html\">« previous</a>";
        else
            $pagination.= "<span class=\"disabled\">« previous</span>"; 

        //pages 
        if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"search-$counter.html\">$counter</a>";                  
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"search-$counter.html\">$counter</a>";                  
                }
                $pagination.= "...";
                $pagination.= "<a href=\"search-$lpm1.html\">$lpm1</a>";
                $pagination.= "<a href=\"search-$lastpage.html\">$lastpage</a>";        
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"search-1.html\">1</a>";
                $pagination.= "<a href=\"search-2.html\">2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"search-$counter.html\">$counter</a>";                  
                }
                $pagination.= "...";
                $pagination.= "<a href=\"search-$lpm1.html\">$lpm1</a>";
                $pagination.= "<a href=\"search-$lastpage.html\">$lastpage</a>";        
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"search-1.html\">1</a>";
                $pagination.= "<a href=\"search-2.html\">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"search-$counter.html\">$counter</a>";                  
                }
            }
        }

        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href=\"search-$next.html\">next »</a>";
        else
            $pagination.= "<span class=\"disabled\">next »</span>";
        $pagination.= "</div>\n";       
    }

    $q=mysql_query("select * from scripts where title like '%$term%' or discription like '%$term%' and approved='1' order by script_id  desc limit $start,$limit");
    $numr=mysql_num_rows($q);
    if ($numr==0)
    {
    echo '<div class="msg_info_box">No results were found. Please try a different search.</div>';
    }
    while($script=mysql_fetch_assoc($q)){
    $str = $script['discription'];
    $wcount = strlen($str);
    if ($wcount > 295){
    $dis = substr($str, 0, 295).'...';
    }else{
    $dis = $str;

}
    $sname = $script['title'];
    $sname = preg_replace("![^a-z0-9]+!i", "-", $sname);
    $sname = strtolower($sname);
    $lid = $script['licens_id'];

$license= mysql_query("SELECT * FROM licens WHERE id='$lid' LIMIT 1") or die (mysql_error());
$lrow = mysql_fetch_array($license);

?>
<div class="new_scripts">
<div class="n_title"><a href="script-<?php echo $script['script_id'];?>-<?php echo $sname;?>.html"><?php echo $script['title'];?></a></div>
<div class="n_dis"><?php echo $dis;?></div>
<div class="n_menu">
<!--#--> 
<div class="ii_info"><span class="is_lable"><a class="s_link" href="<?php echo $script['script_url'];?>" target="_blank">Visit Publisher Site</a></span> | </div>
<div class="ii_info"><span class="is_lable"><a class="s_link" href="<?php echo $script['script_demo'];?>" target="_blank">View Demo</a></span></div>
<div class="i_info"><span class="is_lable">License Type:</span><span class="license"> <?php echo $lrow['lname'];?></span></div>
<!--#-->
</div>
</div> 
<?php } ?>

<div class="pagediv"><?php echo $pagination;?></div>
</div>

im wondering is the any way to give the search form action like action="search.php?term=keyword. i think if i can post the url like this i can use $_GET to take the keyworkd. if anyone can help me with this in anyway i really appropriated. thanks in advanced.

Was it helpful?

Solution

Change your action attribute to get

<form name="search" id="search" method="get" action="search.php">

This way your form's values will be appended to search.php on submission.

For example:

search.php?term=the_term

OTHER TIPS

Rather than inline pagination code you an have a class and then can use it by calling its functions. This way you can avoid re-coding and its a better way to program.

You do not need to do anything with the class but just need to check the call.

Call:

    $paging = new Pager;
    $pagesize=$_GET['pagesize']; 
    $sql="select * from tbl where 1 order by tbl_id desc ";
    $limit=$paging->pageSize($pagesize);
    $start = $paging->findStart($limit);
    $ExecQuery=mysql_query($sql) or die(mysql_error());
    $count=mysql_num_rows($ExecQuery);
    $pages =$paging->findPages($count, $limit);
    $pagelist = $paging->pageList($_GET['page'], $pages,$pagesize , "srchfield=234&srcfield2=455");
    $showingR=$paging->showingRecords($start,$limit,$count);

    $orderBy=$_REQUEST['order_by'];
    $orderBy2=$_REQUEST['order_by2'];
    $Exec=mysql_query("$sql $orders LIMIT  $start , $limit") or die(mysql_error());
    $countrecord=mysql_num_rows($Exec);

Pager Class

class Pager {

        function pageSize($size) 
        {
            if(!empty($size)) 
            {
                return $size;
            }else{
                return 10;
            }   
        }

        function findStart($limit) {
            if ((!isset($_GET['page'])) || ($_GET['page'] == "1"))  {

                $start = 0;
                $_GET['page'] = 1;
            }
            else {
                $start = ($_GET['page']-1) * $limit;
            }
            return $start;
        }
        function findPages($count, $limit) {

                $pages = (($count % $limit) == 0) ? $count / $limit : floor($count / $limit) + 1;

                return $pages;
        }
        function pageList($curpage, $pages,$pagesize,$queryString=false) 
        {
            $page_list = "";
            if (($curpage-1) > 0) 
            {
                if($pages<=5)
                {
                    $page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."&".$queryString."&pagesize=".$pagesize."\" title=\"Previous Page\">Previous</a>                       &nbsp;&nbsp;";
                }
                else
                {
                    $page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".'1'."&".$queryString."&pagesize=".$pagesize."\" title=\"First Page\">First</a>&nbsp;&nbsp;&nbsp;&nbsp;                       <a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."&".$queryString."&pagesize=".$pagesize."\" title=\"Previous Page\">Previous</a>                       &nbsp;&nbsp;";
                }
            }
            $limit=$pages-4;
            $startpage=$curpage;
            if($pages<=5)
                {
                    $startpage=1;
                    $lastpage=$pages;
                }
            elseif($startpage>$limit)
                    {
                        $startpage=$limit;
                        $lastpage=$pages;
                    }
                    else
                    {
                        $lastpage=$startpage+4;
                    }
        for ($i=$startpage; $i<=$pages and $i<=$lastpage; $i++) 
        {
            if ($i == $curpage) 
            {
             $page_list .= "<b>".$i."</b>";
            }
            else 
            {
             $page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".$i."&".$queryString."&pagesize=".$pagesize."\" title=\"Page ".$i."\">".$i."</a>";
            }
            $page_list .= " ";
        }

        /* Print the Next and Last page links if necessary */
            if (($curpage+1) <= $pages)
            {
               if(($curpage+1) <= $limit)
               {  
                    $page_list .= "&nbsp;&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."&".$queryString."&pagesize=".$pagesize."\" title=\"Next Page\">Next</a>
                    &nbsp;&nbsp;&nbsp;&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?page=".$pages."&".$queryString."&pagesize=".$pagesize."\" title=\"Last Page\">Last</a> ";
               }
               else
               {
                    $page_list .= "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."&".$queryString."&pagesize=".$pagesize."\" title=\"Next Page\">Next</a>";
               }
            }

            $page_list .= "</td>\n";

            return $page_list;
        }
        function nextPrev($curpage, $pages,$pagesize) {

            $next_prev = "";

            if (($curpage-1) <= 0) {

                $next_prev .= "Previous";
        }else {

            $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."&pagesize=".$pagesize."\">Previous</a>";
        }

            $next_prev .= " | ";

        if (($curpage+1) > $pages) {

            $next_prev .= "Next";

        }else {

            $next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."&pagesize=".$pagesize."\">Next</a>";
        }

            return $next_prev;
        }

        function showingRecords($start,$limit,$count) {

            $toEnd=$start+$limit;

            if($toEnd>$count) {

                $endPoint=$count;

            }else {

                $endPoint=$toEnd;
            }

            return ($start+1)." to ".$endPoint." of ".$count;
        }

        }
         ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top