面向对象的三大特点:
1:封装。2:继承。3:多态,对于多态在PHP当中不是那么的好介绍,只需要记住是运行时加载就行了!
对象的几个语句的意思;1:public在对象中是公开访问的,2:private在对象中是不能访问他的内容,保密状态
3:protected:户类和内部访问;4:——construct初始化对象!
接下来就是列子代码,代码如下:
1 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> 2 <?php 3 4 class Person{ 5 public $name; 6 public $pwd; 7 public $age; 8 9 //在初始化对象的时候该函数会自动运行 10 //初始化函数 11 // function __construct($name,$pwd,$age){ 12 // 13 // } 14 15 public function intro(){ 16 17 echo "我的名字是:".$this->name." 我的密码是:".$this->pwd; 18 } 19 20 21 } 22 23 $p1 = new Person(); 24 $p1->name = "diaosi"; 25 $p1->age = 18; 26 $p1->pwd = "22222"; 27 28 $p1->intro(); 29 30 $p2 = new Person(); 31 $p2->name = "asdsad"; 32 $p2->age = 19; 33 $p2->pwd = "44444"; 34 35 36 37 38 ?>
1 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> 2 <?php 3 4 class Person{ 5 private $name; 6 private $pwd; 7 private $age; 8 9 //在初始化对象的时候该函数会自动运行 10 //初始化函数 11 function __construct($name,$pwd,$age){ 12 $this->name = $name; 13 $this->pwd = $pwd; 14 $this->age = $age; 15 } 16 17 public function intro(){ 18 19 echo "我的名字是:".$this->name." 我的密码是:".$this->pwd; 20 } 21 22 23 } 24 25 $p1 = new Person("zhangsan","sssss",20); 26 $p1->intro(); 27 28 ?>
接下来就是分页代码,代码如下:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <meta content="ellen" name="author"> 6 <link rev="made" href=""> 7 <link href="page.css" type="text/css" rel="stylesheet"> 8 <title>新闻列表页</title> 9 </head> 10 <body> 11 <table border="1" width="600" cellspacing="1" align="center"> 12 <tr> 13 <td>地址</td> 14 <td>百分比</td> 15 <td>点击量</td> 16 <td>时间</td> 17 </tr> 18 <?php 19 include("page.class.php"); //分页类 20 include("conn.php"); 21 $page=@$_GET['page']; 22 $sql = "SELECT * FROM topic"; 23 $query = mysql_query($sql); 24 $totail = mysql_num_rows($query);//记录总条数 25 $number = 5;//每页显示条数 26 $my_page=new PageClass($totail,$number,$page,'?page={page}');//参数设定:总记录,每页显示的条数,当前页,连接的地址 27 $sql_p = "SELECT * FROM topic WHERE 1 LIMIT ".$my_page->page_limit.",".$my_page->myde_size; 28 $query_p = mysql_query($sql_p); 29 while($row = mysql_fetch_array($query_p)){ 30 ?> 31 <tr> 32 <td> 33 <?php echo $row['title'];?> 34 </td> 35 <td> 36 <?php echo $row['content'];?> 37 </td> 38 <td> 39 <?php echo $row['userid'];?> 40 </td> 41 <td> 42 <?php echo $row['publishtime'];?> 43 </td> 44 </tr> 45 <?php 46 } 47 ?> 48 </table> 49 <?php 50 echo $my_page->myde_write1();//输出分页 51 ?> 52 </body> 53 </html>
2:
1 <?php 2 $conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误"); 3 mysql_select_db("bbs", $conn) or die("连接失败。。。"); 4 mysql_query("set names 'utf8'"); //使用GBK中文编码; 5 ?>
3:
1 <?php 2 class PageClass 3 { 4 private $myde_count; //总记录数 5 public $myde_size; //每页记录数 6 private $myde_page; //当前页 7 private $myde_page_count; //总页数 8 private $page_url; //页面url 9 private $page_i; //起始页 10 private $page_ub; //结束页 11 public $page_limit; 12 13 function __construct($myde_count=0,$myde_size=1,$myde_page=1,$page_url) //构造函数,初始化 14 { 15 $this->myde_count=$this->numeric($myde_count); 16 $this->myde_size=$this->numeric($myde_size); 17 $this->myde_page=$this->numeric($myde_page); 18 $this->page_limit=($this->myde_page * $this -> myde_size) - $this -> myde_size; //下一页的开始记录 19 $this->page_url=$page_url; //连接的地址 20 if($this->myde_page<1)$this->myde_page=1; //当前页小于1的时候,,值赋值为1 21 if($this->myde_count<0)$this->myde_page=0; 22 $this->myde_page_count=ceil($this->myde_count/$this->myde_size);//总页数 23 if($this->myde_page_count<1) 24 $this->myde_page_count=1; 25 if($this->myde_page > $this->myde_page_count) 26 $this->myde_page = $this->myde_page_count; 27 28 29 //控制显示出来多少个页码(这个是原来的) 30 //$this->page_i = $this->myde_page-2; 31 //$this->page_ub = $this->myde_page+2; 32 33 $this->page_i = $this->myde_page; 34 $this->page_ub = $this->myde_page+5; 35 //以下这个if语句是保证显示5个页码 36 if($this->page_ub > $this->myde_page_count) 37 { 38 $this->page_ub = $this->myde_page_count; 39 $this->page_i = $this->page_ub-5; 40 } 41 42 43 if($this->page_i<1)$this->page_i=1; 44 if($this->page_ub>$this->myde_page_count){$this->page_ub=$this->myde_page_count; } 45 } 46 private function numeric($id) //判断是否为数字 47 { 48 if (strlen($id)) 49 { 50 // if (!preg_match("^[0-9]+$",$id)) $id = 1; 51 } 52 else 53 { 54 $id = 1; 55 } 56 return $id; 57 } 58 59 private function page_replace($page) //地址替换 60 {return str_replace("{page}", $page, $this -> page_url);} 61 62 private function myde_home() //首页 63 { if($this -> myde_page != 1){ 64 return " <li class=\"page_a\"><a href=\"".$this -> page_replace(1)."\" title=\"首页\" >首页</a></li>\n"; 65 }else{ 66 return " <li>首页</li>\n"; 67 } 68 } 69 private function myde_prev() //上一页 70 { if($this -> myde_page != 1){ 71 return " <li class=\"page_a\"><a href=\"".$this -> page_replace($this->myde_page-1) ."\" title=\"上一页\" >上一页</a></li>\n"; 72 }else{ 73 return " <li>上一页</li>\n"; 74 } 75 } 76 private function myde_next() //下一页 77 { 78 if($this -> myde_page != $this -> myde_page_count){ 79 return " <li class=\"page_a\"><a href=\"".$this -> page_replace($this->myde_page+1) ."\" title=\"下一页\" >下一页</a></li>\n"; 80 }else 81 { 82 return " <li>下一页</li>\n"; 83 } 84 } 85 private function myde_last() //尾页 86 { 87 if($this -> myde_page != $this -> myde_page_count){ 88 return " <li class=\"page_a\"><a href=\"".$this -> page_replace($this -> myde_page_count)."\" title=\"尾页\" >尾页</a></li>\n"; 89 }else{ 90 return " <li>尾页</li>\n"; 91 } 92 } 93 function myde_write($id='page') //输出 94 { 95 $str = "<div id=\"".$id."\" class=\"pages\">\n <ul>\n "; 96 $str .= "<li>总记录:<span>".$this -> myde_count."</span></li>\n"; 97 $str .= "<li><span>".$this -> myde_page."</span>/<span>".$this -> myde_page_count."</span></li>\n"; 98 $str .= $this -> myde_home(); //调用方法,显示“首页” 99 $str .= $this -> myde_prev(); //调用方法,显示“上一页” 100 //以下显示1,2,3...分页 101 for($page_for_i=$this->page_i;$page_for_i <= $this -> page_ub;$page_for_i++){ 102 if($this -> myde_page == $page_for_i){ 103 $str .= "<li class=\"on\">".$page_for_i."</li>\n"; 104 } 105 else{ 106 $str .= "<li class=\"page_a\"><a href=\"".$this -> page_replace($page_for_i)."\" title=\"第".$page_for_i."页\">"; 107 $str .= $page_for_i . "</a></li>\n"; 108 } 109 } 110 $str .= $this -> myde_next(); //调用方法,显示“下一页” 111 $str .= $this -> myde_last(); //调用方法,显示“尾页” 112 //以下是显示跳转页框 113 $str .= "<li class=\"pages_input\"><input type=\"text\" value=\"".$this -> myde_page."\""; 114 $str .= "onmouseover=\"javascript:this.value='';this.focus();\" onkeydown=\"javascript: if(event.keyCode==13){ location='"; 115 $str .= $this -> page_replace("'+this.value+'")."';return false;}\""; 116 $str .= " title=\"输入您想要到达的页码,然后回车!\" /></li>\n"; 117 //以上是显示跳转页框 118 $str .= " </ul></div>"; 119 return $str; 120 } 121 function myde_write1($id='page') //输出 122 { 123 $str = "<div id=\"".$id."\" class=\"pages\">\n <ul>\n "; 124 $str .= "<li>总记录:<span>".$this -> myde_count."</span></li>\n"; 125 $str .= "<li><span>".$this -> myde_page."</span>/<span>".$this -> myde_page_count."</span></li>\n"; 126 $str .= $this -> myde_home(); //调用方法,显示“首页” 127 $str .= $this -> myde_prev(); //调用方法,显示“上一页” 128 //以下显示1,2,3...分页 129 for($page_for_i=$this->page_i;$page_for_i <= $this->page_ub;$page_for_i++){ 130 if($this -> myde_page == $page_for_i) 131 { 132 $str .= "<li class=\"on\">".$page_for_i."</li>\n"; 133 } 134 else{ 135 $str .= "<li class=\"page_a\"><a href=\"".$this -> page_replace($page_for_i)."\" title=\"第".$page_for_i."页\">"; 136 $str .= $page_for_i . "</a></li>\n"; 137 } 138 //以上显示1,2,3...分页 139 } 140 $str .= $this -> myde_next(); //调用方法,显示“下一页” 141 $str .= $this -> myde_last(); //调用方法,显示“尾页” 142 //以下是显示下拉式跳转页框 143 $str .="<li ><select class=\"**********\" onchange=\" javascript: location='".$this->page_replace("'+this.value+'")."';return false; \">"; 144 $str .="<option value=\"\"></option>"; 145 for($i=1;$i <= $this->myde_page_count;$i++) 146 { 147 $str .="<option value=\"".$i."\">".$i."</option>"; 148 } 149 $str .="</select></li>\n"; 150 //以下是显示下拉式跳转页框 151 152 //以下是显示跳转页框 153 $str .= "<li class=\"pages_input\"><input type=\"text\" value=\"".$this -> myde_page."\""; 154 $str .= "onmouseover=\"javascript:this.value='';this.focus();\" onkeydown=\"javascript: if(event.keyCode==13){ location='"; 155 $str .= $this -> page_replace("'+this.value+'")."';return false;}\""; 156 $str .= "title=\"输入您想要到达的页码,然后回车!\" /></li>\n"; 157 //以上是显示跳转页框 158 $str .= " </ul></div>"; 159 return $str; 160 } 161 } 162 /*-------------------------实例--------------------------------* 163 $page = new PageClass(1000,5,$_GET['page'],'?page={page}');//用于动态 164 $page = new PageClass(1000,5,$_GET['page'],'list-{page}.html');//用于静态或者伪静态 165 $page -> myde_write();//显示 166 */ 167 ?>
接下里为CSS代码:
1 .pages { 2 font-family:Arial, Helvetica, sans-serif; 3 font-size:12px; 4 } 5 .pages li { 6 display:inline; 7 float:left; 8 padding:0px 5px; 9 height:25px; 10 line-height:25px; 11 color:#666; 12 margin-right: 0.3em; 13 border: 1px solid #E0E0E0; 14 background:#FFF; 15 } 16 .pages li span { 17 color:#cc3300; 18 background:#FFF; 19 } 20 .pages li.page_a { 21 padding:0; 22 border:0; 23 } 24 .pages li.page_a a { 25 FLOAT: left; 26 padding:0px 5px; 27 color:#0044DD; 28 border: 1px solid #E0E0E0; 29 } 30 .pages li.page_a a:hover { 31 background-color:#9CC0F8; 32 border: 1px solid #A0A0A0; 33 } 34 .pages li.pages_input { 35 padding:0; 36 border: 1px solid #A0A0A0; 37 } 38 .pages li.pages_input input { 39 width:18px; 40 font-size:14px; 41 border:1px; 42 padding:0px 3px; 43 margin:0px 3px; 44 text-align:center; 45 } 46 .pages .on { 47 padding:0px 5px; 48 color: red; 49 font-weight:bold; 50 } 51 li{ 52 position: relative; 53 left: 430px; 54 } 55 a{ 56 text-decoration: none; 57 } 58 a:hover{ 59 text-decoration: underline; 60 }