หัวข้อ: Code แบ่งหน้าใช้ไม่ได้กับ PHP 5
เริ่มหัวข้อโดย: mikeyx ที่ 29 เมษายน 2008, 13:50:37
Create Database member;
CREATE TABLE member ( id int(4) NOT NULL auto_increment, user varchar(30) NOT NULL, name varchar(50) NOT NULL, email varchar(50) NOT NULL, tel varchar(50) NOT NULL, PRIMARY KEY (id) );
Sample1.php
<html> <head> <body bgcolor="#FFFFFF"> <br> <table width="72%" border="1" align="center" cellspacing="0" cellpadding="0" height="23"> <tr> <td width="8%"> <div align="center">id</div> </td> <td width="14%"> <div align="center">user</div> </td> <td width="30%"> <div align="center">name</div> </td> <td width="26%"> <div align="center">email</div> </td> <td width="22%"> <div align="center">tel</div> </td> </tr> </table> <? $host="localhost"; $username=""; $pass_word=""; $db="member"; mysql_connect( $host,$username,$pass_word) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ "); mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
$sql = "select * From member "; /* ตั้งค่า แสดงผลต่อหน้า $Per_Page */
$Per_Page =2; if(!$Page) $Page=1;
$Prev_Page = $Page-1; $Next_Page = $Page+1;
$result = mysql_query($sql); $Page_start = ($Per_Page*$Page)-$Per_Page; $Num_Rows = mysql_num_rows($result);
if($Num_Rows<=$Per_Page) $Num_Pages =1; else if(($Num_Rows % $Per_Page)==0) $Num_Pages =($Num_Rows/$Per_Page) ; else $Num_Pages =($Num_Rows/$Per_Page) +1;
$Num_Pages = (int)$Num_Pages;
if(($Page>$Num_Pages) || ($Page<0)) print "<center><b>จำนวน $Page มากกว่า $Num_Pages ยังไม่มีข้อความ<b></center>"; $sql .= " Where 1 Order by id Desc LIMIT $Page_start , $Per_Page"; //ส่วนแสดงผล $result = mysql_query($sql); While($row= mysql_fetch_array($result)){ $id = $row["id"]; $user = $row["user"]; $name = $row["name"]; $email = $row["email"]; $tel = $row["tel"];
?> <table width="72%" border="1" align="center" cellspacing="0" cellpadding="0" height="23"> <tr> <td width="8%"> <div align="center"> <?= $id;?> </div> </td> <td width="14%"> <?= $user;?> </td> <td width="30%"> <?= $name;?> </td> <td width="27%"> <?= $email;?> </td> <td width="21%"> <?= $tel;?> </td> </tr> </table> <?}?> <div align="center"><br> มีจำนวน Recored ทั้งหมด <?= $Num_Rows;?> รวมทั้งหมด : <b> <?=$Num_Pages;?> </b> หน้า : <?/* สร้างปุ่มย้อนกลับ */ if($Prev_Page) echo " <a href='$PHP_SELF?Page=$Prev_Page'><< ย้อนกลับ </a>"; for($i=1; $i<$Num_Pages; $i++){ if($i != $Page) echo "[<a href='$PHP_SELF?Page=$i'>$i</a>]"; else echo "<b> $i </b>"; } /*สร้างปุ่มเดินหน้า */ if($Page!=$Num_Pages) echo "<a href ='$PHP_SELF?Page=$Next_Page'> หน้าถัดไป>> </a>";
?> </div> </body> </html>
:-* รวบกวนผู้รู้ด้วยค๊าบ
หัวข้อ: Re: Code แบ่งหน้าใช้ไม่ได้กับ PHP 5
เริ่มหัวข้อโดย: EThaiZone ที่ 29 เมษายน 2008, 13:56:50
Sample1.php
<html> <head> <body bgcolor="#FFFFFF"> <br> <table width="72%" border="1" align="center" cellspacing="0" cellpadding="0" height="23"> <tr> <td width="8%"> <div align="center">id</div> </td> <td width="14%"> <div align="center">user</div> </td> <td width="30%"> <div align="center">name</div> </td> <td width="26%"> <div align="center">email</div> </td> <td width="22%"> <div align="center">tel</div> </td> </tr> </table> <? $host="localhost"; $username=""; $pass_word=""; $db="member"; mysql_connect( $host,$username,$pass_word) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ "); mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
$sql = "select * From member "; /* ตั้งค่า แสดงผลต่อหน้า $Per_Page */
$Per_Page =2; if(!$_GET['Page']) $_GET['Page']=1;
$Prev_Page = $_GET['Page']-1; $Next_Page = $_GET['Page']+1;
$result = mysql_query($sql); $Page_start = ($Per_Page*$_GET['Page'])-$Per_Page; $Num_Rows = mysql_num_rows($result);
if($Num_Rows<=$Per_Page) $Num_Pages =1; else if(($Num_Rows % $Per_Page)==0) $Num_Pages =($Num_Rows/$Per_Page) ; else $Num_Pages =($Num_Rows/$Per_Page) +1;
$Num_Pages = (int)$Num_Pages;
if(($_GET['Page']>$Num_Pages) || ($_GET['Page']<0)) print "<center><b>จำนวน ".$_GET['Page']." มากกว่า $Num_Pages ยังไม่มีข้อความ<b></center>"; $sql .= " Where 1 Order by id Desc LIMIT $Page_start , $Per_Page"; //ส่วนแสดงผล $result = mysql_query($sql); While($row= mysql_fetch_array($result)){ $id = $row["id"]; $user = $row["user"]; $name = $row["name"]; $email = $row["email"]; $tel = $row["tel"];
?> <table width="72%" border="1" align="center" cellspacing="0" cellpadding="0" height="23"> <tr> <td width="8%"> <div align="center"> <?= $id;?> </div> </td> <td width="14%"> <?= $user;?> </td> <td width="30%"> <?= $name;?> </td> <td width="27%"> <?= $email;?> </td> <td width="21%"> <?= $tel;?> </td> </tr> </table> <?}?> <div align="center"><br> มีจำนวน Recored ทั้งหมด <?= $Num_Rows;?> รวมทั้งหมด : <b> <?=$Num_Pages;?> </b> หน้า : <?/* สร้างปุ่มย้อนกลับ */ if($Prev_Page) echo " <a href='$PHP_SELF?Page=$Prev_Page'><< ย้อนกลับ </a>"; for($i=1; $i<$Num_Pages; $i++){ if($i != $_GET['Page']) echo "[<a href='$PHP_SELF?Page=$i'>$i</a>]"; else echo "<b> $i </b>"; } /*สร้างปุ่มเดินหน้า */ if($_GET['Page']!=$Num_Pages) echo "<a href ='$PHP_SELF?Page=$Next_Page'> หน้าถัดไป>> </a>";
?> </div> </body> </html>
หัวข้อ: Re: Code แบ่งหน้าใช้ไม่ได้กับ PHP 5
เริ่มหัวข้อโดย: mikeyx ที่ 29 เมษายน 2008, 14:06:43
อ๋อลืมไป ต้องมี
GET ขอบคุณมากครับ :-*
|