我们可以使用POST在多个页面中显示表格记录吗? [英] Can we display table records in multiple pages using POST?

查看:124
本文介绍了我们可以使用POST在多个页面中显示表格记录吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用方法post的表单。表单包含一个提交按钮,点击它后,我的数据库记录显示在一个html表格中。我想限制显示到每个页面的行数(5),但我不想使用GET。有什么办法可以用我的post方法做到这一点?

I have a form that uses the method post. The form contains a submit button and on clicking it, my database records get displayed in an html table. I would like to limit the number of rows (5) displayed to each page, but I'm not looking to use GET. Is there any way I can do that with my post method?

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

任何帮助将不胜感激。感谢!

Any help would be appreciated. Thanks!

推荐答案

您可以使用 LIMIT 20 OFFSET 0 前20个结果。然后在下一页中,您可以使用 LIMIT 20 OFFSET 20 获得第二组20个结果。有关此功能的更多信息,请参见 W3School

You can use LIMIT 20 OFFSET 0 to get the first 20 results. Then for the next page you can use LIMIT 20 OFFSET 20 the get the second set of 20 results. See W3Schools for more information on this

您也可以使用信息跟踪您的页码,但您必须将其发布在输入字段中。您可以使用隐藏的输入类型,如下所示:< input type =hiddenname =pageNumbervalue =0> 并且每次切换时更改该值页。

You can also keep track of your pagenumber with post but you'll have to post it in an input field. You can use an input type hidden like so: <input type="hidden" name="pageNumber" value="0"> and change the value everytime you switch page.

然后你可以这样做:

Then you could do something like this:

    $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 . '">'

这篇关于我们可以使用POST在多个页面中显示表格记录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