• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

Hướng dẫn phân trang bảng PHP sử dụng MySQLi

Admin

Well-Known Member
Staff member
Administrator
Chào các bạn, hôm nay mình sẽ chia sẻ cho các bạn code phân trang sử dụng PHP và MySQLi
49651013748_3358fa9a97_o.png

1. Tạo bảng
SQL:
CREATE TABLE IF NOT EXISTS `phantrang` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `ten` varchar(250) NOT NULL,
  `tuoi` int(10) NOT NULL,
  `diachi` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
2. Chèn dữ liệu vào bảng
SQL:
insert  into `phantrang`(`ten`,`tuoi`,`diachi`) values
('Nguyễn Văn A','16','Hà Nội'),('Nguyễn Văn B','20','Hà Giang'),('Nguyễn Văn C','20','Thành phố Hồ Chí Minh'),('Nguyễn Văn D','20','Yên Bái'), ('Nguyễn Văn Đ','20','Nghệ An'),('Nguyễn Văn E','30','Cần Thơ'),('Nguyễn Văn F','30','Đà Nẵng'),('Nguyễn Văn G','25','Hải Phòng'),('Nguyễn Văn H','25','Bạc Liêu'),('Nguyễn Văn I','25','Sóc Trăng'),('Nguyễn Thị K','18','Cà Mau'),('Trần Thị L','17','Cà Mau')
2. Tạo file dulieu.php để kết nối tới cơ sở dữ liệu
PHP:
<?php
$con = mysqli_connect("localhost","Tên database","Mật khẩu user","User");
mysqli_set_charset($con,"utf8");
    if (mysqli_connect_errno()){
        echo "Khong the ket noi: " . mysqli_connect_error();
        die();
        }
?>
3. Code phân trang (Ở đây mình để Order By và DESC để sắp xếp dữ liệu theo id giảm dần, bạn có thể chọn ASC để sắp xếp theo thứ tự tăng dần nhé)
PHP:
<html>
<head>
<title>Phân trang chuyên nghiệp bởi tuoitreit.vn</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div style="width:1000px; margin:0 auto;">

<h3>Phân trang chuyên nghiệp bởi tuoitreit.vn</h3>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th style='width:50px;'>STT</th>
<th style='width:150px;'>Tên</th>
<th style='width:50px;'>Tuổi</th>
<th style='width:150px;'>Địa chỉ</th>
</tr>
</thead>
<tbody>
<?php
include('dulieu.php');

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

    $total_records_per_page = 2;
    $offset = ($trang-1) * $total_records_per_page;
    $previous_page = $trang - 1;
    $next_page = $trang + 1;
    $adjacents = "2";

    $result_count = mysqli_query($con,"SELECT COUNT(*) As total_records FROM `phantrang`");
    $total_records = mysqli_fetch_array($result_count);
    $total_records = $total_records['total_records'];
    $total_no_of_pages = ceil($total_records / $total_records_per_page);
    $second_last = $total_no_of_pages - 1; // total page minus 1

    $result = mysqli_query($con,"SELECT * FROM `phantrang` order by id DESC LIMIT $offset, $total_records_per_page");
    while($row = mysqli_fetch_array($result)){
        echo "<tr>
              <td>".$row['id']."</td>
              <td>".$row['ten']."</td>
               <td style=word-break:break-all;>".$row['tuoi']."</td>
                 <td>".$row['diachi']."</td>
                 </tr>";
        }
    mysqli_close($con);
    ?>
</tbody>
</table>

<div style='padding: 10px 20px 0px; border-top: dotted 1px #CCC;'>
<strong>Trang <?php echo $trang." của ".$total_no_of_pages; ?></strong>
</div>

<ul class="pagination">
    <?php // if($trang > 1){ echo "<li><a href='?trang=1'>Trang đầu tiên</a></li>"; } ?>
 
    <li <?php if($trang <= 1){ echo "class='disabled'"; } ?>>
    <a <?php if($trang > 1){ echo "href='?trang=$previous_page'"; } ?>>Trang trước</a>
    </li>
   
    <?php
    if ($total_no_of_pages <= 10){    
        for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
            if ($counter == $trang) {
           echo "<li class='active'><a>$counter</a></li>";
                }else{
           echo "<li><a href='?trang=$counter'>$counter</a></li>";
                }
        }
    }
    elseif($total_no_of_pages > 10){
     
    if($trang <= 4) {        
     for ($counter = 1; $counter < 8; $counter++){      
            if ($counter == $trang) {
           echo "<li class='active'><a>$counter</a></li>";
                }else{
           echo "<li><a href='?trang=$counter'>$counter</a></li>";
                }
        }
        echo "<li><a>...</a></li>";
        echo "<li><a href='?trang=$second_last'>$second_last</a></li>";
        echo "<li><a href='?trang=$total_no_of_pages'>$total_no_of_pages</a></li>";
        }

     elseif($trang > 4 && $trang < $total_no_of_pages - 4) {      
        echo "<li><a href='?trang=1'>1</a></li>";
        echo "<li><a href='?trang=2'>2</a></li>";
        echo "<li><a>...</a></li>";
        for ($counter = $trang - $adjacents; $counter <= $trang + $adjacents; $counter++) {        
           if ($counter == $trang) {
           echo "<li class='active'><a>$counter</a></li>";
                }else{
           echo "<li><a href='?trang=$counter'>$counter</a></li>";
                }              
       }
       echo "<li><a>...</a></li>";
       echo "<li><a href='?trang=$second_last'>$second_last</a></li>";
       echo "<li><a href='?trang=$total_no_of_pages'>$total_no_of_pages</a></li>";  
            }
     
        else {
        echo "<li><a href='?trang=1'>1</a></li>";
        echo "<li><a href='?trang=2'>2</a></li>";
        echo "<li><a>...</a></li>";

        for ($counter = $total_no_of_pages - 6; $counter <= $total_no_of_pages; $counter++) {
          if ($counter == $trang) {
           echo "<li class='active'><a>$counter</a></li>";
                }else{
           echo "<li><a href='?trang=$counter'>$counter</a></li>";
                }                
                }
            }
    }
?>
 
    <li <?php if($trang >= $total_no_of_pages){ echo "class='disabled'"; } ?>>
    <a <?php if($trang < $total_no_of_pages) { echo "href='?trang=$next_page'"; } ?>>Tiếp</a>
    </li>
    <?php if($trang < $total_no_of_pages){
        echo "<li><a href='?trang=$total_no_of_pages'>Trang sau &rsaquo;&rsaquo;</a></li>";
        } ?>
</ul>
</div>
</body>
</html>
Đây là link tải style:
Hidden content
You need to react to this post in order to see this content.
Chúc bạn thành công
 

Facebook Comments

New posts New threads New resources

Back
Top