|
|
|
ช่วยดูให้หน่อยค่ะเกี่ยวกับการแบ่งหน้าแสดงผล ช่วยหน่อยค่ะเครียดมากเลย ทำโปรเจ็คอยู่ค่ะ |
|
|
|
|
|
|
|
ช่วยดูโค๊ดด้านล่างนี้ให้หน่อยค่ะ มันแจ้ง Error ว่า :
PageNavigator Page :: 1 Notice: Undefined variable: PHP_SELF in F:\WEBSERVER\page.php on line 34
โค๊ดทั้งหมดค่ะ
-----------------------------------------------------------------------------------------------------------------------------------------
<?php
// สร้างข้อมูลจำลอง
for ( $i = 1; $i<=112; $i++ ) {
$data[] = array( "date"=>date("m-d-Y H:i:s"),
"address"=>rand(0,255).".".rand(0,255).".".rand(0,255).".".rand(0,255),
"call_number"=>"346".rand(100,999),
"page"=>rand(0,100),
"access"=>rand(0,1000)
);
}
/**
คำนวณค่าที่เกี่ยวข้องกับการแบ่งหน้า
1. ตำแหน่งหน้าปัจจุบันที่ต้องการให้แสดงรายการ $page
2. จำนวนรายการต่อหน้า $itemsPerPage
3. จำนวนรายการทั้งหมด $totalItems
4. จำนวนหน้าทั้งหมด $totalPage
5. รายการแรกและรายการสุดท้ายที่จะต้องแสดงผล $startItem,$endItem
*/
if ( ! IsSet($page) ) $page=1;
$itemsPerPage=10; // กำหนดจำนวนรายการต่อหน้า
$allItems = Count($data); // กำหนดจำนวนรายการทั้งหมด
$totalPage = Ceil($allItems / $itemsPerPage); // หาค่าจำนวนหน้าทั้งหมด
if ( $page < 1 ) $page = 1; // ถ้าหน้าที่ต้องการแสดงผลน้อยกว่าหน้า 1 ให้กำหนดเป็นหน้า 1
if ( $page > $totalPage ) $page = $totalPage; // ถ้าหน้าที่ต้องการแสดงผลมากกว่าจำนวนหน้าทั้งหมด ให้กำหนดเป็นหน้าสุดท้าย
$startItem = ($page-1)*$itemsPerPage; // คำนวณหารายการแรกที่จะแสดงผล
$endItem = $startItem+$itemsPerPage-1; // คำนวณหารายการสุดท้าที่จะแสดงผล
// สร้างลิงค์สำหรับเลือกหน้าแสดงผล
echo "<table width=100%><tr><td nowrap width=100%>PageNavigator</td><td nowrap>Page :: $page</td>";
echo "<td nowrap>";
$self = $PHP_SELF; มัน Error ที่บรรทัดนี้ค่ะ เห็นในหนังสือเขาเขียนมาแบบนี้ แต่ดิฉันก็สงสัยอยู่ว่า ตัวแปร $PHP_SELF มันมาจากไหนเหรอค่ะ
// ถ้าหน้าที่กำลังแสดงผลเป็นหน้าแรก ก็ไม่ต้องแสดงลิงค์ไปหน้าแรก
if ( $page > 1) {
echo "<a href=$self?page=1 title=หน้าแรก>|<</a> ";
echo "<a href=$self?page=".($page-1)." title=หน้าก่อนนี้><</a> ";
}
// แสดงลิงค์ของทุกหน้า
for ( $i = 1; $i <= $totalPage; $i++ ) {
echo "<a href=$self?page=$i>";
echo "$i";
echo "</a> ";
}
// ถ้าหน้าที่กำลังแสดงผลเป็นหน้าสุดท้าย ก็ไม่ต้องแสดงลิงค์ไปหน้าสุดท้าย
if ( $page < $totalPage ) {
echo "<a href=$self?page=".($page+1)." title=หน้าถัดไป>></a> ";
echo "<a href=$self?page=$totalPage title=หน้าสุดท้าย>>|</a> ";
}
echo "</td></tr></table>";
$item = 0; // กำหนดตัวนับรายการเพื่อนำไปใช้ในการตรวจสอบว่ารายการอยู่ในหน้าที่ต้องการแสดงผลหรือเปล่า
// แสดงหัวของตาราง
echo "<table width=100% cellspacing=1 cellpadding=2>";
echo "<tr bgcolor=AA99DD align=center>";
echo "<td>no</td>";
echo "<td>date</td>";
echo "<td>address</td>";
echo "<td>call_number</td>";
echo "<td>page</td>";
echo "<td>access</td>";
echo "</tr>";
while ( List(,$row) = each($data) ) {
if ( $item >= $startItem and $item <= $endItem ) { // ตรวจสอบว่ารายการนี้อยู่ในหน้าที่ต้องการแสดงผลหรือไม่
echo "<tr align=center>";
echo "<td>".($item+1)."</td>";
while ( List($key,$value) = each($row) ) {
echo "<td>$value</td>";
}
echo "</tr>";
} elseif ( $item > $endItem ) break; // ถ้าหากรายการเกินจำนวนหน้าที่ต้องการแสดงผล ให้หยุดการทำงาน
$item++;
}
echo "</table>";
?>
Tag : - - - -
|
|
|
|
|
|
Date :
15 ส.ค. 2550 18:31:41 |
By :
somying |
View :
1861 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 2; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
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;
}
$strSQL .=" order by CustomerID ASC LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL);
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
</tr>
<?
}
?>
</table>
<br>
Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
<?
if($Prev_Page)
{
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
}
mysql_close($objConnect);
?>
</body>
</html>
|
|
|
|
|
Date :
2009-04-25 15:26:39 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|