|
 |
|
มีปัญหา เรื่อง Code แบ่งหน้าครับ รบกวนช่วยดูหน่อยครับ |
|
 |
|
|
 |
 |
|
ผมไปก๊อปเอา code แบ่งหน้าจากตัวอย่างด้านล่างนี้ไปใช้ แล้วผมมีปัญหาตรงที่
url ของผมเป็นแบบนี้ index1.php?id=101113&st=1&sh=gal&by=0&page=1
ตรง page=1 ผมลองแก้เป็น page=0 หรือ page=-1 เพื่อส่งค่าที่ไม่มีจริงเข้าไป มันจะฟ้องแบบนี้ครับ
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\AppServ
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\AppServ
และอีกปัญหาคือ ถ้าเราใส่ค่ามากกว่าจำนวนหน้าที่มันนับได้ ( สมมุตินับหน้า $total ได้ค่า page=14 )
และ url ของผมคือ index1.php?id=101113&st=1&sh=gal&by=0&page=14
ผมส่งค่าที่ไม่มีจริง ไปแทนคือ page=15
มันจะแสดง ออกมาแบบล่างนี้อ่ะครับ
« Previous 13 14 [127-126] of 125
มันจะแสดงค่า page ที่ไม่มีจริงออกมา และไม่สามารถดึงข้อมูลออกมาแสดงได้ เพราะ $_GET ค่าที่ไม่มีจริง
ที่ผมอยากได้คือ ผมอยากจะได้เงื่อนไข คือ ถ้าใส่ค่าที่ไม่มีจริงมา
เช่นถ้าใส่ page=0 หรือ page=-1 เข้ามา ก็ให้แสดงหน้าแรก คือ page=1 แทน
ถ้าใส่ page=15 ก็ให้แสดงหน้าสุดท้าย คือ page=14 แทน
ประมาณนี้อ่ะครับ รบกวนผู้รู้ช่วยแนะนำด้วยครับ
นี่ครับ ตัวแบ่งหน้า ที่ผม copy ไปใช้
Code (PHP)
<?php
#######################################################################
# $page = (isset($_GET['page']))? intval($_GET["page"]) : 0;
# $limit_end = 20;
# $limit_start = ($page-1)*$limit_end;
# $sql = "select * from table limit " . $limit_start. "," . $limit_end;
# page_navi($page, $limit_end, 4, 1000, "_self", "current_page", "other_page", $querystring);
#######################################################################
# $page = หน้าปัจจุบัน
# $limit= จำนวน record ต่อหน้า
# $range= บวกลบจากหน้าปัจจุบัน ใส่ 4 จะได้ตัวเลขออกมา 9
# $count= จำนวน record ทั้งหมด
# $send= ส่งตัวแปรอะไรไปด้วย
# $current_style = css สำหรับหน้าปัจจุบัน
# $other_style = css สำหรับอื่นๆ รวมถึง Prev Next First Last
# $target= อันนี้รู้ๆ กัน
#######################################################################
function page_navi($page, $limit, $range, $count, $send=NULL, $current_style="navi_on", $other_style="navi_out", $target="_self"){
$output = "";
$total = ceil($count/$limit);
$navi_start = $page-$range;
$navi_end = $page+$range;
$send .= (!empty($send))? "&" : NULL;
if($navi_start <= 0) $navi_start = 1;
if($navi_end >= $total) $navi_end = $total;
if($page>1){
$navi_back = $page-1;
if($page > 2)
$output .= "<a href=\"?" . $send . "page=1\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>«</strong></a> ";
$output .= "<a href=\"?" . $send . "page=" . $navi_back . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>‹</strong></a> ";
}
for($i = $navi_start; $i <= $navi_end; $i++){
if($i == $page)
$output .= "<a href=\"?" . $send . "page=" . $i . "\" target=\"" . $target . "\" class=\"" . $current_style . "\"><strong><font color=\"" . $other_style . "\">$i</font></strong></a> ";
else
$output .= "<a href=\"?" . $send . "page=" . $i . "\" target=\"" . $target . "\" class=\"" . $other_style . "\">$i</a> ";
}
if($page < $total){
$navi_next = $page+1;
$output .= "<a href=\"?" . $send . "page=" . $navi_next . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>›</strong></a> ";
if(($page+1) < $total)
$output .= "<a href=\"?" . $send . "page=" . $total . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>»</strong></a>";
}
if($navi_start>$navi_end)
$output .= "<a href=\"?" . $send . "page=" . $page . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>$page</strong></a> ";
return $output;
}
?>
Code (PHP)
<?php
$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end = 2;
$limit_start = ($page-1)*$limit_end;
$send = "";
if(isset($_GET["id"]) and $_GET["id"] != ""){
$send = "id=" . $_GET["id"];
$condition= " WHERE category_id = '" . $_GET["id"] . "'";
}
$sql = "SELECT * FROM product" . $condition;
$query_id = mysql_query($sql . " LIMIT " . $limit_start. "," . $limit_end, $connection_id);
$count = mysql_num_rows($query_id);
$total = mysql_num_rows(mysql_query($sql, $connection_id));
if($total > 0){
while($rows = mysql_fetch_assoc($query_id)){
echo $rows["product_name"]."<br>";
}
echo "\n<div style=\"float:left;\">Page : " . page_navi($page, $limit_end, 4, $total, $send) . " </div><div style=\"float:right;\">[" . ($limit_start+1) . "-" . ($limit_start+$count) . "] of <strong>" . $total . "</strong></div>";
}
?>
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2009-12-05 22:15:57 |
By :
basic111 |
View :
940 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือผมไม่มีเวลามากนะ โทษทีคับ
if( ! $count) ไม่มีหน้า ให้แก้ sql ไปที่หน้า 1 ไม่ก็เอา error แสดง
|
 |
 |
 |
 |
Date :
2009-12-05 22:27:20 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โคดนี้ต้องตามพี่ PlaKrim มาช่วยดู .... อ้ายอั๋นๆอยู่ไสๆ
|
 |
 |
 |
 |
Date :
2009-12-06 00:46:34 |
By :
panyapol |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
if($total > 0){
if($count > 0){
while($rows = mysql_fetch_assoc($query_id)){
echo $rows["product_name"]."<br>";
}
echo "\n<div style=\"float:left;\">Page : " . page_navi($page, $limit_end, 4, $total, $send) . " </div><div style=\"float:right;\">[" . ($limit_start+1) . "-" . ($limit_start+$count) . "] of <strong>" . $total . "</strong></div>";
}else{
echo "ไม่พบข้อมูล";
}
}else{
echo "ไม่พบข้อมูล";
}
แนะนำให้ฟ้องเออเร่อดีกว่าครับ
ถ้าจะทำจริงๆ มันหลายขั้นตอนเปลืองเปล่าๆ
|
 |
 |
 |
 |
Date :
2009-12-06 13:08:25 |
By :
plakrim |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|