PHP

[email protected]

   

การแบ่งข้อมูลออก เป็นหลายหน้า


หลักการคิด
  • หาจำนวนข้อมูลทั้งหมด(totalRows) ของหน้าที่จะแสดง
  • แสดงข้อมูลต่อหน้าที่ต้องการ(Per Page)
  • จำนวนหน้าที่มี = จำนวนข้อมูลทั้งหมด(totalRows) / จำนวนต่อหน้าที่ต้องการ(Per Page)
  • ทั้งหมดเรียกใช้ใน คลาส
  •  
    Ex.
    Page.class.php
    <?php
    class show_page { var $output; var $total_page; var $pvar = "p"; var $psize; var $curr; var $varstr; var $tpage; var $db_connect_id; var $pagesize=10; var $query_result; function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true) { $this->persistency = $persistency; $this->user = $sqluser; $this->password = $sqlpassword; $this->server = $sqlserver; $this->dbname = $database; $this->db_connect_id = @mysql_pconnect($this->server, $this->user, $this->password); if($this->db_connect_id) { if($database != "") { $this->dbname = $database; $dbselect = @mysql_select_db($this->dbname); if(!$dbselect) { @mysql_close($this->db_connect_id); $this->db_connect_id = $dbselect; } } return $this->db_connect_id; } else { return false; } } function sql_query($query) { global $HTTP_GET_VARS; $HTTP_GET_VARS[$this->pvar]=="" ? $page=1: $page=$HTTP_GET_VARS[$this->pvar]; $page_start= ($page-1)*$this->pagesize; $query .=" LIMIT $page_start, $this->pagesize "; return $query; } function setpage() { global $HTTP_SERVER_VARS,$HTTP_GET_VARS; $this->tpage = ceil($this->total_page/$this->pagesize); if ($HTTP_GET_VARS[$this->pvar]!="") { $current = $HTTP_GET_VARS[$this->pvar]; }else {$current =1;} if ($current>$this->tpage) {$current = $this->tpage;} if ($current < 1) { $current = 1;} $this->curr = $current; $this->psize = $this->pagesize; if ($this->tpage > 1) { if ($current>10) { $this->output.='<a href='http://www.geocities.com/oneprogram/phpf/.$PHP_SELF.'?'. $this->pvar.'='.($current-10).($this->varstr).'><<<</a> '; } if ($current>1) { $this->output.='<a href='http://www.geocities.com/oneprogram/phpf/.$PHP_SELF.'?'.$this->pvar.'=' .($current-1).($this->varstr).'><<</a> '; } $start= floor($current/10)*10; $end= $start+9; if ($start<1) {$start=1;} if ($end>$this->tpage) {$end=$this->tpage;} for ($i=$start; $i<=$end; $i++) { if ($current==$i) { $this->output.='<font color="red">'.$i.'</font> '; } else { $this->output.='<a href="http://www.geocities.com/oneprogram/phpf/'.$PHP_SELF.'?'.$this->pvar.'='.$i.$this->varstr.'">['.$i.']</a> '; } } if ($current<$this->tpage) { $this->output.='<a href='http://www.geocities.com/oneprogram/phpf/.$PHP_SELF.'?'.$this->pvar.'=' .($current+1).($this->varstr).'>>></a> '; } if ($this->tpage>10 && ($this->tpage-$current)>=10 ) { $this->output.='<a href='http://www.geocities.com/oneprogram/phpf/.$PHP_SELF.'?'.$this->pvar.'=' .($current+10).($this->varstr).'>>>></a>'; } } } function setvar($data) { foreach ($data as $k=>$v) { $this->varstr.='&'.$k.'='.urlencode($v); } } function output($return = false) { if ($return) { return $this->output; } else { echo $this->output; } } function limit() { return (($this->curr-1)*$this->psize).','.$this->psize; } } //End Class //เริ่มการใช้งาน $p = new show_page; $database="test"; $table="test"; $p->pvar="pagecount"; $p->pagesize=5; $p ->sql_db("localhost","root","",$database); $p->sql_query("SELECT * FROM test WHERE No>10 order by No"); $p->setvar(array("a" => '3', "b" => '4')); $p->setpage(); $p->output(0); echo $p->limit(); ?>

      ที่มา:=> จำไม่ได้ว่านำมาจากที่ใด  PHP ที่ทดสอบ v4.3.4


    สร้างเมื่อวันที่ : 13-08-2548

    เกี่ยวกับเรา | Site Map | Privacy Policy | ติดต่อเรา | ©2005 Company Name

    Hosted by www.Geocities.ws

    1