|
 |
|
การดึงรูปจาก database มาสร้างตารางครับ สมมติว่าผมมีรูปอยู่ในโฟล์เดอร์ประมาณ 100 รูป แล้วกับ เปน url ไว้ใน database นะครับ |
|
 |
|
|
 |
 |
|
ลองเซิร์จเว็บนี้
คีย์เวิร์ด 'แบ่งหน้า' ดูคับ
|
 |
 |
 |
 |
Date :
2009-05-15 13:48:37 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
class page_split
{
var $var_pageSize;
var $var_currentPage;
var $var_totalPage;
var $var_file;
function page_split($pagesize=10, $file=""){
$this->var_pageSize=$pagesize;
$this->var_currentPage=$page;
$this->var_file=$file;
}
function _setPageSize($size=10){
$this->var_pageSize=$size;
}
function _setPage($page=1){
if(empty($page)) $page=1;
$this->var_currentPage=$page;
}
function _setFile($file=""){
$this->var_file=$file;
}
function _query($sql)
{
$result=mysql_query($sql);
$num=mysql_num_rows($result);
$rt = $num%$this->var_pageSize;
# หาจำนวนหน้าทั้งหมด
$this->var_totalPage = ($rt!=0) ? ceil($num/$this->var_pageSize) : floor($num/$this->var_pageSize);
$goto = ($this->var_currentPage - 1) * $this->var_pageSize;
$sql .= " LIMIT $goto , ".$this->var_pageSize;
$result=mysql_query($sql);
return $result;
}
function _displayPage($option="",$align="left")
{
# รูปแบบตัวแปร option คือ $option = "id=$c_id";
# ถ้ามีหลายตัวแปรก็จะเป็น $option = "id=$c_id&name=$myname&action=$action";
echo "<table align=center width=98% class=clear border=0 bordercolor=black cellspacing=0 cellpadding=2>\n";
echo "<tr><td align=$align>\n";
echo "<font color=#686898>\n";
# สร้าง link เพื่อไปหน้าก่อน-หน้าถัดไป
echo "กำลังแสดงหน้าที่ ";
if($this->var_currentPage >1 && $this->var_currentPage<=$this->var_totalPage) {
$prevpage = $this->var_currentPage - 1;
echo "<a href='".$this->var_file."?page=".$prevpage."&".$option."' title='หน้าก่อนนี้'><</a>\n";
}
echo " <b>".$this->var_currentPage."/".$this->var_totalPage."</b> ";
if($this->var_currentPage != $this->var_totalPage) {
$nextpage = $this->var_currentPage + 1;
echo "<a href='".$this->var_file."?page=".$nextpage."&".$option."' title='หน้าถัดไป'>></a>\n";
}
echo "</font>\n";
echo "</td>\n";
echo "<td align=right>\n";
# วนลูปแสดงเลขหน้าทั้งหมด แบบเป็นช่วงๆ ช่วงละ 10 หน้า
$b=floor($this->var_currentPage/10);
$c=(($b*10));
if($c>1) {
$prevpage = $c-1;
echo "<a href='".$this->var_file."?page=".$prevpage."&".$option."' title='10 หน้าก่อนนี้'><<</a> \n";
}
else{
echo "<<\n";
}
echo " <b>";
for($i=$c; $i<$this->var_currentPage ; $i++) {
if($i>0)
echo "<a href='".$this->var_file."?page=".$i."&".$option."'>$i</a> \n";
}
echo "<font size=2 color=red>".$this->var_currentPage."</font> \n";
for($i=($this->var_currentPage+1); $i<($c+10) ; $i++) {
if($i<=$this->var_totalPage)
echo "<a href='".$this->var_file."?page=".$i."&".$option."'>$i</a> \n";
}
echo "</b> ";
if($c>=0) {
if(($c+10)<=$this->var_totalPage){
$nextpage = $c+10;
echo "<a href='".$this->var_file."?page=".$nextpage."&".$option."' title='10 หน้าถัดไป'>>></a> \n";
}
else
echo ">>\n";
}
else{
echo ">>\n";
}
echo "</td></tr>\n";
echo "</table>\n";
}# ปิด ฟังก์ชั่น _displayPage
} # ปิด Class
/*
วิธีการใช้งาน
==================================================
แบบที่ 1
==================================================
# ส่วนของการติดต่อ ฐานข้อมูล MySQL ใช้ปกติเหมือนที่เคยทำ หรืออาจจะเก็บไว้ในไฟล์ connect.php แล้วเรียกใช้ ก็ได้
mysql_connect("localhost","root","");
mysql_select_db("test");
require_once "class.page_split.php"; # เรียกใช้ไฟล์ ระบบ
$obj = new page_split(); # ประกาศ object
$obj->_setPageSize(5); # กำหนดจำนวนข้อมูลต่อหนึ่งหน้าการแสดงผล
$obj->_setFile("showdata.php"); # กำหนดชื่อไฟล์ที่ใช้แสดงผล (ไม่อยากใช้ ตัวแปรระบบ เพราะไม่รู้ว่ามันใช้ได้ทุก Browser หรือเปล่าส่งค่ามันดื้อๆนี้แหละ)
$obj->_setPage($page); # กำหนดเพจ (ต้องกำหนดไว้แบบนี้เลย ห้ามเปลี่ยน)
$sql="SELECT * FROM prophesy";
$result=$obj->_query($sql); # นำมาใช้งานแทนคำสั่ง mysql_query(...); ที่เดิม เคยใช้งานอยู่
$obj->_displayPage(); # กำหนดให้แสดง รายการหน้าทั้งหมด
# ส่วนของการวนลูปแสดงผลแบบเดิมๆที่ใช้งานอยู่ก่อน ไม่ต้องเปลี่ยนใดๆ
while($data=mysql_fetch_array($result))
{
echo sprintf("%03d.",$data["No"])." ".$data["prophesy"]."<br>";
}
$obj->_displayPage(); # กำหนดให้แสดง รายการหน้าทั้งหมด (อีกครั้งตามต้องการ)
==================================================
แบบที่ 2 (เหมือนกับแบบที่ 1 แต่เปลี่ยนนิดหน่อย)
==================================================
require_once "class.page_split.php"; # เรียกใช้ไฟล์ ระบบ
$obj = new page_split(2, "showdata.php"); # เป็นการกำหนด ข้อมูลจำนวนหน้า และไฟล์ ลงไปขณะ สร้าง Object เลย
$obj->_setPage($page);
?>
|
 |
 |
 |
 |
Date :
2009-05-15 14:51:04 |
By :
melodyApinan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จะลองนำไปใช้ดูนะครับ
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2009-05-15 14:53:58 |
By :
domeawji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอรบกวนอีกนิดนะครับ ผมลองดูแล้วดีมากเลยแต่ผมไม่รู้ว่าจะทำให้เป็นตารางแบบในรูปนี้ยังไงครับ

ส่วนข้างล่างก็จะเป็นการแบ่งหน้าครับตามcodeที่อยู่ข้างบน
|
 |
 |
 |
 |
Date :
2009-05-15 15:39:31 |
By :
domeawji |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|