搜索和分页

时间:2013-09-11 08:26:53

标签: php mysql twitter-bootstrap pagination twitter-bootstrap-3

我有一个搜索表单,我想要分页,到目前为止我成功进行搜索和分页但我发现当我按下分页上的下一个按钮或第二页时使用分页它没有从搜索查询中获取数据如何解决?

'if(isset($_POST['butendn']) )
        {

        if ($_REQUEST["Category"]<>'') {
            $search_Category = " AND Category='".mysql_real_escape_string($_REQUEST["Category"])."'";   
        }
        if ($_REQUEST["ProductCode"]<>'') {
            $search_ProductCode = " AND ProductCode='".mysql_real_escape_string($_REQUEST["ProductCode"])."'";  
        }
        if ($_REQUEST["OutletName"]<>'') {
            $search_OutletName = " AND OutletName='".mysql_real_escape_string($_REQUEST["OutletName"])."'"; 
        }

            $targetpage = "pmb.php";
            $limit = 10;  
            $query = "SELECT COUNT(ECRNo) as num FROM ".$SETTINGS['data_table']."";
            $total_pages = mysql_fetch_array(mysql_query($query));
            $total_pages = $total_pages[num];

            $stages = 3;
            $page = mysql_escape_string($_GET['page']);
            if($page){
                $start = ($page - 1) * $limit; 
            }else{
                $start = 0; 
                }  

        $search_groupby = "Group by CategoryID,OracleCategory,ProductCode,SalesDate ORDER BY CategoryID,OracleCategory,ProductCode LIMIT $start, $limit ";


        if ($_REQUEST["startfrom"]<>'' and $_REQUEST["end"]<>'') {
            $sql = "SELECT ECRNo,SalesDate,TimeSold,AmountDue,OutletName,ProductCode,Category,MealType,SUM(Quantity) AS Quantity1,ROUND(SUM(NetPrice)) AS NetPrice1 FROM ".$SETTINGS["data_table"]." WHERE SalesDate BETWEEN '".mysql_real_escape_string($_REQUEST["startfrom"])."'And'".mysql_real_escape_string($_REQUEST["end"])."'".$search_OutletName.$search_Category.$search_ProductCode.$search_groupby ;
        } else if ($_REQUEST["startfrom"]<>'') {
            $sql = "SELECT ECRNo,SalesDate,TimeSold,AmountDue,SalesDate,OutletName,ProductCode,Category,MealType,SUM(Quantity) AS Quantity1,ROUND(SUM(NetPrice)) AS NetPrice1 FROM ".$SETTINGS["data_table"]." WHERE SalesDate >= '".mysql_real_escape_string($_REQUEST["startfrom"])."'".$search_OutletName.$search_Category.$search_ProductCode.$search_groupby ;
        } else if ($_REQUEST["end"]<>'') {
            $sql = "SELECT ECRNo,SalesDate,TimeSold,SalesDate,AmountDue,OutletName,ProductCode,Category,MealType,SUM(Quantity) AS Quantity1,ROUND(SUM(NetPrice)) AS NetPrice1 FROM ".$SETTINGS["data_table"]." WHERE SalesDate <= '".mysql_real_escape_string($_REQUEST["end"])."'".$search_OutletName.$search_Category.$search_ProductCode.$search_groupby ;
        }else {
            $sql = "SELECT ECRNo,SalesDate,TimeSold,SalesDate,AmountDue,OutletName,ProductCode,Category,MealType,SUM(Quantity) AS Quantity1,ROUND(SUM(NetPrice)) AS NetPrice1 FROM ".$SETTINGS["data_table"]." WHERE ECRNo>0 ".$search_OutletName.$search_Category.$search_ProductCode.$search_groupby ;
        }

        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
        if (mysql_num_rows($sql_result)>0) {
            while ($row = mysql_fetch_array($sql_result))

            {           
    ?>

                                <tr>

                                    <td class="input-sm"><?php echo ($row["ProductCode"]); ?></td>
                                    <td class="input-sm"><?php echo ($row["Quantity1"]); ?></td>
                                    <td class="input-sm"><?php echo ($row["NetPrice1"]); ?></td>
                                    <td class="input-sm"><?php echo ($row['SalesDate']);?></td>
                                    <td class="input-sm"><?php echo ($row["Category"]); ?></td>

                                </tr>
    <?php
            }
                                            } 
        else
            {

    ?>
                                <tr>
                                    <td colspan="9">No results found.</td>
                                </tr>
    <?php   
            }
        }
    ?>

