Terdahulu kami menyebut menyediakan kelas LyDB yang menggunakan pangkalan data
Di sini kami merangkum semua fungsi SQL dan paging dan memanggilnya terus kemudian:
<?php class LyDB{ var $_host="localhost"; var $_user="root";// var $_password="root"; var $_database="test"; var $page_size=3; var $link; public function __construct(){ //连接数据库 date_default_timezone_set('PRC'); $this->link = mysqli_connect($this->_host,$this->_user,$this->_password,$this->_database); if (!$this->link) { die('Could not connect to MySQL: ' . mysqli_connect_error()); } } public function __destruct(){ mysqli_close($this->link); } public function insert($nickname,$avatar,$message) //添加留言内容到数据库 { $message=str_replace ("<" , "<" , $message); //str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写)。 $message=str_replace (">" , ">" , $message); $message=str_replace ("\n" , " " , $message); $message=trim($message); //trim() 函数移除字符串两侧的空白字符或其他预定义字符。 $lytime=date("Y-m-d H:i:s"); $sql="insert into ly (nickname,message,avatar,lytime)values('$nickname','$message','$avatar','$lytime')"; $query=mysqli_query($this->link,$sql); if($query){ return true; } else { return false; } } public function reply($id,$reply){ $message=str_replace ("<" , "<" , $reply); $message=str_replace (">" , ">" , $message); $message=str_replace ("\n" , " " , $message); $message=trim($message); $reply=$message; $retime=date("Y-m-d H:i:s"); $sql="update ly set reply='$reply', retime='$retime' where id=$id"; $query=mysqli_query($this->link,$sql); if($query)return true; else return false; } public function delete_by_id($id){ //删除留言 $sql="delete from ly where id=$id"; mysqli_query($this->link,$sql); } public function select_page_info(){ //分页 $sql = "select count(*) as pagecount from ly"; $query = mysqli_query($this->link,$sql); $row = mysqli_fetch_assoc($query); $ly_count=$row["pagecount"]; $page_count=($ly_count%$this->page_size==0)?($ly_count/$this->page_size):($ly_count/$this->page_size+1); $page_info=array("ly_count"=> $ly_count,"page_count"=>(int)$page_count,"page_size"=>$this->page_size); return $page_info; } public function select_all_result(){ //查询显示留言 $sql="select * from ly order by id desc"; $query=mysqli_query($this->link,$sql); return $query; } public function select_page_result(&$page_no){ //上一页,下一页,首页,末页 $page_info=$this->select_page_info(); if(!isset($page_no))$page_no=1; else if($page_no<1)$page_no=1; else if($page_no>$page_info["page_count"])$page_no=$page_info["page_count"]; $first=($page_no-1)*$this->page_size; $sql="select * from ly order by id desc limit $first,$this->page_size "; $query=mysqli_query($this->link,$sql); $page_result=array("page_data"=>$query,"page_info"=>$page_info,"page_no"=>$page_no); return $page_result; } } ?>
Beri nama fail kelas ini. : lydb.class.php
Log masuk pentadbir dan penghakiman kebenaran juga mencipta fail class authority.class.php yang dipanggil
<?php class Authority{ static public function check_insert(){ //声明公共变量 //检查是否具有添加留言权限 return true; } static public function check_delete(){ //检查是否具有delete权限 if(isset($_SESSION["username"]) && $_SESSION["username"]=="admin") return true; else return false; } } ?> <?php class User{ static public function validate($username,$password){ // static public 声明公共变量 if("admin"==$username && "12345"==$password){ return true; } else return false; } } ?>
dan kemudian merujuknya terus pada halaman muka hadapan. Boleh:
<?php require_once("lydb.class.php");?> <?php require_once("authority.class.php");?>
pernyataan memerlukan_sekali() menyertakan dan menjalankan fail yang ditentukan semasa pelaksanaan skrip (dalam istilah awam, fail dalam kurungan akan dilaksanakan sekali). Tingkah laku ini serupa dengan pernyataan require(), satu-satunya perbezaan ialah jika kod dalam fail sudah disertakan, ia tidak akan disertakan lagi.
bahagian seterusnya