doumingo04696 2013-09-11 08:26
浏览 47
已采纳

搜索和分页

I have a search form and i want to put a pagination on , so far i succeed in doing the search and pagination but i found out that the when i press the next button or second page on the search using pagination it did no fetch the data from search query how do i fix it

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

Thats my code for Search and here is the code for pagination

  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条回答 默认 最新

  • duanshai4484 2013-09-11 08:47
    关注

    Here is a sample one, which may help you

    <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>”;  
    ?>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址