这是我的搜索代码,这里是分页代码

  if ($page == 0){$page = 1;}
        $prev = $page - 1;  
        $next = $page + 1;                          
        $lastpage = ceil($total_pages/$limit);      
        $LastPagem1 = $lastpage - 1;                    


        $paginate = '';
        if($lastpage > 1)
        {   

            $paginate .= "<ul class='pagination'>";

            if ($page > 1){
                $paginate.= "<li><a href='$targetpage?page=$prev'>← prev</a></li>";
            }else{
                $paginate.= "<li><span class='disabled'>← prev</span></li>";    }


            if ($lastpage < 7 + ($stages * 2))  
            {   
                for ($counter = 1; $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<li><span class='active'>$counter</span></li>";
                    }else{
                        $paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}                   
                }
            }
            elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
            {
                // Beginning only hide later pages
                if($page < 1 + ($stages * 2))       
                {
                    for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                    {
                        if ($counter == $page){
                            $paginate.= "<li><span class='active'>$counter</span></li>";
                        }else{
                            $paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}                   
                    }
                    $paginate.= "<li><a href='#'>...</a></li>";
                    $paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
                    $paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";      
                }

                elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
                {
                    $paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
                    $paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
                    $paginate.= "<li><a href='#'>...</a></li>";
                    for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                    {
                        if ($counter == $page){
                            $paginate.= "<li><span class='active'>$counter</span></li>";
                        }else{
                            $paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}                   
                    }
                    $paginate.= "<li><a href='#'>...</a></li>";
                    $paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
                    $paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";      
                }

                else
                {
                    $paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
                    $paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
                    $paginate.= "<li><a href='#'>...</a>";
                    for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                    {
                        if ($counter == $page){
                            $paginate.= "<li><span class='active'>$counter</span></li>";
                        }else{
                            $paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}                   
                    }
                }
            }


            if ($page < $counter - 1){ 
                $paginate.= "<li><a href='$targetpage?page=$next'>next → </a></li>";
            }else{
                $paginate.= "<li><span class='disabled'>next → </span></li>";
                }

            $paginate.= "</ul>";



    }

1 个答案:

答案 0 :(得分:1)

以下是一个示例,可以帮助您

<link href=“bootstrap.css” rel=“stylesheet”>  
<?php  
$dbhost = ‘localhost’;  
$dbuser = ‘root’;  
$dbpass = ”;  
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (‘Error connecting to mysql’);  
$dbname = ‘test’;  
$connection = mysql_select_db($dbname);  

$limit = 2;  
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };  
$start_from = ($page-1) * $limit;  

$sql = “SELECT * FROM posts ORDER BY title ASC LIMIT $start_from, $limit”;  
$rs_result = mysql_query ($sql);  
?>  
<table class=“table table-bordered table-striped”>  
<thead>  
<tr>  
<th>title</th>  
<th>body</th>  
</tr>  
<thead>  
<tbody>  
<?php  
while ($row = mysql_fetch_assoc($rs_result)) {  
?>  
            <tr>  
            <td><? echo $row["title"]; ?></td>  
            <td><? echo $row["body"]; ?></td>  
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>  
<?php  
$sql = “SELECT COUNT(id) FROM posts”;  
$rs_result = mysql_query($sql);  
$row = mysql_fetch_row($rs_result);  
$total_records = $row[0];  
$total_pages = ceil($total_records / $limit);  
$pagLink = “<div class=’pagination’>”;  
for ($i=1; $i<=$total_pages; $i++) {  
             $pagLink .= “<a href=’index.php?page=”.$i.“‘>”.$i.“</a>”;  
};  
echo $pagLink . “</div>”;  
?>