gpt4 book ai didi

php - 如何使用 post 方法和 $_SERVER ['PHP_SELF' 对多个字段的搜索结果应用分页]

转载 作者:行者123 更新时间:2023-11-29 10:51:14 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

关于php - 如何使用 post 方法和 $_SERVER ['PHP_SELF' 对多个字段的搜索结果应用分页],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43692072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com