roger roger - 1 year ago 69
PHP Question

PDO Pagination row by row, stuck at next button and previous button

as what my title above, and from the previous page, i've solved my issue about pagination, but now i'm stuck at the next and previous button, i've searhed there are a lot of ways such using javascript and many more, i've tried but i can't display my data instead so here's my code so far :

if($typeuser == 'admin'){
$item_per_page = 1;
$dbcon = new PDO('mysql:host=localhost;dbname=laundry','root','');
$results = $dbcon->prepare("SELECT COUNT(*) FROM konsumen");
$get_total_rows = $results->fetch();

//breaking total records into pages
$pages = ceil($get_total_rows[0]/$item_per_page);

$page_number = filter_var($_GET["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
$page_number = 1;
$position = (($page_number-1) * $item_per_page);

$results = $dbcon->prepare("SELECT * FROM konsumen ORDER BY kode_pemesanan ASC LIMIT $position, $item_per_page");
while($row = $results->fetch()){
print_r ("<table>
<td>Kode Pemesanan</td><td>:</td><td>".$row['kode_pemesanan']."</td></tr>
<tr><td>Atas Nama</td><td>:</td><td>".$row['atas_nama']."</td></tr>
<tr><td>Jumlah Baju</td><td>:</td><td>".$row['jmlhbaju']."</td></tr>
<tr><td>Jumlah Celana</td><td>:</td><td>".$row['jmlhcelana']."</td></tr>
<tr><td>Jumlah Jaket</td><td>:</td><td>".$row['jmlhjaket']."</td></tr>
<tr><td>Jumlah Jas</td><td>:</td><td>".$row['jmlhjas']."</td></tr>
<tr><td>Jumlah Dress</td><td>:</td><td>".$row['jmlhdress']."</td></tr>
<tr><td>Pengurus Pesanan</td><td>:</td><td>".$row['penguruspesanan']."</td></tr>
<tr><td>Status Pesanan</td><td>:</td><td>".$row['statuspesanan']."</td></tr>
<tr><td>Total Pesanan</td><td>:</td><td>".$row['totalpesanan']."</td>

is there any way for solving the next and previous button ? can i build the next and previous button from php ?

And very big thanks for Jamie, he helps me so much for giving me the clue for php pagination.

Answer Source

From the looks of it, I think you just need buttons that have a dynamic link based on the current request page


// your code as is, dying if page not numeric

if(isset($_GET["page"]) ){
  $next = $page_number + 1;
  echo "<a href='?page=$next'>Next</a>";
} else "echo <a href='?page=2'>Next</a>";