Blogger Information
Blog 29
fans 0
comment 1
visits 18720
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分页实例(补4月27日作业)-2018年5月6日
小小的菜的博客
Original
494 people have browsed it

实例

<!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>上下页、首页、尾页</title>
    <style type="text/css">
        table,th,td {
            border:1px solid black;
        }
        table th{
            background-color:gray;
        }
        table {
            border-collapse:collapse;
            width:70%;
            margin:30px auto;
            text-align: center;
        }
        div {
            text-align: center;
        }
        div a {
            text-decoration: none;
            margin-left:10px;
        }
        div a:hover, .active {
            background-color: gray;
            color:white;
        }
        form {
            display: inline;
        }
    </style>
</head>
<body>
<?php
$db = mysqli_connect('127.0.0.1','root','root');
mysqli_select_db($db,'php');
$num = 5;
$page = isset($_GET['p']) ? $_GET['p'] : 1;
$offset = ($page -1) * $num;
$sql = "SELECT * FROM staff LIMIT {$offset},{$num}";
$res = mysqli_query($db,$sql);
$rows = mysqli_fetch_all($res,MYSQLI_ASSOC);
$number = mysqli_query($db,"SELECT COUNT(*) FROM staff");
list($total) = mysqli_fetch_row($number);
$pages = ceil($total / $num);

?>
<table>
    <caption><h2>员工信息表</h2></caption>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <?php foreach ($rows as $row): ?>
        <tr>
            <td><?php echo $row['staff_id'] ?></td>
            <td><?php echo $row['name'] ?></td>
            <td><?php echo $row['sex'] ?></td>
            <td><?php echo $row['age'] ?></td>
            <td><?php echo $row['salary'] ?></td>
        </tr>
    <?php endforeach;?>
</table>

<div>
    <?php if($page !=1): ?>
    <a href="http://127.0.0.1/front/0427/demo4.php?p=1">首页</a>
    <a href="http://127.0.0.1/front/0427/demo4.php?p=
<?php echo (($page-1)==0) ? 1: ($page-1);
    ?>
           ">上一页</a><?php endif; ?>
    <?php for($i=1; $i<=$pages; $i++): ?>
        <a class="<?php if($page == $i) echo 'active'; ?>" href="http://127.0.0.1/front/0427/demo4.php?p=<?php echo $i; ?>"><?php echo $i; ?></a>
    <?php endfor?>
    <?php if($page !=$pages): ?>
    <a href="http://127.0.0.1/front/0427/demo4.php?p=
<?php echo (($page+1)>$pages) ? $pages : ($page+1);
    ?>
           ">下一页</a>
    <a href="http://127.0.0.1/front/0427/demo4.php?p=<?php echo $pages ?>">尾页</a>
    <?php endif; ?>

    <form action="" method="get">
    第
    <select name="p" id="">
        <?php for($i=1; $i<=$pages; $i++): ?>
        <option value="<?php echo $i; ?>" <?php if($page == $i) echo 'selected' ?>><?php echo $i; ?></option>
        <?php endfor ?>

    </select>
    页
    <button>提交</button>
    </form>
</div>


</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post