搜索限制使用分页PHP

时间:2014-10-06 05:45:48

标签: php mysql search

我希望功能搜索使用分页,就像谷歌一样 但我很困惑:(

我的数据库已有140635条记录,我想要 限制50条记录/页

我的数据供应商是“Hariff”,“Tingali”,“FMC”等...

如果我在表单文件localhost / grid.php中写“Hariff”

此页面重定向到localhost / search_grid.php 这是工作搜索,这个表只显示“哈夫”:)

但我是去第2,3页,下一页,最后一次

此表显示随机供应商(“Hariff”,“Tingali”,“FMC”等...) 不只是供应商“哈夫”:(

我很困惑请帮帮我.. 感谢的

grid.php

    <form method="GET" action="search_grid.php" id="search">
  <input name="q" type="text" size="40" placeholder="Search for Vendor" />
</form>

search_grid.php

<?php 
      $q = $_GET['q'];
      include "conection.php";
      include "func_search_grid.php";

        $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
        $limit = 50;
        $startpoint = ($page * $limit) - $limit;

        //to make pagination
        $statement = "`task`";

?>

<table class="bordered">
    <thead>

    <tr>
        <th><center>Vendor</center></th>
    </tr>
    </thead>
     <?php 

            $query = mysql_query("SELECT * FROM {$statement} where vendor like '%{$q}%' LIMIT {$startpoint} , {$limit}");

            while ($row = mysql_fetch_assoc($query)) {
                            ?>
    <tr>
                                    <td>
                                    <?php 
                                    if ($row['vendor']!=NULL) 
                                    {
                                        echo "{$row['vendor']}";
                                    }
                                    else 
                                    {
                                        echo "<center><font color='red'>Vendor NULL</font></center>";
                                    }
                                    ?>
                                    </td>   
    </tr>        
   <?php } ?>

</table>    
    <br/>

<?php
    echo pagination($statement,$limit,$page);
?>

func_search_grid.php

    <?php

   function pagination($query, $per_page = 10,$page = 1, $url = '?'){     
        $q = $_GET['q']; 
        $query = "SELECT COUNT(*) as `num` FROM {$query} where vendor like '%$q%' ";
        $row = mysql_fetch_array(mysql_query($query));
        $total = $row['num'];
        $adjacents = "2"; 

        $page = ($page == 0 ? 1 : $page);  
        $start = ($page - 1) * $per_page;                               

        $prev = $page - 1;                          
        $next = $page + 1;
        $lastpage = ceil($total/$per_page);
        $lpm1 = $lastpage - 1;

        $pagination = "";
        if($lastpage > 1)
        {   
            $pagination .= "<ul class='pagination'>";
                    $pagination .= "<li class='details'>Page $page of $lastpage</li>";
            if ($lastpage < 7 + ($adjacents * 2))
            {   
                for ($counter = 1; $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<li><a class='current'>$counter</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page=$counter?q=$q'>$counter</a></li>";                   
                }
            }
            elseif($lastpage > 5 + ($adjacents * 2))
            {
                if($page < 1 + ($adjacents * 2))        
                {
                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter?q=$q'>$counter</a></li>";                   
                    }
                    $pagination.= "<li class='dot'>...</li>";
                    $pagination.= "<li><a href='{$url}page=$lpm1?q=$q'>$lpm1</a></li>";
                    $pagination.= "<li><a href='{$url}page=$lastpage?q=$q'>$lastpage</a></li>";     
                }
                elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                {
                    $pagination.= "<li><a href='{$url}page=1?q=$q'>1</a></li>";
                    $pagination.= "<li><a href='{$url}page=2?q=$q'>2</a></li>";
                    $pagination.= "<li class='dot'>...</li>";
                    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter?q=$q'>$counter</a></li>";                   
                    }
                    $pagination.= "<li class='dot'>..</li>";
                    $pagination.= "<li><a href='{$url}page=$lpm1?q=$q'>$lpm1</a></li>";
                    $pagination.= "<li><a href='{$url}page=$lastpage?q=$q'>$lastpage</a></li>";     
                }
                else
                {
                    $pagination.= "<li><a href='{$url}page=1?q=$q'>1</a></li>";
                    $pagination.= "<li><a href='{$url}page=2?q=$q'>2</a></li>";
                    $pagination.= "<li class='dot'>..</li>";
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter?q=$q'>$counter</a></li>";                   
                    }
                }
            }

            if ($page < $counter - 1){ 
                $pagination.= "<li><a href='{$url}page=$next?q=$q'>Next</a></li>";
                $pagination.= "<li><a href='{$url}page=$lastpage?q=$q'>Last</a></li>";
            }else{
                $pagination.= "<li><a class='current'>Next</a></li>";
                $pagination.= "<li><a class='current'>Last</a></li>";
            }
            $pagination.= "</ul>\n";        
        }


        return $pagination;
    } ?>

0 个答案:

没有答案