php+bootstrap的原生分页

自己随便写的php原生分页
config.php



//配置项 pdo连接
$dsn = "mysql:host=localhost;dbname=www.hm.com";
$username = "hmcom";
$password = "123456";
$pdo = new PDO($dsn,$username,$password);
//设置字符集
$pdo->exec('set names utf8');
//设置pdo异常类处理
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//设置pdo数据集返回状态
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);

index.php


//pdo原生分页
header("content-type:text/html;charset=utf-8");
//config里面是pdo数据库连接
include "config.php";

$pdo->beginTransaction();
try{
     
    //记录总数
    $sqlcount = "select count(*) from `user` where id > 0 ";
    $smt2 = $pdo->prepare($sqlcount);
    $smt2->execute();
    $count = $smt2->fetchColumn();
    //    echo $count;
    //每页展示多少条
    $pagenum = 2;
    //总的页数
    $lastpage = ceil($count / $pagenum);
    //当前页
    $page = $_GET['p']<=1 ? 1: $_GET['p'];

    $page = $_GET['p']>=$lastpage ? $lastpage : $page;
    //上一页
    $prevpage = ($page - 1) <= 1 ? 1: ($page - 1);
    //下一页
    $nextpage = ($page + 1) >= $lastpage ? $lastpage: ($page + 1);

	//开始查询的基数
    $start = ($page - 1)*$pagenum;
	//第一页是 1 (1-1)*2 ->  limit 0,2 
	//第二页是 2 (2-1)*2->  limit 2,2 
	//第三页是 3 (3-1)*2 ->  limit 4,2 
    $sqlall = "SELECT *FROM `user` WHERE id > 0 ORDER BY id ASC LIMIT $start,$pagenum";
    //pdo预处理
    $smt = $pdo->prepare($sqlall);
    //执行预处理语句
    $smt->execute();
    $result = $smt->fetchAll();
    $pdo->commit();

}catch (PDOException $pdoex){
     
    echo $pdoex->getMessage();
    echo '出错啦';
    $pdo->rollBack();
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</head>
<body>
    <div class="contaniner container-lg">
        <div class="page-header">
            <h2>用户列表</h2>
        </div>
        <table class="table">
            <thead class="thead-dark">
                <tr>
                    <th scope="col">编号</th>
                    <th scope="col">用户名</th>
                    <th scope="col">年龄</th>
                    <th scope="col">资产</th>
                </tr>
            </thead>
            <tbody>
                
                    foreach ($result as $k => $row){
     
                ?>
                <tr>
                    <th scope="row">$row['id']?></th>
                    <td>$row['name']?></td>
                    <td>$row['age']?></td>
                    <td>$row['money']?></td>
                </tr>
                
                    }
                ?>
            </tbody>
        </table>
        <nav aria-label="Page navigation ">
            <ul class="pagination">
                <li class="page-item"><a class="page-link" href="index.php?p=1">第一页</a></li>
                <li class="page-item $page <= 1){?>disabled">
                    <a class="page-link" href="index.php?p=$prevpage?>" tabindex="-1" if($page <= 1){
     ?>aria-disabled="true"}?>>上一页</a>
                </li>
                <!--这里没有写的那么细致了-->
                
                    if($page >= 1){
     
                            for($i = $page;$i< $page+ 2;$i++){
     
                ?>
                <li class="page-item">
                    
                        if($i <= $lastpage ){
     
                    ?>
                    <a class="page-link" href="index.php?p=$i?>">$i?></a>
                    }?>
                </li>
                
                            }}
                ?>
                <li class="page-item">
                    <a class="page-link" href="index.php?p=$nextpage?>">下一页</a>
                </li>
                <li class="page-item"><a class="page-link" href="index.php?p=$lastpage?>">尾页</a></li>
                <buttom style="border: 1px solid #ccc;" class="btn btn-info" href="#">$page?>/$lastpage?></buttom>
            </ul>
        </nav>
    </div>
</body>
</html>

php+bootstrap的原生分页_第1张图片

php+bootstrap的原生分页_第2张图片
php+bootstrap的原生分页_第3张图片

不足之处希望大佬提提意见。

你可能感兴趣的:(php开发,php,bootstrap,列表)