时间:2021-07-01 10:21:17 帮助过:6人阅读
$con = mysql_connect("localhost","root","123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("task", $con);
mysql_query("set names utf8");
$result = mysql_query("SELECT * FROM tesk limit 0,10 ");
echo "
time | id | name | task | remark |
---|---|---|---|---|
" . $row['date'] . " | ";" . $row['id'] . " | ";" . $row['name'] . " | ";" . $row['task'] . " | ";" . $row['remark'] . " | ";
class PageTool{
protected $total=0;
protected $perpage=10;
protected $page=1;
/*构造函数的参数分别为商品总条目数,每页多少条目,当前页*/
public function __construct($total,$page=false,$perpage=false){
$this->total = $total;
if($perpage){
$this->perpage = $perpage;
}
if($page){
$this->page = $page;
}
}
public function show(){
$cnt = ceil($this->total/$this->perpage);//$cnt表示一共需要多少页
$uri = $_SERVER['REQUEST_URI']; //从地址栏获取URL
$parse = parse_url($uri); //将获取的URL变成特定数组
//print_r($parse);exit;
$param = array();
if(isset($parse['query'])){ //如果地址栏传了参数(也就是url?之后的字符串),则获取参数放入$param数组中
parse_str($parse['query'],$param);
}
/*不管地址栏有没有传page参数,都要把page从$param中剔除
因为page参数是需要计算出来的,不能定死*/
unset($param['page']);
$url = $parse['path'].'?';
if(!empty($param)){
$param = http_build_query($param); //把数组$param再变成字符串,这一次已经剔除了page参数
$url = $url.$param.'&'; //重新拼凑$url,最后的&符号用于连接计算出的page
}
/*
关键部分:计算页码导航
此处逻辑有点复杂,多思考下
*/
$nav = array(); //用于存放最终地址栏链接
$nav[0] = ''.$this->page.''; //把当前地址栏的page参数放入$nav数组
for($left=$this->page-1,$right=$this->page+1;
($left>=1
------解决思路----------------------
$right<=$cnt)&&count($nav)<=5;){
if($left>=1){
array_unshift($nav, ''.'['.$left.']'.'');
$left-=1;
}
if($right<=$cnt){
array_push($nav, ''.'['.$right.']'.'');
$right+=1;
}
}
return implode('', $nav);
}
}
/*
测试分页类效果
$page = isset($_GET['page'])?$_GET['page']:1;
$pagetool = new PageTool(20,$page,6);
echo $pagetool->show();
*/