code แบ่งหน้า PHP (comment บทความ)

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา code แบ่งหน้า PHP (comment บทความ)

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 } ?> 


Atomy_mink 183.89.127.xxx 08-08-2010 23:50:00

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1

จากคำสั่ง 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;



Ninenik 124.120.23.xxx 09-08-2010
 ความคิดเห็นที่ 2

แล้วถ้าหากกำหนด register_global=off; ควรใส่ยังไงดีคะ



atomy_mink 183.89.69.xxx 09-08-2010 19:29
 ความคิดเห็นที่ 3

ลองใส่ตามที่คุณ Ninenik บอกก็ใช้ได้อ่ะค่ะ แต่มาติดอยู่นิดนึงตรงที่สมมุติมีทั้งหมด 7 comment ต้องการโชว์หน้าละ 5 ซึ่งจะมีการแบ่งหน้าทั้งหมด 2 หน้า ซึ่งหน้าแรกโชว์comment ที่ 3-7 ส่วนหน้า 2 ต้องโชว์ 1-2 แต่ที่ทำพอรันแล้ว หน้าที่ 2 ดันโชว์เป็น 7-6

ต้องแก้โค้ดยังไงเหรอคะ



atomy_mink 183.89.69.xxx 09-08-2010 19:42
 ความคิดเห็นที่ 4

ตรงที่แสดงความ แทน
 

<?=$temp?>

ด้วย
 

<?=($temp-($chk_page*$e_page))?>

 



Ninenik 124.120.7.xxx 09-08-2010
 ความคิดเห็นที่ 5

ขอบคุณมากๆค่ะ



atomy_mink 183.89.69.xxx 09-08-2010 21:45
 ความคิดเห็นที่ 6

สอบถามหน่อยค่ะ พอลองใส่ข้อมูลตรง comment ไปเรื่อยๆ ปรากฎมันไม่แบ่งเป็นช่วงๆให้ค่ะเพิ่ม อย่าง comment มีทั้งหมด 10 หน้า

โชว์เป็น 1 2 3 4 5 6 7 8 9 10 แทนที่จะโชว์เป็น 1 2 3 4 5 ... 10 อ่ะค่ะ

ต้องแก้ไขตรงไหนเหรอคะ หาไม่เจออ่ะค่ะ



atomy_mink 58.8.216.xxx 17-08-2010 15:52
 ความคิดเห็นที่ 7

ข้อมูลจะแบ่งเป็นช่วงกรณี มีรายการจำนวนมาก ถ้าจำไม่ผิด มีมากกว่า หรือเท่ากับ 11 หน้าขึ้นไป



Ninenik 110.164.244.xxx 17-08-2010
 ความคิดเห็นที่ 8

 adsasdf



madeawsri 182.53.114.xxx 04-09-2011 20:20
 ความคิดเห็นที่ 9
fasdf


Weeradach Chinphi 202.176.115.xxx 17-04-2019 14:26
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