<?php
header('content-type:text/html;charset=utf-8');
//php中header的总结
class Sep_page{
private $page = 3;
private $ip = 'localhost',$db_name = 'root',$db_pass = '199486',$db_table = null;
public function __construct($config = array()){
$this->page = $config['page'];
$this->ip = $config['ip'];
$this->db_name = $config['db_name'];
$this->db_pass = $config['db_pass'];
$this->db_table = $config['db_table'];
}
public function judge_page(){
$page = $this->page;
$para = self::connect_mysqli(); //类中方法调用方法用self
//print_r($para['rows'][0]['id']);
$total_page = ceil($para['total']/10);
//得到总页数
$p1 = $page - 1;
$p2 = $page + 1;
//上下页的URL传参
$start = 1;
$end = $total_page;
//初始化开始,结束页
$show_page = 5;
//页面显示数字按钮数
$offset_page = ($show_page-1)/2;
//点击按钮左右按钮的数量
$page_banner = "";
//通过字符串拼接分页
if($page > 1){
$page_banner.= "<li><a href='".$_SERVER['PHP_SELF']."?id=1'>首页</a></li>";
$page_banner.= "<li><a href='".$_SERVER['PHP_SELF']."?id=$p1'>上一页</a></li>";
}
//$_SERVER['PHP_SELF'] 显示当前文件路径
//页数大一1才显示首页和上一页 judge/判断
if($total_page > $show_page){
//先判断总页数与显示页数的关系
if($page>$offset_page){
//开始的页码为页数减去偏移量
$start = $page - $offset_page;
$end = $total_page>$page+$offset_page?$page+$offset_page:$total_page;
}else{
$start = 1;
$end = $show_page;
}
if($page+$offset_page>$total_page){
$start = $start - ($page + $offset_page - $total_page);
}
}
for($i = $start;$i<=$end;$i++){
$page_banner .="<li><a href='".$_SERVER['PHP_SELF']."?id=$i'>$i</a></li>";
}
if($page < $total_page){
$page_banner.="<li><a href='".$_SERVER['PHP_SELF']."?id=$p2'>下一页</a></li>";
$page_banner.= "<li><a href='".$_SERVER['PHP_SELF']."?id=$total_page'>尾页</a></li>";
}//页数小于总页数才显示尾页和下一页
$page_banner.="<li>共{$total_page}页</li>";
$response = array('rows' => $para['rows'], '$string'=>$page_banner);
return $response;
}
private function connect_mysqli(){
$mysqli = new mysqli($this->ip,$this->db_name,$this->db_pass,$this->db_table);
$sql = "SELECT * FROM page LIMIT ".(($this->page-1)*10).",10";
//var_dump($this->mysqli);
//echo "<hr>";
//echo $sql;
$mysqli_result = $mysqli->query($sql);
//var_dump($mysqli_result);
while($row = $mysqli_result->fetch_assoc()){
$rows[] = $row;
}
//var_dump($row);
//var_dump($rows);
$sql_count = "SELECT COUNT(*) FROM page";
//得到数据表数据总数
$mysqli_result = $mysqli->query($sql_count);
$row = $mysqli_result->fetch_array();
//print_r($row); //$row返回的是一个关联加索引的数组,第一项就存储的数据总数,因此需要$row[0]来取
$total = $row[0];
$return = array('rows' => $rows, 'total'=>$total);
return $return;
}
}
$config = $arrayName = array('page'=>$_GET['id'],'ip'=> 'localhost','db_name'=>'root','db_pass'=>'199486','db_table'=>'article');
$weidapao = new Sep_page($config);
//print_r($rows = $weidapao->judge_page());
$rows = $weidapao->judge_page();
$number = $rows['rows']
//print_r($weidapao->connect_mysqli());
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
>
</head>
<body>
<table class="table table-striped">
<?php foreach($number as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['username']; ?></td>
</tr>
<?php endforeach;?>
</table>
<ul class="pagination">
<?php echo $rows['$string'];?>
</ul>
</body>
</html>测试是没有问题的,但是我觉得肯定有细节问题需要改进,希望有人能提出怎么改,为什么,具体代码,谢谢
添加回答
举报
0/150
提交
取消