Bagi meringkaskan paparan pagination. Sambungan dari post sebelum ini.
<table class="table table-hover table-condensed"> <thead> <tr> <th>title</th> <th>title</th> <th>title</th> </tr> </thead> <tbody> <?php $limit_rekod=200; //200 rekod akan dipaparkan dalam satu page $num_range = 3; //jumlah butang page bersebelahan dengan current button di kiri dan kanan. Jika current butang = 6. Kiri papar 3,4 dan 5. Kanan pula papar 7,8 dan 9. Selebihnya tidak dipaparkan. $num_limit = $num_range + 4; $paging_url = "viewdata.php"; if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * $limit_rekod; $sql = "SELECT .... LIMIT $start_from, $limit_rekod"; $result = mysql_query($sql,$con); while($row = mysql_fetch_array($result)) { ?> <tr> <td><?php echo $row['xxx']; ?></td> <td><?php echo $row['xxx']; ?></td> <td><?php echo $row['xxx']; ?></td> </tr> <?php } ?> </tbody> </table> <!-- mula untuk pagination --> <?php $sqlpg = "SELECT ...."; //select tanpa limit $rs_result = mysql_query($sqlpg,$con); $total_records = mysql_num_rows($rs_result); $total_pages = ceil($total_records / $limit_rekod); ?> <ul class="pagination mtm mbm"> <li class="<?php if($page == 1) echo "disabled"; ?>"><a href="<?php echo $paging_url; ?>?page=1">«</a></li> <?php if($total_pages < ($num_range)) { for ($i=1; $i<=$total_pages; $i++) { ?> <li class="<?php if($page == $i) echo "active"; ?>"><a href="<?php echo $paging_url; ?>?page=<?php echo $i; ?>"><?php echo $i; ?></a></li> <?php }; } elseif($total_pages > $num_limit) { if(($page - $num_range) > 1) { ?> <li class=""><a href="<?php echo $paging_url; ?>?page=<?php echo ($page - 1); ?>"><</a></li> <li class="disabled"><a href="">...</a></li> <?php } if(($page + $num_range) < $num_limit) { for ($i=1; $i<=($page+$num_limit); $i++) { ?> <li class="<?php if($page == $i) echo "active"; ?>"><a href="<?php echo $paging_url; ?>?page=<?php echo $i; ?>"><?php echo $i; ?></a></li> <?php }; }elseif(($page + $num_range) > $total_pages) { for ($i=$page - $num_range; $i<=$total_pages; $i++) { ?> <li class="<?php if($page == $i) echo "active"; ?>"><a href="<?php echo $paging_url; ?>?page=<?php echo $i; ?>"><?php echo $i; ?></a></li> <?php }; }else { for ($i=$page - $num_range; $i<=($page + $num_range); $i++) { ?> <li class="<?php if($page == $i) echo "active"; ?>"><a href="<?php echo $paging_url; ?>?page=<?php echo $i; ?>"><?php echo $i; ?></a></li> <?php }; } if(($page + $num_range) < $total_pages){ ?> <li class="disabled"><a href="">...</a></li> <li class=""><a href="<?php echo $paging_url; ?>?page=<?php echo ($page + 1); ?>">></a></li> <?php } } ?> <li class="<?php if($page == $total_pages) echo "disabled"; ?>"><a href="<?php echo $paging_url; ?>?page=<?php echo $total_pages; ?>">»</a></li> </ul>
Paparan yang terhasil seperti di bawah.
Jadi, paparan tidak jadi terlalu panjang. Jika ada 1 juta data pun, tidak dipaparkan sehingga 5 ribu butang. Hanya beberapa butang sahaja.