PHP中,类的封装,调用问题
本人刚接触PHP,最近写了个分页显示东西,虽然效果有,但是师傅说规范有问题,一些判断啊,逻辑上的代码,他说最好是能封装到类里,然后调用下,我琢磨了半天,只专研出,可以把那...
本人刚接触PHP,最近写了个分页显示东西,虽然效果有,但是师傅说规范有问题,一些判断啊,逻辑上的代码,他说最好是能封装到类里,然后调用下,我琢磨了半天,只专研出,可以把那部分的代码放到独立的PHP文件中,然后用一个include把它们串联起来,至于师傅说的封装到类里,然后调用,真没搞出什么花头来,所以请各位大大帮忙,具体代码如下:
<?php
$perNumber = 2;
$page = $_GET['page'];
$count = mysql_query("select count(*) from people");
$rs = mysql_fetch_array($count);
$totalNumber = $rs[0];
$totalPage = ceil($totalNumber/$perNumber);
if(!isset($page)){
$page = 1;
}
$startCount = ($page-1)*$perNumber;
$result = mysql_query("select * from people limit $startCount,$perNumber");
$num = mysql_numrows($result);
for($i=0;$i<$num;$i++){
$a = mysql_result($result,$i,"id");
$b = mysql_result($result,$i,"name");
$c = mysql_result($result,$i,"age");
echo "<br />".$a."<br />".$b."<br />".$c."<br />";
}
?>
这一段是用判断分页数并且输出的,请问,如果想帮这段封装到类里,并且在别的地方调用的话,该怎么写能?(越具体越好) 展开
<?php
$perNumber = 2;
$page = $_GET['page'];
$count = mysql_query("select count(*) from people");
$rs = mysql_fetch_array($count);
$totalNumber = $rs[0];
$totalPage = ceil($totalNumber/$perNumber);
if(!isset($page)){
$page = 1;
}
$startCount = ($page-1)*$perNumber;
$result = mysql_query("select * from people limit $startCount,$perNumber");
$num = mysql_numrows($result);
for($i=0;$i<$num;$i++){
$a = mysql_result($result,$i,"id");
$b = mysql_result($result,$i,"name");
$c = mysql_result($result,$i,"age");
echo "<br />".$a."<br />".$b."<br />".$c."<br />";
}
?>
这一段是用判断分页数并且输出的,请问,如果想帮这段封装到类里,并且在别的地方调用的话,该怎么写能?(越具体越好) 展开
3个回答
展开全部
<?
class Page{
function 分页函数(参数){
$perNumber = 2;
$page = $_GET['page'];
$count = mysql_query("select count(*) from people");
$rs = mysql_fetch_array($count);
$totalNumber = $rs[0];
$totalPage = ceil($totalNumber/$perNumber);
if(!isset($page)){
$page = 1;
}
$startCount = ($page-1)*$perNumber;
$result = mysql_query("select * from people limit $startCount,$perNumber");
$num = mysql_numrows($result);
for($i=0;$i<$num;$i++){
$a = mysql_result($result,$i,"id");
$b = mysql_result($result,$i,"name");
$c = mysql_result($result,$i,"age");
echo "<br />".$a."<br />".$b."<br />".$c."<br />";
}
}
}
?>
class Page{
function 分页函数(参数){
$perNumber = 2;
$page = $_GET['page'];
$count = mysql_query("select count(*) from people");
$rs = mysql_fetch_array($count);
$totalNumber = $rs[0];
$totalPage = ceil($totalNumber/$perNumber);
if(!isset($page)){
$page = 1;
}
$startCount = ($page-1)*$perNumber;
$result = mysql_query("select * from people limit $startCount,$perNumber");
$num = mysql_numrows($result);
for($i=0;$i<$num;$i++){
$a = mysql_result($result,$i,"id");
$b = mysql_result($result,$i,"name");
$c = mysql_result($result,$i,"age");
echo "<br />".$a."<br />".$b."<br />".$c."<br />";
}
}
}
?>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class Page
{
public $page;
public $pagesize;
public $host;
public $user;
public $pass;
public $ut;
public $db
public $conn;
public function __construct($page,$pagesize,$host,$user,$pass,$db,$ut='GBK')
{
$this->page = $page;
$this->pagesize = $pagesize;
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db = $db;
$this->ut = $ut;
}
public function db()
{
$this->conn= mysql_connect($this->host,$this->user,$this->pass);
mysql_select_db($this->db);
mysql_query("set names '".$this->ut."'")
}
public function page()
{
$query = mysql_query("select count(*) as id from people");
$count = mysql_fetch_array($query);
$count = $count['id'];
$totalPage = ceil($count/$this->pagesize);
if(!isset($this->page)){
$this->page = 1;
}
$startCount = ($this->page-1)*$this->pagesize;
$result = mysql_query("select * from people limit ".$startCount.",".$this->pagesize);
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$a = mysql_result($result,$i,"id");
$b = mysql_result($result,$i,"name");
$c = mysql_result($result,$i,"age");
echo "<br />".$a."<br />".$b."<br />".$c."<br />";
}
}
}
{
public $page;
public $pagesize;
public $host;
public $user;
public $pass;
public $ut;
public $db
public $conn;
public function __construct($page,$pagesize,$host,$user,$pass,$db,$ut='GBK')
{
$this->page = $page;
$this->pagesize = $pagesize;
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db = $db;
$this->ut = $ut;
}
public function db()
{
$this->conn= mysql_connect($this->host,$this->user,$this->pass);
mysql_select_db($this->db);
mysql_query("set names '".$this->ut."'")
}
public function page()
{
$query = mysql_query("select count(*) as id from people");
$count = mysql_fetch_array($query);
$count = $count['id'];
$totalPage = ceil($count/$this->pagesize);
if(!isset($this->page)){
$this->page = 1;
}
$startCount = ($this->page-1)*$this->pagesize;
$result = mysql_query("select * from people limit ".$startCount.",".$this->pagesize);
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$a = mysql_result($result,$i,"id");
$b = mysql_result($result,$i,"name");
$c = mysql_result($result,$i,"age");
echo "<br />".$a."<br />".$b."<br />".$c."<br />";
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把分页的具体过程写的函数里 调用的时候传递相应的参数即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询