ช่วยหน่อยดูโค้ดให้หน่อยคับ paging มันไม่ยอมลิ้งค์ให้ T T
คือผมจะทำการแบ่งหน้า Paging อะคับ มันแบ่งหน้าให้เฉพาะที่ตั้งไว้ หน้าที่เหลือมันไม่ยอมลิ้งไป อะคับ
ไฟล์ paging.php ฟังก์ชั่นแบ่งหน้า
Code
<?php
function paging_start_row($current_page,$rows_per_page)
{
return ($current_page-1)*$rows_per_page;
}
function paging_total_pages($total_rows,$rows_per_page)
{
return ceil($total_rows/$rows_per_page);
}
function paging_pagenum($cerrent_page,$total_pages,$page_range,$query_string)
{
$page_start=$current_page-$page_range;
$page_end=$current_page+$page_range;
if($page_start<1)
{
$page_end+=1 - $page_start;
$page_start=1;
}
if($page_end>$total_pages)
{
$diff=$page_end-$total_pages;
$page_start-=$diff;
if($page_start<1)
{
$page_start=1;
}
$page_end=$total_pages;
}
$url=$_SERVER['PHP_SELF']."?".$query_string;
$result="";
if($current_page>1)
{
$pg=$current_page-1;
$result.=" ";
$result.="<a href=\"$url&page=$pg\">ย้อนกลับ</a>";
}
if($page_start>1)
{
$pg=$page_start-1;
$result.=" ";
$result.="<a href=\"$url&page=$pg\">...</a>";
}
for($i=$page_start;$i<=$page_end;$i++)
{
$result.=" ";
if($i==$current_page)
{
$result.=$i;
}
else
{
$result.="<a href=\"$url&page=$i\">$i</a>";
}
$result.=" ";
}
if($page_end<$total_pages)
{
$pg=$page_end+1;
$result.=" ";
$result.="<a href=\"$url&page=$pg\">...</a>";
}
if($current_page<$total_pages)
{
$pg=$current_page+1;
$result.=" ";
$result.="<a href=\"$url&page=$pg\">ถัดไป </a>";
}
if($result=="")
{
return "1";
}
else
{
return $result;
}
}
?>
นี้ไฟล์ index.php ตารางแสดงผล
<?php
include"dbconnect.php";
include"paging.inc.php";
$current_page=1;
if(isset($_GET['page']))
{
$current_page=$_GET['page'];
}
$rows_per_page=5;
$start_row = paging_start_row($current_page,$rows_per_page);
$sql="SELECT SQL_CALC_FOUND_ROWS * FROM brands LIMIT $start_row , $rows_per_page;";
$result=mysql_query($sql,$con);
$found_rows=mysql_query("SELECT FOUND_ROWS();");
$total_rows=mysql_result($found_rows,0,0);
$total_pages = paging_total_pages($total_rows,$rows_per_page);
echo "<table border=1 cellpadding=5 cellspacing=0 align=center>
<tr bgcolor=#ccc>";
$num_fields=mysql_num_fields($result);
for($i=0; $i<$num_fields;$i++)
{
echo "<th>".mysql_field_name($result,$i)."<th>";
}
echo "</tr>";
while($data=mysql_fetch_array($result))
{
echo "<tr>";
for($i=0;$i<$num_fields;$i++)
{
echo "<td>".$data[$i]."</td>";
}
echo "</tr>";
}
echo "</table><p align=center>";
echo "หน้า : $current_page / $total_pages <br/>";
$page_range=1;
$qry_string="";
$page_str= paging_pagenum($current_page,$total_pages,$page_range,$qry_string);
echo $page_str;
mysql_close();
?>
<?php
include"dbconnect.php";
include"paging.inc.php";
$current_page=1;
if(isset($_GET['page']))
{
$current_page=$_GET['page'];
}
$rows_per_page=5;
$start_row = paging_start_row($current_page,$rows_per_page);
$sql="SELECT SQL_CALC_FOUND_ROWS * FROM brands LIMIT $start_row , $rows_per_page;";
$result=mysql_query($sql,$con);
$found_rows=mysql_query("SELECT FOUND_ROWS();");
$total_rows=mysql_result($found_rows,0,0);
$total_pages = paging_total_pages($total_rows,$rows_per_page);
echo "<table border=1 cellpadding=5 cellspacing=0 align=center>
<tr bgcolor=#ccc>";
$num_fields=mysql_num_fields($result);
for($i=0; $i<$num_fields;$i++)
{
echo "<th>".mysql_field_name($result,$i)."<th>";
}
echo "</tr>";
while($data=mysql_fetch_array($result))
{
echo "<tr>";
for($i=0;$i<$num_fields;$i++)
{
echo "<td>".$data[$i]."</td>";
}
echo "</tr>";
}
echo "</table><p align=center>";
echo "หน้า : $current_page / $total_pages <br/>";
$page_range=1;
$qry_string="";
$page_str= paging_pagenum($current_page,$total_pages,$page_range,$qry_string);
echo $page_str;
mysql_close();
?>
Tag : PHP
Date :
2013-12-19 14:23:04
By :
Aum
View :
840
Reply :
3
ลองดูดีๆครับ คนอื่นเค้าเอาสคริปต์นี้ไปใช้ เค้าก็ทำกันได้น่ะ
Date :
2013-12-19 14:25:44
By :
mangkunzo
Load balance : Server 00