<?php
$conn = mysql_connect("localhost","db_username","db_passwo rd");
mysql_select_db("db_name",$conn);
$qr=mysql_query("SELECT * FROM table ORDER BY userid DESC");//
$n_record=mysql_num_rows($qr);//so ban ghi cua bang
$p=5;//so bang ghi trong 1 trang can hien thi
//-------Ham tinh so trang-------------------------
function num_page()
{
global $n_record;//lay bien toan cuc
global $p;
if($n_record%$p==0)
{
$n_page=$n_record/$p;
return $n_page;
}
else
{
$n_page=($n_record-($n_record%$p))/$p+1;
return $n_page;
}
}
//-------------------------------------------------
function view_page($page_number)
{
global $n_record;
echo "<DIV align='right'>";
echo "Page ";
for($i=1;$i<=num_page();$i++)
{
if ($page_number == $i){
echo "<a href='salon_listing.php?n=".$i."'> ".$i."</a>";
}else{
echo "<a href='salon_listing.php?n=".$i."'> ".$i."</a>";
}
}
}
echo "</DIV>";
//-------------------------------------------------
//echo "<br>";
$n=$HTTP_GET_VARS['n'];//lay bien n tren trinh duyet
$s=($n-1)*$p; //thu tu cua bang ghi tai trang thu n
if ($n!='all')
{
$qr1=mysql_query("SELECT * FROM salon_users ORDER BY userid DESC limit $s,$p");
}
else
{
$qr1=mysql_query("SELECT * FROM salon_users ORDER BY userid DESC");
}
//--------Hien thi bang-----------------------------
view_page($n);
while($row=mysql_fetch_array($qr1))
{
$userid = $row['userid'];
$salon_name = $row['salon_name'];
$salon_address = $row['salon_address'];
$salon_phone = $row['salon_phone'];
$salon_fax = $row['salon_fax'];
$owner_email = $row['owner_email'];
echo "<table width='95%' border='0' align='center'>";
echo "<tr>";
echo "<td colspan='2'><a href='view_salon_detail.php?userid=$userid'><B>$sa lon_name</B></a></td>";
echo "</tr>";
echo "<tr>";
echo "<td width='2%'>salon_phone</td>";
echo "<td width='98%' valign='top'><div align='justify'>$overview <a href='view_salon_detail.php?userid=$userid'>View more...</a></div></td>";
echo "</tr>";
echo "</table><br>";
echo "<table width='90%' border='0' align='center' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td bgcolor='#666666'></td>";
echo "</tr>";
echo "</table>";
}
view_page($n);
?>
Thứ Năm, 15 tháng 10, 2009
Cần giúp code phân trang trong PHP
Đăng ký:
Đăng Nhận xét (Atom)
2 nhận xét:
cái này chạy sẽ báo lỗi Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\phantrang\page.php on line 58
, khi chưa chọn trang thì ko có biến n
Thêm điều kiện $n="" thì chạy ok hơn:
if ($n!='all')
{
if($n=="")
{
$qr1=mysql_query("SELECT * FROM salon_users ORDER BY userid DESC limit 1,$p");
}
else
{
$qr1=mysql_query("SELECT * FROM salon_users ORDER BY userid DESC limit $s,$p");
}
}
else
{
$qr1=mysql_query("SELECT * FROM salon_users ORDER BY userid DESC");
}
Đăng nhận xét