体会,所要显示的第几页数和每页所要显示的数目。之间的计算。蛮好用的。
还有。是升序排列还是降序排列。自己去选择。不影响。
/** * 获取留言 */ public function readMessage($array) { if(empty($array)) { return; } $page=$array["page"]; $limit=$array["limit"]; $offset=($page-1)*$limit; $id=mysql_connect("localhost","root","123456"); mysql_select_db("chatroom",$id); mysql_query("set character set utf8"); $num=mysql_num_rows(mysql_query("SELECT * FROM `message` ",$id))+1; if($num==1) { //这里表示,表里边一条数据都没有呢。 return; } $sql="SELECT `message`.`id`,`message`.`nickname`,`message`.`leavedate`,`message`.`contents` FROM `message` WHERE 1 order by `message`.`id` DESC limit $offset,$limit"; $info=mysql_query($sql); $mes=array(); while($row = mysql_fetch_assoc($info)) { $arr=array(); $arr["total"]=$num; $arr["id"]=$row["id"]; $arr["nickname"]=$row["nickname"]; $arr["leavedate"]=$row["leavedate"]; $arr["contents"]=$row["contents"]; $mes[]=$arr; } mysql_close($id); return $mes; } /** * 插入留言 */ public function leaveMessage($array) { if(empty($array)) { return; } $limit=$array["limit"]; $nickname=$array["nickname"]; $contents=$array["contents"]; $id=mysql_connect("localhost","root","123456"); mysql_select_db("chatroom",$id); mysql_query("set character set utf8"); $sql="SELECT `account`.`id` FROM `account` WHERE `account`.`nickname`='".$nickname."'"; $info=mysql_query($sql); $userid=array(); while($row = mysql_fetch_assoc($info)) { $userid[]=$row["id"]; } $num=mysql_num_rows(mysql_query("SELECT * FROM `message` ",$id))+1; $cnunixtime=time()+8*60*60; $gd=getdate($cnunixtime); $mestime=$gd["year"]."-".$gd["mon"]."-".$gd["mday"]." ".$gd["hours"].":".$gd["minutes"].":".$gd["seconds"]; $insert="INSERT INTO message(`id`,`account_id`,`nickname`,`leavedate`,`contents`) VALUES ('$num','$userid[0]','$nickname','$mestime','$contents')"; mysql_query($insert,$id); mysql_close($id); $obj=array(); $obj["page"]=1; $obj["limit"]=$limit; $all=$this->readMessage($obj); return $all; }