Can we display table records in multiple pages using POST?

匆匆过客 提交于 2019-12-13 03:53:28

问题


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!


回答1:


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

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


来源:https://stackoverflow.com/questions/46409070/can-we-display-table-records-in-multiple-pages-using-post

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!