VHEiR
VHEiR

Reputation: 1

PHP Pagination: Won't Display in next page

Good Day! I have a problem in my pagination, my pagination will display the data that is between from date (January 1, 2018) to date (January 31, 2018), at first page it's working properly. When I click the next page the result are gone and I have to re entry the data Between the Date. Here's my Pagination Code.

<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>';
}
?>

The Result of my Pagination and my Desired Output.

First Page Result

Next Page Result

My Desired Output When Click Next Page

Upvotes: 0

Views: 186

Answers (1)

Syscall
Syscall

Reputation: 19764

No $_POST data are sent when you "click on a link", only when you submit a form.

So, you could do something like this :

  • Change the method of the <form> to "GET",
  • Change to way you get the from and to (and see bottom note 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.
    
  • Change your pagination links :

    echo '<a href="efms.php?page=' . $page . '&from=' . $from . '&to=' . $to .'">...</a>' ;
    
  • Write your <input> tags after getting $from and $to to fill them with those values.

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

Another method could be to store $from and $to into the $_SESSION and change them if something is posted.

Finally, and important notes :

  1. you should have a look to mysqli_prepare() to secure you queries against SQL injections.

  2. you should secure your input data to prevent your site from XSS attacks.

Upvotes: 1

Related Questions