【问题标题】:how to apply pagination on search result from multiple fields using post method and $_SERVER['PHP_SELF']如何使用 post 方法和 $_SERVER['PHP_SELF'] 对来自多个字段的搜索结果应用分页
【发布时间】:2025-12-25 15:25:15
【问题描述】:

我是 PHP 新手。我开发了一个新页面,显示表格中的所有结果。我还在页面顶部放置了一些过滤器以显示搜索结果。提交搜索结果后,它会显示过滤结果并带有适当的分页。但是当我点击分页号时。第 2 或第 3 页,它再次显示所有结果。我在 * 网站或其他网站上搜索了很多,但无法这样做。请帮助解决这个问题。我在这里发布了代码

// single page for show result and submit query "allcustomerdetails.php"

<form name="frmnamesd" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" onSubmit="return validated(this)" id = "frmnamesd" enctype="multipart/form-data">
                <fieldset class="row1">
                &nbsp &nbsp; <label>Search By:</label>
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;<label>Customer ID:</label> &nbsp &nbsp;
                <input type="text" name="c_id" size = "8" onKeyPress="return isNumberKey(event)" value = "<?php echo isset($_POST['c_id']) ? $_POST['c_id'] : '';?>" />&nbsp &nbsp;
                <label>Name:</label> &nbsp &nbsp;
                <input type="text" name="c_name" size = "10" value = "<?php echo isset($_POST['c_name']) ? $_POST['c_name'] : '';?>" /> &nbsp &nbsp;
                <label>Mobile:</label> &nbsp &nbsp;
                <input type="text" name="c_mobile" onKeyPress="return isNumberKey(event)" size="10" value = "<?php echo isset($_POST['c_mobile']) ? $_POST['c_mobile'] : '';?>" /> &nbsp &nbsp;
                <label>Customer Type:</label>&nbsp &nbsp 
                <select name="c_type">              
                    <option value="<?php echo isset($_POST['c_type']) ? $_POST['c_type'] : '';?>" ><?php echo isset($_POST['c_type']) ? $_POST['c_type'] : "-select-";?></option>
                    <option value="IW">IW</option>
                    <option value="AMC">AMC</option>
                    <option value="SAC">SAC</option>
                    <option value="OW">OW</option>
                    <option value="PS">PS</option>
                </select>&nbsp &nbsp;
                <label>Date From/To:</label>&nbsp &nbsp;
                <input type="date" name="c_from" value = "<?php echo isset($_POST['c_from']) ? $_POST['c_from'] : '';?>">
                <input type="date" name="c_to" value = "<?php echo isset($_POST['c_to']) ? $_POST['c_to'] : '';?>" ><br><br>
                &nbsp &nbsp&nbsp &nbsp;<label>Address:</label> &nbsp &nbsp;&nbsp &nbsp;&nbsp &nbsp;&nbsp &nbsp;
                <input type="text" name="c_address" size = "32" value = "<?php echo isset($_POST['c_address']) ? $_POST['c_address'] : '';?>" > &nbsp &nbsp;
                <center><input type="Submit" name="submit" value="Submit">&nbsp &nbsp;<input type="reset" name="reset" value="reset"></center>
                </fieldset>
                </form>

<?php 

$limit = 100;  
if (isset($_GET["page"])) { 

    $page  = $_GET["page"];
    $page_offset = ($page-1) * $limit; 
} 
else { 
$page_offset = 0;
$page = 1;
} 

$query = "SELECT * FROM customer_details";
$querycount = "SELECT COUNT(*) FROM customer_details";
if(isset($_POST['submit'])) {
    // define the list of fields
    $fromdate = $_POST['c_from'];
    $todate  = $_POST['c_to'];
    $fields = array('c_id','c_name','c_mobile', 'c_type','c_address');
    $conditions = array();

    // loop through the defined fields
    foreach($fields as $field){
        //$abc = $_POST[$field];
        //die;
        // if the field is set and not empty
        if(isset($_POST[$field]) && $_POST[$field] != '') {
            // create a new condition while escaping the value inputed by the user (SQL Injection)
            $conditions[] = "`$field` LIKE '%" . mysqli_real_escape_string($con, $_POST[$field]) . "%'";
        }
    }
    if(isset($_POST['c_from']) && $_POST['c_to'] != '') {
    // builds the query
            $query .= " WHERE c_next_service between '$fromdate' and '$todate'";
            $querycount .= " WHERE c_next_service between '$fromdate' and '$todate'";

            if(count($conditions) > 0) {

                $query .= " AND" . implode (' AND ', $conditions);
                $querycount .= " AND" . implode (' AND ', $conditions);
                $query .= " order by c_id desc LIMIT $page_offset, $limit";
                $querycount .= " order by c_id desc";
            }
            else {
                $query .= " order by c_id desc LIMIT $page_offset, $limit";
                $querycount .= " order by c_id desc";
            }
        }
    // if there are conditions defined
    elseif(count($conditions) > 0) {
            $query .= " WHERE" . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
            $query .= " order by c_id desc LIMIT $page_offset, $limit" ;
            $querycount .= " WHERE" . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
            $querycount .= " order by c_id desc" ;
        }
    else {
        // append the conditions
            $query .= " order by c_id desc LIMIT $page_offset, $limit";
            $querycount .= " order by c_id desc";
        }
    }
