Correction status:qualified
Teacher's comments:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>设备列表</title> <link rel="stylesheet" type="text/css" href="css/main.css"/> </head> <body> <?php require 'lib/func_pdo.php'; require 'lib/func_paging.php'; $pdo = connect(); // 分页变量设置 $page = isset($_GET['page']) ? $_GET['page']:1; // 默认设置为第一页 $page = $page<=0 ? 1:$page; //如果get提交的page参数值小于等于0,设置默认页为1 // 每页显示的行数 $page_rownum = 5; //获取返回数据和总行数 list($data,$rownum) = paging($pdo,'road','*',$where,'id','asc',$page,$page_rownum); // 计算总页数 $pages = ceil($rownum/$page_rownum); ?> <div class="main"> <table width="100%"> <tr> <th>ID</th> <th>路口名称</th> <th>东</th> <th>南</th> <th>西</th> <th>北</th> <th>信号机</th> <th>球机</th> <th>工控机</th> <th>操作</th> </tr> <?php if ($data){ foreach ($data as $key=>$value){ echo '<tr>'; echo '<td>'.$value['id'].'</td>'; echo '<td>'.$value['road_name'].'</td>'; echo '<td style="text-align: center">'.$value['east_ip'].'</td>'; echo '<td style="text-align: center">'.$value['south_ip'].'</td>'; echo '<td style="text-align: center">'.$value['west_ip'].'</td>'; echo '<td style="text-align: center">'.$value['north_ip'].'</td>'; echo '<td style="text-align: center">'.$value['signal_ip'].'</td>'; echo '<td style="text-align: center">'.$value['ball_ip'].'</td>'; echo '<td style="text-align: center">'.$value['pc_ip'].'</td>'; echo "<td style='text-align: center'><a href='?id={$value['id']}'>修改</a> <a href='?id={$value['id']}'>删除</a></td>"; echo '</tr>'; } } ?> </table> <p> <!-- 首页、上一页--> <?php if($page>1):?> <a href="http://www.p.com:81/main.php?page=1">首页</a> <a href="http://www.p.com:81/main.php?page=<?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://www.p.com:81/main.php?page=<?php echo $i ?>"><?php echo $i ?></a> <?php endfor ?> <!-- 下一页、尾页--> <?php if($page<$pages):?> <a href="http://www.p.com:81/main.php?page=<?php echo ($page+1)>$pages? $pages:($page+1) ?>">下一页</a> <a href="http://www.p.com:81/main.php?page=<?php echo $pages ?>">尾页</a> <?php endif; ?> </p> </div> </body> </html>
点击 "运行实例" 按钮查看在线实例
<?php if (!function_exists(paging)) { function paging($pdo, $table, $fields = '*', $where = '', $order, $sort, $page = '1', $page_rownum = '5') { $offset = ($page - 1) * $page_rownum; $limit = "$offset,$page_rownum"; $sql = 'SELECT '; if (is_array($fields)) { foreach ($fields as $field) { $sql .= $field . ','; } } else { $sql .= $fields; } $sql = rtrim(trim($sql), ','); $sql .= " FROM {$table}"; if (!empty($where)) { $sql .= " WHERE {$where}"; } $sql .= " ORDER BY {$order} {$sort} LIMIT {$limit} "; $sql2 = "SELECT count(*) from {$table};"; $sql = rtrim(trim($sql), ',') . ';'; $stmt = $pdo->prepare($sql); $stmt2 = $pdo ->prepare($sql2); // 执行 if ($stmt->execute() && $stmt2->execute()) { if ($stmt->rowCount() > 0) { $stmt->setFetchMode(PDO::FETCH_ASSOC); $data = $stmt->fetchAll(); $row = $stmt2->fetchAll(); $row = $row[0]['count(*)']; return [$data,$row]; } } else { return false; } } } ?>
点击 "运行实例" 按钮查看在线实例