【问题标题】:PHP Pagination: Won't Display in next pagePHP分页:不会显示在下一页
【发布时间】:2018-01-29 20:47:13
【问题描述】:

美好的一天!我的分页有问题,我的分页将显示从日期(2018 年 1 月 1 日)到日期(2018 年 1 月 31 日)之间的数据,在第一页它工作正常。当我单击下一页时,结果消失了,我必须重新输入日期之间的数据。这是我的分页代码。

<th><input type="text" name="from" id="from" class="tcal"> to <input type="text" name="to" id="to" class="tcal"><br><br></th>
<th><input type="submit" class="button" value=""></th>
<?php
$con = mysqli_connect('localhost','root','');
mysqli_select_db($con, 'efms');

$results_per_page = 20;
$from=@$_POST['from'];
$to=@$_POST['to'];
$sql="SELECT *, SUM(rqty), SUM(iqty), SUM(bbal), COUNT(item), COUNT(description) FROM issuances WHERE trandate BETWEEN '$from' AND '$to' GROUP BY item, description, category ORDER BY item, description, category ASC";
$result = mysqli_query($con, $sql);
$number_of_results = mysqli_num_rows($result);

$number_of_pages = ceil($number_of_results/$results_per_page);

if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}

$this_page_first_result = ($page-1)*$results_per_page;

$from=@$_POST['from'];
$to=@$_POST['to'];
$sql="SELECT *, SUM(rqty), SUM(iqty), SUM(bbal), COUNT(item), COUNT(description) FROM issuances WHERE trandate BETWEEN '$from' AND '$to' GROUP BY item, description, category ORDER BY item, description, category ASC LIMIT " . $this_page_first_result . ',' .  $results_per_page;
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)) {
$id=$row['id'];
$item=$row['item'];
$description=$row['description'];
$rqty=$row['SUM(rqty)'];
$iqty=$row['SUM(iqty)'];
$bqty=$row['bqty']; 
$bbal=$row['SUM(bbal)'];    
$totqtyb=$bbal+$rqty-$iqty;
echo "<th width='80px' class='text-left left bottom right'>$item ($description)</th>";
echo "<th width='80px' class='right bottom'>$bbal</th>";
echo "<th width='100px' class='right bottom'></th>";
echo "<th width='100px' class='right bottom'></th>";
echo "<th width='80px' class='right bottom'>$rqty</th>";
echo "<th width='100px' class='right bottom'></th>";
echo "<th width='100px' class='right bottom'></th>";
echo "<th width='100px' class='right bottom'>$rqty</th>";
echo "<th width='80px' class='right bottom'>$iqty</th>";
echo "<th width='100px' class='right bottom'></th>";
echo "<th width='100px' class='right bottom'></th>";
echo "<th width='100px' class='right bottom'>$totqtyb</th>";
echo "</tr>";
}
for ($page=1;$page<=$number_of_pages;$page++) {
echo '<b><a href="efms.php?page=' . $page . '">&nbsp;&nbsp;&nbsp;' . $page . '&nbsp;&nbsp;&nbsp;</a></b>';
}
?>

我的分页结果和我想要的输出。

First Page Result

Next Page Result

My Desired Output When Click Next Page

【问题讨论】:

  • 没有$_POST 数据在您“点击链接”时发送,仅在您提交表单时发送。
  • 我该怎么办?

标签: php jquery pagination


【解决方案1】:

只有在您提交表单时,才会在“单击链接”时发送$_POST 数据。

所以,你可以这样做:

  • &lt;form&gt;method改为"GET"
  • 更改获取fromto 的方式(见底部注释2):

    $from = isset($_GET['from']) ? $_GET['from'] : '' ; // or change '' to a default initial date. 
    $to = isset($_GET['to']) ? $_GET['to'] : '' ; // or change '' to a default end date.
    
  • 更改您的分页链接:

    echo '<a href="efms.php?page=' . $page . '&from=' . $from . '&to=' . $to .'">...</a>' ;
    
  • 在得到$from$to 后写下你的&lt;input&gt; 标签,用这些值填充它们。

    echo '<th><input type="text" name="from" id="from" class="tcal" value="'.$from.'"> 
          to 
          <input type="text" name="to" id="to" class="tcal" value="'.$to.'"><br><br></th>' ;
    

另一种方法是将$from$to 存储到$_SESSION 中,并在发布内容时更改它们。

最后,重要说明

  1. 您应该查看mysqli_prepare() 以保护您对SQL injections 的查询。

  2. 您应该保护您的输入数据,以防止您的网站被 XSS attacks 访问。

【讨论】:

  • 感谢您的回复,我试过了,但它不能正常工作或者我不是那么好。分页链接不会显示。我不熟悉 mysqli_prepare() / SQL Injections / XSS 攻击,但我会记下你的。
猜你喜欢
  • 2017-08-31
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-17
  • 2011-08-02
  • 1970-01-01
相关资源
最近更新 更多