我有一个使用方法post的表单。表单包含一个提交按钮,单击它时,我的数据库记录将显示在html表中。我想限制每页显示的行数(5),但我不想使用GET。有什么方法可以用我的post方法做到这一点吗?
// My form using post method
<form action = "" name = "dealer_call_log.php" id = "dealer_call_log.php" method = "post">
//Data displayed in the table below from post
$record_per_page = 5;
$page = '';
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else{
$page = 1;
}
$start_from = ($page - 1) * $record_per_page;
if(isset($_POST['submit'])){
echo '<center>';
echo '<br>';
$sql = "SELECT SFID, Comment, Time FROM tbl_call_log_detail
WHERE
(dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
OR ( Time <= '$endDate' AND Time >= '$startDate'
AND (dealer_id = '$call_id' OR'$call_id'='' ))
OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01' AND '$call_id'='')
ORDER BY Time DESC LIMIT $start_from, $record_per_page" ;
$result = mysqli_query($conn, $sql);
$rows = mysqli_num_rows($result);
$all_property = array();
echo "<table class = 'data-table' border = '1' cellpadding = '9' bgcolor = '#CCCCCC'>
<tr class = 'data-heading'>";
while($property = mysqli_fetch_field($result)){
echo '<td><b> '. $property ->name. ' </b></td>';
array_push($all_property, $property ->name);
}
echo '</tr>';
}
while ($row = mysqli_fetch_array($result)){
echo '<tr>';
foreach($all_property as $item){
echo '<td> '. $row[$item] . ' </td>';
}
echo '</tr>';
echo '</center>';
}
}
$page_query = "SELECT * FROM tbl_call_log_detail ";
$page_result = mysqli_query($conn, $page_query);
$total_records = mysqli_num_rows($page_result);
$total_pages = ceil($total_records/$record_per_page);
$start_loop = $page;
$difference = $total_pages - $page;
if($difference <= $total_pages){
$start_loop = $total_pages - $difference;
}
$end_loop = $start_loop + 2;
if($difference > $total_pages){
$end_loop = $total_pages;
}
if($page > 1){
echo "<a href= 'dealer_call_log.php?page=1'>First</a>";
echo "<a href= 'dealer_call_log.php?page=".($page - 1)."'><<</a>";
}
for ($i = $start_loop; $i <= $end_loop; $i++){
echo "<a href= 'dealer_call_log.php?page=".$i."'>".$i."</a>";
}
if($page <= $end_loop){
echo "<a href= 'dealer_call_log.php?page=".($page + 1)."'>>></a>";
echo "<a href= 'dealer_call_log.php?page=".$total_pages."'>Last</a>";
}
if($page < 1){
$page = 1;
}
echo '</table>';
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
您可以使用LIMIT 20 OFFSET 0
获取前20个结果。然后,对于下一页,您可以使用LIMIT 20 OFFSET 20
获取第二组20个结果。有关此
您还可以使用帖子跟踪您的页面编号,但是您必须将其发布到输入字段中。您可以使用隐藏的输入类型:<input type="hidden" name="pageNumber" value="0">
并在每次切换页面时更改值。
然后你可以这样做:
$limit = 20;
if(isset($_POST['pageNumber']) {
$page = $_POST['pageNumber'];
} else {
$page = 0;
}
$sql = "SELECT sfid, comment, time_stamp, time_of_submission FROM tbl_call_log_detail
WHERE
(dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
OR ( time_stamp <= '$endDate' AND time_stamp >= '$startDate'
AND (dealer_id = '$call_id' OR'$call_id'='' ))
OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01' AND '$call_id'='')
ORDER BY time_stamp DESC
LIMIT " . $limit . " OFFSET " . $page * $limit;
echo '<input type="hidden" name="pageNumber" value="' . $page + 1 . '">'