else {
    $query .= " order by c_id desc LIMIT $page_offset, $limit";
    $querycount .= " order by c_id desc";
}

$result = mysqli_query($con, $query);
$resultcount = mysqli_query($con,$querycount);
$count = mysqli_fetch_row($resultcount);  
$totalrows = $count[0];  

<?php if($_POST) {?>
<br><center><button onClick="history.go(-1)">Go Back</button></center>

<?php
    }
$total_pages = ceil($totalrows/$limit);  
$pagLink = "<div class='pagin'>";  
for ($i=1; $i<=$total_pages; $i++) { 
                $pagLink .= "<a href='allcustomerdetail.php?page=".$i."'>&nbsp;&nbsp;&nbsp;&nbsp;".$i."</a>";   
};  
    ?><br><br><center><?php echo $pagLink . "</div>";?><br><br></center>

【问题讨论】:

    标签: php mysql post pagination


    【解决方案1】:

    在 * 用户提供一些线索后,我能够解决我的问题,我在下面发布了一些代码:

    <?php if(isset($_GET['msg'])) { ?><center><h5><b><font color="red"><?php echo $_GET['msg'];}?></font></b></h5></center>         
                    <h4>All Customers Detail</h4>
    
                    <form name="frmnamesd" method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" onSubmit="return validated(this)" id = "frmnamesd" enctype="multipart/form-data">
                    <fieldset class="row1">
                    &nbsp &nbsp; <label>Search By:</label>
                    <br>
                    &nbsp;&nbsp;&nbsp;&nbsp;<label>Customer ID:</label> &nbsp &nbsp;
                    <input type="text" name="c_id" size = "8" onKeyPress="return isNumberKey(event)" value = "<?php echo isset($_GET['c_id']) ? $_GET['c_id'] : '';?>" />&nbsp &nbsp;
                    <label>Name:</label> &nbsp &nbsp;
                    <input type="text" name="c_name" size = "10" value = "<?php echo isset($_GET['c_name']) ? $_GET['c_name'] : '';?>" /> &nbsp &nbsp;
                    <label>Mobile:</label> &nbsp &nbsp;
                    <input type="text" name="c_mobile" onKeyPress="return isNumberKey(event)" size="10" value = "<?php echo isset($_GET['c_mobile']) ? $_GET['c_mobile'] : '';?>" /> &nbsp &nbsp;
                    <label>Customer Type:</label>&nbsp &nbsp 
                    <select name="c_type">              
                        <option value="<?php echo isset($_GET['c_type']) ? $_GET['c_type'] : '';?>" ><?php echo isset($_GET['c_type']) ? $_GET['c_type'] : "-select-";?></option>
                        <option value="IW">IW</option>
                        <option value="AMC">AMC</option>
                        <option value="SAC">SAC</option>
                        <option value="OW">OW</option>
                        <option value="PS">PS</option>
                    </select>&nbsp &nbsp;
                    <label>Date From/To:</label>&nbsp &nbsp;
                    <input type="date" name="c_from" value = "<?php echo isset($_GET['c_from']) ? $_GET['c_from'] : '';?>">
                    <input type="date" name="c_to" value = "<?php echo isset($_GET['c_to']) ? $_GET['c_to'] : '';?>" ><br><br>
                    &nbsp &nbsp&nbsp &nbsp;<label>Address:</label> &nbsp &nbsp;&nbsp &nbsp;&nbsp &nbsp;&nbsp &nbsp;
                    <input type="text" name="c_address" size = "32" value = "<?php echo isset($_GET['c_address']) ? $_GET['c_address'] : '';?>" > &nbsp &nbsp;
                    <center><input type="Submit" name="submit" value="Submit">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" name="reset" value="Clear">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button><a href="allcustomerdetail.php" >Refresh</a></button></center>
                    </fieldset>
                    </form>
    
    <?php 
    
    $limit = 100;  
    if (isset($_GET["page"])) { 
    
        $page  = $_GET["page"];
        $page_offset = ($page-1) * $limit; 
    } 
    else { 
    $page_offset = 0;
    $page = 1;
    } 
    //echo  $page_offset;
    //die;
    
    $query = "SELECT * FROM customer_details";
    $querycount = "SELECT COUNT(*) FROM customer_details";
    if(isset($_GET['submit'])) {
        // define the list of fields
        $fromdate = $_GET['c_from'];
        $todate  = $_GET['c_to'];
        $fields = array('c_id','c_name','c_mobile', 'c_type','c_address');
        $conditions = array();
    
        // loop through the defined fields
        foreach($fields as $field){
            //$abc = $_GET[$field];
            //die;
            // if the field is set and not empty
            if(isset($_GET[$field]) && $_GET[$field] != '') {
                // create a new condition while escaping the value inputed by the user (SQL Injection)
                $conditions[] = "`$field` LIKE '%" . mysqli_real_escape_string($con, $_GET[$field]) . "%'";
            }
        }
        if(isset($_GET['c_from']) && $_GET['c_to'] != '') {
        // builds the query
                $query .= " WHERE c_next_service between '$fromdate' and '$todate'";
                $querycount .= " WHERE c_next_service between '$fromdate' and '$todate'";
    
                if(count($conditions) > 0) {
    
                    $query .= " AND" . implode (' AND ', $conditions);
                    $querycount .= " AND" . implode (' AND ', $conditions);
                    $query .= " order by c_id desc LIMIT $page_offset, $limit";
                    $querycount .= " order by c_id desc";
                }
                else {
                    $query .= " order by c_id desc LIMIT $page_offset, $limit";
                    $querycount .= " order by c_id desc";
                }
            }
        // if there are conditions defined
        elseif(count($conditions) > 0) {
                $query .= " WHERE" . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
                $query .= " order by c_id desc LIMIT $page_offset, $limit" ;
                $querycount .= " WHERE" . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
                $querycount .= " order by c_id desc" ;
            }
        else {
            // append the conditions
                $query .= " order by c_id desc LIMIT $page_offset, $limit";
                $querycount .= " order by c_id desc";
            }
        }
    else {
        $query .= " order by c_id desc LIMIT $page_offset, $limit";
        $querycount .= " order by c_id desc";
    }
    
    $result = mysqli_query($con, $query);
    $resultcount = mysqli_query($con,$querycount);
    $count = mysqli_fetch_row($resultcount);  
    $totalrows = $count[0];  
    

    使用 $_SERVER['PHP_SELF'] 我将 POST 方法更改为 GET 方法

    <?php 
    
    $total_pages = ceil($totalrows/$limit);  
    $pagLink = "<div class='pagin'>";
    
    if(isset($_GET['submit'])) { ?>
    <br><center><button onClick="history.go(-1)">Go Back</button></center>
    <?php 
    $url = "c_id=".$_GET['c_id']."&c_name=".$_GET['c_name']."&c_mobile=".$_GET['c_mobile']."&c_type=".$_GET['c_type']."&c_from=".$_GET['c_from']."&c_to=".$_GET['c_to']."&c_address=".$_GET['c_address']."&submit=".$_GET['submit'];
    for ($i=1; $i<=$total_pages; $i++) { 
                    $pagLink .= "<a href='{$_SERVER['PHP_SELF']}?".$url."&page=".$i."'>&nbsp;&nbsp;&nbsp;&nbsp;".$i."</a>";   
    };  
        ?><br><br><center><?php echo $pagLink . "</div>";?><br><br></center>
    <?php
     }
     else {
    
    for ($i=1; $i<=$total_pages; $i++) { 
                    $pagLink .= "<a href='{$_SERVER['PHP_SELF']}?page=".$i."'>&nbsp;&nbsp;&nbsp;&nbsp;".$i."</a>";   
    };  
     ?><br><br><center><?php echo $pagLink . "</div>"; }?><br><br></center>
    

    像魅力一样工作......

    【讨论】:

      【解决方案2】:

      这是因为当您在第二页时,您没有得到任何发布数据,并且您的查询变成了 SELECT * FROM customer_details order by c_id desc LIMIT $page_offset, $limit,就像这样,它为您提供了所有记录。您应该在会话中存储所有变量或查询。 阅读来自here的会话

      【讨论】:

      • 我欢迎你的想法。但我是 PHP 新手。请发送一个示例以通过会话或查询存储变量....
      • @J.S.Bajaj 在查询中添加了链接并更新答案请再次检查是语法错误
      • @J.S.Bajaj 很高兴它成功了。您也可以访问:Ask question | Get answers 以获取有关 SO 的更多信息并尽可能多地支持我。你也可以标记为正确的
      最近更新 更多