当前位置:Gxlcms > 数据库问题 > 1、查询数据库并分页显示出来

1、查询数据库并分页显示出来

时间:2021-07-01 10:21:17 帮助过:8人阅读

<html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 <title>无标题文档</title> 5 </head> 6 7 <body> 8 9 10 <?php 11 12 //加载类 13 include("haoyou.php"); 14 include("xwfy.php"); 15 16 //造对象 17 $db = new haoyou(); 18 $sqlcount = "select count(*) from news"; 19 $attrc = $db->Query($sqlcount); 20 $total = $attrc[0][0]; 21 22 23 //造分页对象 24 $page = new Page($total,3,""); 25 26 $page->set(‘head‘,‘条数据‘); 27 //$page->set(‘prev‘,"<img src=‘0063.png‘ width=‘15‘ height=‘15‘ />"); 28 $page->set(‘first‘,‘第一页‘); 29 30 //显示 31 $sql = "select * from news ".$page->limit; 32 33 $attr = $db->Query($sql); 34 35 for($i=0;$i<count($attr);$i++) 36 { 37 38 echo "<h1>{$attr[$i][1]}-".date("Y-m-d",time())."</h1>"; 39 40 echo "<h3>{$attr[$i][2]}</h3><a href=‘www.baidu.com‘>详情</a><img src=‘pic/0063.png‘ width=‘25‘ height=‘25‘><hr></hr>"; 41 } 42 43 44 45 ?> 46 47 <?php 48 echo $page->fpage(); 49 ?> 50 </body> 51 </html> 52 53 //写分页方法 54 <?php 55 /** 56 file: page.class.php 57 完美分页类 Page 58 */ 59 class Page { 60 private $total; //数据表中总记录数 61 private $listRows; //每页显示行数 62 private $limit; //SQL语句使用limit从句,限制获取记录个数 63 private $uri; //自动获取url的请求地址 64 private $pageNum; //总页数 65 private $page; //当前页 66 private $config = array( 67 ‘head‘ => "条记录", 68 ‘prev‘ => "上一页", 69 ‘next‘ => "下一页", 70 ‘first‘=> "首页", 71 ‘last‘ => "末页" 72 ); 73 //在分页信息中显示内容,可以自己通过set()方法设置 74 private $listNum = 3; //默认分页列表显示的个数 75 76 /** 77 构造方法,可以设置分页类的属性 78 @param int $total 计算分页的总记录数 79 @param int $listRows 可选的,设置每页需要显示的记录数,默认为25条 80 @param mixed $query 可选的,为向目标页面传递参数,可以是数组,也可以是查询字符串格式 81 @param bool $ord 可选的,默认值为true, 页面从第一页开始显示,false则为最后一页 82 */ 83 public function __construct($total, $listRows=3, $query="", $ord=true){ 84 $this->total = $total; 85 $this->listRows = $listRows; 86 $this->uri = $this->getUri($query); 87 $this->pageNum = ceil($this->total / $this->listRows); 88 /*以下判断用来设置当前面*/ 89 if(!empty($_GET["page"])) { 90 $page = $_GET["page"]; 91 }else{ 92 if($ord) 93 $page = 1; 94 else 95 $page = $this->pageNum; 96 } 97 98 if($total > 0) { 99 if(preg_match(‘/\D/‘, $page) ){ 100 $this->page = 1; 101 }else{ 102 $this->page = $page; 103 } 104 }else{ 105 $this->page = 0; 106 } 107 108 $this->limit = "LIMIT ".$this->setLimit(); 109 } 110 111 /** 112 用于设置显示分页的信息,可以进行连贯操作 113 @param string $param 是成员属性数组config的下标 114 @param string $value 用于设置config下标对应的元素值 115 @return object 返回本对象自己$this, 用于连惯操作 116 */ 117 function set($param, $value){ 118 if(array_key_exists($param, $this->config)){ 119 $this->config[$param] = $value; 120 } 121 return $this; 122 } 123 124 /* 不是直接去调用,通过该方法,可以使用在对象外部直接获取私有成员属性limit和page的值 */ 125 function __get($args){ 126 if($args == "limit" || $args == "page") 127 return $this->$args; 128 else 129 return null; 130 } 131 132 /** 133 按指定的格式输出分页 134 @param int 0-7的数字分别作为参数,用于自定义输出分页结构和调整结构的顺序,默认输出全部结构 135 @return string 分页信息内容 136 */ 137 function fpage(){ 138 $arr = func_get_args(); 139 140 $html[0] = "<span class=‘p1‘>&nbsp;共<b> {$this->total} </b>{$this->config["head"]}&nbsp;</span>"; 141 $html[1] = "&nbsp;本页 <b>".$this->disnum()."</b> 条&nbsp;"; 142 $html[2] = "&nbsp;本页从 <b>{$this->start()}-{$this->end()}</b> 条&nbsp;"; 143 $html[3] = "&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;"; 144 $html[4] = $this->firstprev(); 145 $html[5] = $this->pageList(); 146 $html[6] = $this->nextlast(); 147 $html[7] = $this->goPage(); 148 149 $fpage = ‘<div style="font:12px \‘\5B8B\4F53\‘,san-serif;">‘; 150 if(count($arr) < 1) 151 $arr = array(0,1,2,3,4,5,6,7); 152 153 for($i = 0; $i < count($arr); $i++) 154 $fpage .= $html[$arr[$i]]; 155 156 $fpage .= ‘</div>‘; 157 return $fpage; 158 } 159 160 /* 在对象内部使用的私有方法,*/ 161 private function setLimit(){ 162 if($this->page > 0) 163 return ($this->page-1)*$this->listRows.", {$this->listRows}"; 164 else 165 return 0; 166 } 167 168 /* 在对象内部使用的私有方法,用于自动获取访问的当前URL */ 169 private function getUri($query){ 170 $request_uri = $_SERVER["REQUEST_URI"]; 171 $url = strstr($request_uri,‘?‘) ? $request_uri : $request_uri.‘?‘; 172 173 if(is_array($query)) 174 $url .= http_build_query($query); 175 else if($query != "") 176 $url .= "&".trim($query, "?&"); 177 178 $arr = parse_url($url); 179 180 if(isset($arr["query"])){ 181 parse_str($arr["query"], $arrs); 182 unset($arrs["page"]); 183 $url = $arr["path"].‘?‘.http_build_query($arrs); 184 } 185 186 if(strstr($url, ‘?‘)) { 187 if(substr($url, -1)!=‘?‘) 188 $url = $url.‘&‘; 189 }else{ 190 $url = $url.‘?‘; 191 } 192 193 return $url; 194 } 195 196 /* 在对象内部使用的私有方法,用于获取当前页开始的记录数 */ 197 private function start(){ 198 if($this->total == 0) 199 return 0; 200 else 201 return ($this->page-1) * $this->listRows+1; 202 } 203 204 /* 在对象内部使用的私有方法,用于获取当前页结束的记录数 */ 205 private function end(){ 206 return min($this->page * $this->listRows, $this->total); 207 } 208 209 /* 在对象内部使用的私有方法,用于获取上一页和首页的操作信息 */ 210 private function firstprev(){ 211 if($this->page > 1) { 212 $str = "&nbsp;<a href=‘{$this->uri}page=1‘>{$this->config["first"]}</a>&nbsp;"; 213 $str .= "<a href=‘{$this->uri}page=".($this->page-1)."‘>{$this->config["prev"]}</a>&nbsp;"; 214 return $str; 215 } 216 217 } 218 219 /* 在对象内部使用的私有方法,用于获取页数列表信息 */ 220 private function pageList(){ 221 $linkPage = "&nbsp;<b>"; 222 223 $inum = floor($this->listNum/2); 224 /*当前页前面的列表 */ 225 for($i = $inum; $i >= 1; $i--){ 226 $page = $this->page-$i; 227 228 if($page >= 1) 229 $linkPage .= "<a href=‘{$this->uri}page={$page}‘>{$page}</a>&nbsp;"; 230 } 231 /*当前页的信息 */ 232 if($this->pageNum > 1) 233 $linkPage .= "<span style=‘padding:1px 2px;background:#BBB;color:white‘>{$this->page}</span>&nbsp;"; 234 235 /*当前页后面的列表 */ 236 for($i=1; $i <= $inum; $i++){ 237 $page = $this->page+$i; 238 if($page <= $this->pageNum) 239 $linkPage .= "<a href=‘{$this->uri}page={$page}‘>{$page}</a>&nbsp;"; 240 else 241 break; 242 } 243 $linkPage .= ‘</b>‘; 244 return $linkPage; 245 } 246 247 /* 在对象内部使用的私有方法,获取下一页和尾页的操作信息 */ 248 private function nextlast(){ 249 if($this->page != $this->pageNum) { 250 $str = "&nbsp;<a href=‘{$this->uri}page=".($this->page+1)."‘>{$this->config["next"]}</a>&nbsp;"; 251 $str .= "&nbsp;<a href=‘{$this->uri}page=".($this->pageNum)."‘>{$this->config["last"]}</a>&nbsp;"; 252 return $str; 253 } 254 } 255 256 /* 在对象内部使用的私有方法,用于显示和处理表单跳转页面 */ 257 private function goPage(){ 258 if($this->pageNum > 1) { 259 return ‘&nbsp;<input style="width:20px;height:17px !important;height:18px;border:1px solid #CCCCCC;" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>‘.$this->pageNum.‘)?‘.$this->pageNum.‘:this.value;location=\‘‘.$this->uri.‘page=\‘+page+\‘\‘}" value="‘.$this->page.‘"><input style="cursor:pointer;width:25px;height:18px;border:1px solid #CCCCCC;" type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>‘.$this->pageNum.‘)?‘.$this->pageNum.‘:this.previousSibling.value;location=\‘‘.$this->uri.‘page=\‘+page+\‘\‘">&nbsp;‘; 260 } 261 } 262 263 /* 在对象内部使用的私有方法,用于获取本页显示的记录条数 */ 264 private function disnum(){ 265 if($this->total > 0){ 266 return $this->end()-$this->start()+1; 267 }else{ 268 return 0; 269 } 270 } 271 } 272 273 274 275

浙江省首例禽流感病人死亡 未现动物禽流感疫情-2016-04-08

中新社杭州三月八日电(记者 袁胤)据浙江省卫生厅通报,浙江省首例禽流感病人有某因病情恶化,经全力抢救无效,于三月六日晚死亡。到目前为止,浙江省没有发现动物禽流感疫情,也没有出现新的人禽流感疑似病例。 据介绍,患者有某,女,九岁,学生,浙江省安吉县人,二月十日发病,临床有发热、肺炎表现,经当地治疗无好转,于二月二十日晚转入杭州治疗,二十五日被确诊为人感染高致病性禽流感病例。流行病学调查表明,患者有某发病前曾两次随家人到安徽省广德县走访亲戚,其亲戚家在此期间曾出现病死鸡现象

详情技术分享


 共 21 条数据  本页 3 条  本页从 1-3 条  1/7页  1 2  下一页  末页   

1、查询数据库并分页显示出来

标签:

人气教程排行