MySQL的PHP​​搜索与分页

时间:2012-07-28 12:05:33

标签: php mysql

我正在尝试搜索我正在处理的项目。我面临的问题是当我进入第二页搜索结果时会出错。

这是我的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" />

这是我的搜索页面php代码

<?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>

我想知道是什么方式给搜索表单操作像action =“search.php?term = keyword。我想如果我可以发布这样的网址我可以使用$ _GET来接受keyworkd。如果有人可以无论如何我真的很适合帮助我。感谢先进。

2 个答案:

答案 0 :(得分:4)

将您的action属性更改为get

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

这样,您的表单的值将在提交时附加到search.php

例如:

search.php?term=the_term

答案 1 :(得分:0)

而不是内联分页代码,你有一个类,然后可以通过调用它的函数来使用它。这样您就可以避免重新编码,这是一种更好的编程方式。

您不需要对课程做任何事情,只需要检查电话。

拨打

    $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);

传呼机类

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;
        }

        }
         ?>