code แบ่งหน้า PHP (comment บทความ)
พอดีทำโค้ด comment บทความแล้วมีปัญหาค่ะ
- ใส่ code แบ่งหน้าแล้วมีปัญหาค่ะ คือ www.xxx.com/content.php?cont_id=7 แต่พอใส่ code แบ่งหน้าแล้วเป็นแบบนี้ค่ะ www.xxx.com/content.php?s_page=0&urlquery_str= ทำให้เวลาคลิกลิ้งค์แล้วกลายเป็นหน้าเปล่าๆ
***จาก code แบ่งหน้า PHP ด้วย CSS
<?
$sql_comm="select * from tb_comment where cont_id='$cont_id' order by comm_id DESC";
$query_comm=mysql_query($sql_comm);
$total=mysql_num_rows($query_comm); //เรียงลำดับ comment จากมากไปน้อย
$temp=$total;
$temp=$temp+1;
$e_page=5;
if(!isset($_GET['s_page'])){
$_GET['s_page']=0;
}else{
$chk_page=$_GET['s_page'];
$_GET['s_page']=$_GET['s_page']*$e_page;
}
$sql_comm.=" LIMIT ".$_GET['s_page'].",$e_page";
$qr=mysql_query($sql_comm);
if(mysql_num_rows($qr)>=1){
$plus_p=($chk_page*$e_page)+mysql_num_rows($query_comm);
}else{
$plus_p=($chk_page*$e_page);
}
$total_p=ceil($total/$e_page);
$before_p=($chk_page*$e_page)+1;
while($fetch_comm=mysql_fetch_array($qr)){
$temp--;
?>
<div>
<table width="400" border="0" cellpadding="0" cellspacing="0" style="float:left; width:735px; margin:10px 0 10px 0;">
<tr>
<td width="12"><img src="image/comment_head_left.png" /></td>
<td width="711" style="background:url(image/comment_head.png) no-repeat top;">ความคิดเห็นที่ <?=$temp?> เรื่อง <?=$result[cont_name]?></td>
<td width="12"><img src="image/comment_head_right.png" width="12" height="25" /></td>
</tr>
<tr>
<td width="12" style="background:url(image/comment_detail_left.png) repeat-y top;"></td>
<td width="711" style="background:#b6b6b6 repeat top;">
<?
$detail=$fetch_comm[comm_detail];
$detail_rude=CheckRude($detail);
echo $detail_rude;
?>
</td>
<td width="12" style="background:url(image/comment_detail_right.png) repeat-y top;"></td>
</tr>
<tr>
<td width="12"><img src="image/comment_foot_left.png" /></td>
<td width="711" style="background:url(image/comment_foot.png) no-repeat top;">เขียนโดย :
<?
$detail_aut=$fetch_comm[comm_author];
$detail_rude_aut=CheckRude($detail_aut);
echo $detail_rude_aut;
?>
</td>
<td width="12"><img src="image/comment_foot_right.png" /></td>
</tr>
</table>
<? }?>
</div>
<?php if($total>0){ ?>
<div class="browse_page">
<?php
// เรียกใช้งานฟังก์ชั่น สำหรับแสดงการแบ่งหน้า
page_navigator($before_p,$plus_p,$total,$total_p,$chk_page);
?>
</div>
<?php } ?> adsasdf
ข้อมูลจะแบ่งเป็นช่วงกรณี มีรายการจำนวนมาก ถ้าจำไม่ผิด มีมากกว่า หรือเท่ากับ 11 หน้าขึ้นไป
สอบถามหน่อยค่ะ พอลองใส่ข้อมูลตรง comment ไปเรื่อยๆ ปรากฎมันไม่แบ่งเป็นช่วงๆให้ค่ะเพิ่ม อย่าง comment มีทั้งหมด 10 หน้า
โชว์เป็น 1 2 3 4 5 6 7 8 9 10 แทนที่จะโชว์เป็น 1 2 3 4 5 ... 10 อ่ะค่ะ
ต้องแก้ไขตรงไหนเหรอคะ หาไม่เจออ่ะค่ะ
ขอบคุณมากๆค่ะ
ตรงที่แสดงความ แทน
<?=$temp?>
ด้วย
<?=($temp-($chk_page*$e_page))?>
ลองใส่ตามที่คุณ Ninenik บอกก็ใช้ได้อ่ะค่ะ แต่มาติดอยู่นิดนึงตรงที่สมมุติมีทั้งหมด 7 comment ต้องการโชว์หน้าละ 5 ซึ่งจะมีการแบ่งหน้าทั้งหมด 2 หน้า ซึ่งหน้าแรกโชว์comment ที่ 3-7 ส่วนหน้า 2 ต้องโชว์ 1-2 แต่ที่ทำพอรันแล้ว หน้าที่ 2 ดันโชว์เป็น 7-6
ต้องแก้โค้ดยังไงเหรอคะ
แล้วถ้าหากกำหนด register_global=off; ควรใส่ยังไงดีคะ
จากคำสั่ง sql ที่ใช้ มีการส่งค่า ตัวแปร $cont_id
$sql_comm="select * from tb_comment where cont_id='$cont_id' order by comm_id DESC";
คิดว่าถ้า ตัวแปร $cont_id เป็นตัวแปร get ทีเราจะส่งไปพร้อมกับ url ตอนที่ทำการเลือกหน้า
ฟังก์ชันแบ่งหน้า จากบทความ คงต้องประยุกต์อีกนิดหน่อย
โดยให้เพิ่มเงื่อนไข ที่เราส่งตัวแปรเพิ่มเข้าไป ดังนี้
<?php
if(isset($_GET['cont_id'])){
$urlquery_str="&cont_id=".$_GET['cont_id'];
}
?>
โดยให้เพิ่มที่ตำแหน่งต่อไปนี้ ตามด้านล่าง
<?php if($total>0){ ?>
<div class="browse_page">
<?php
// เรียกใช้งานฟังก์ชั่น สำหรับแสดงการแบ่งหน้า
if(isset($_GET['cont_id'])){
$urlquery_str="&cont_id=".$_GET['cont_id'];
}
page_navigator($before_p,$plus_p,$total,$total_p,$chk_page);
?>
</div>
<?php } ?>
ระวังการใช้ตัวแปรแบบ global เช่น $cont_id กับ $_GET['cont_id'] จะไม่เท่ากับถ้า รันบน server ที่กำหนด register_global=off;
