Blogger Information
Blog 49
fans 0
comment 4
visits 41969
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
完成员工信息表的分页显示功能
过儿的博客
Original
932 people have browsed it

分页完善

staff_list.php

实例

<?php
  $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');

  $pageNum = 3;
  $page = isset($_GET['p']) ? $_GET['p'] : 1;
   
  $stmt = $pdo->prepare('SELECT Count(*) FROM `staff`');
  $stmt->execute();
  $total = $stmt->fetchColumn(0);
  $pages = ceil($total / $pageNum);
  $offset = ($page -1)*$pageNum;


$sql="SELECT * FROM `staff` LIMIT {$offset},{$pageNum}";
 $stmt = $pdo->prepare($sql);
$stmt->execute();
$staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$pdo = null;
$tableTitle = '员工信息表';

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php echo $title; ?></title>
    <style>
        table,th,td {
            border: 1px solid #666;
            padding: 8px;
        }
        table {
            border-collapse: collapse;
            width: 80%;
            text-align: center;
            margin: 30px auto;
        }
        thead tr:first-of-type {
            background-color: lightblue;
        }

        tbody tr:hover {
            background-color: #efefef;
        }

        table > caption {
            font-size: 1.2rem;
            margin-bottom: 15px;
        }
        table + p {
            text-align: center;
        }

        button:hover {
            cursor: pointer;
            background-color: lightblue;
        }

        /*添加按钮给个特殊样式*/
        #add {
            height: 25px;
            width: 90px;
            position: absolute;
            left: 650px;
            top: 40px;
        }

    </style>
</head>
<body>
<button onclick="location.href='staff_add.php'" id="add">添加</button>


<table>
    <caption>
        <?php
        echo '<span style="color:red">' . $tableTitle . '</span>';
        ?>
    </caption>
    <thead>
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>职务</th>
        <th>手机</th>
        <td>入职</td>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>

    <!--foreach()替代语法-->
    <?php foreach($staffs as $staff) : ?>
        <tr>

            <td><?php echo $staff['id']; ?></td>
            <td><?php echo $staff['name']; ?></td>


            <td><?php echo $staff['age']; ?></td>

            <!--if()替代语法-->
            <td>
                <?php if($staff['sex'] == 1) : ?>
                    男
                <?php else: ?>
                    女
                <?php endif; ?>
            </td>


            <!--如果只是简单的输出变量可以使用php短标签语法-->
            <td><?=$staff['position']?></td>
            <td><?=$staff['mobile']?></td>

            <td>
                <?php
                echo date('Y/m/d',$staff['hiredate']);
                ?>
            </td>

            <td>
                <button onclick="location.href='staff_edit.php?id=<?php echo $staff['id']; ?>'">编辑</button>
                <button onclick="return confirm('是否删除?') ? location.assign('staff_manage.php?action=del&id=<?=$staff['id']?>') : false">删除</span></button>
            </td>

        </tr>
    <?php endforeach;?>

    </tbody>
</table>
<p style="margin:0 center;">
<a href="staff_list.php?p=1">首页</a>
<?php for($i=1;$i<$pages;$i++): ?>
    <?php if(isset($_GET['p']) && $_GET['p'] == $i):
        $bgColor = 'style= "background-color:red"';
    else:
        $bgColor = '';
     endif; ?>
   <a href="javascript:location.href='<?=$_SERVER['PHP_SELF']?>?p=<?=$i?>'" <?=$bgColor?>><?=$i?></a>
<?php endfor; ?>
<a href="staff_list.php?p=<?=$pages?>">尾页</a>
</p>
</body>
</html>

运行实例 »

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

1.png

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