PHP+MySqli EP.7 แบ่งหน้าการแสดงข้อมูลออกเป็นหน้าๆ (Simple Pagination)
PHP+MySqli EP.7 แบ่งหน้าการแสดงข้อมูลออกเป็นหน้าๆ
(Simple Pagination) เป็นการเรียกแสดงข้อมูลใน database
ซึ่งกำหนดการแบ่งหน้าไว้แล้ว เพื่อง่ายต่อการดูข้อมูล
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
<?php $conn= mysqli_connect("localhost","root","","sqli_1") or die("Error: " . mysqli_error($conn)); mysqli_query($conn, "SET NAMES 'utf8' "); $query=mysqli_query($conn,"SELECT COUNT(member_id) FROM `tbl_member` "); $row = mysqli_fetch_row($query); $rows = $row[0]; $page_rows = 5; //จำนวนข้อมูลที่ต้องการให้แสดงใน 1 หน้า ตย. 5 record / หน้า $last = ceil($rows/$page_rows); if($last < 1){ $last = 1; } $pagenum = 1; if(isset($_GET['pn'])){ $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']); } if ($pagenum < 1) { $pagenum = 1; } else if ($pagenum > $last) { $pagenum = $last; } $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; $nquery=mysqli_query($conn,"SELECT * from tbl_member ORDER BY member_id DESC $limit"); $paginationCtrls = ''; if($last != 1){ if ($pagenum > 1) { $previous = $pagenum - 1; $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'" class="btn btn-info">Previous</a> '; for($i = $pagenum-4; $i < $pagenum; $i++){ if($i > 0){ $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'" class="btn btn-primary">'.$i.'</a> '; } } } $paginationCtrls .= ''.$pagenum.' '; for($i = $pagenum+1; $i <= $last; $i++){ $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'" class="btn btn-primary">'.$i.'</a> '; if($i >= $pagenum+4){ break; } } if ($pagenum != $last) { $next = $pagenum + 1; $paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'" class="btn btn-info">Next</a> '; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> <link href="css/cite.css" rel="stylesheet"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!-- Popper JS --> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script> <link rel="stylesheet" href="css/font-awesome.min.css"> <!-- Add jQuery library --> <script type="text/javascript" src="lib/jquery-1.10.1.min.js"></script> </head> <body> </br> </br> <div class="container"> <div class="row"> <div class="col-md-2"> </div> <div class="col-md-12"> <table width="100%" class="table table-striped table-bordered table-hover"> <thead> <tr class="info"> <th rowspan="2">ลำดับ</th> <th rowspan="2">ชื่อจริง</th> <th rowspan="2">นามสกุล</th> <th rowspan="2">ชื่อผู้ใช้</th> </tr> </thead> <tbody> <?php while($crow = mysqli_fetch_array($nquery)){ ?> <?php @$num++;?> <tr> <td><?php echo $num; ?></td> <td><?php echo $crow['member_firstname']; ?></td> </td> <td><?php echo $crow['member_lastname']; ?></td> <td><?php echo $crow['member_username']; ?></td> </tr> <?php } ?> </tbody> </table> <center><div id="pagination_controls"><?php echo $paginationCtrls; ?></div></center> </div> <div class="col-md-2"> </div> </div> </div> </body> </html> |