在我的項目中,我已經使用PHP完成了分頁,我做了如下: 每個按鈕(下一頁和上一頁)都是超鏈接,鏈接到同一頁並傳遞GET變量(PAGENUMBER)。對於每個頁面更改,都有腳本用於如何提高分頁速度
- 連接到數據庫。
- 查詢指定頁面的數據。
- 關閉
- 數據庫連接。顯示數據。
問題是它的運行速度很慢。每次連接到數據庫並查詢數據並關閉數據庫連接時,是否還有其他方法?
另外,在分頁期間,我只想重新加載數據庫中的數據,頁面中的所有其他內容保持不變並且不想重新加載。如何實現這一目標?
的腳本如下
<?php
require_once("./include/membersite_config.php"); //include files for login system
if($fgmembersite->CheckLogin())
{
$login=true;
}
else
{
$login=false;
}
require_once("db.php");
$con=connect(); //connect to db and select the db
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<HEAD>
<TITLE> Gallery </TITLE>
<META name="generator" content="Adobe Photoshop(R) CS Web Photo Gallery">
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="images/galleryStyle.css" rel="stylesheet" type="text/css">
<link rel="STYLESHEET" type="text/css" href="style/new.css" />
</HEAD>
<body marginheight=0 marginwidth=0 leftmargin=0 topmargin=0>
<table><tr>
<?php
//////////////////DISPLAYING IMAGE THUNBNAILS//////////////////////
$query="SELECT COUNT(*) FROM gallery ";
$res = query($query);
$raw=mysql_fetch_array($res);
$imagecount=$raw[0];
if($imagecount==0) { die ("No images found"); }
$pagecount=ceil($imagecount/15);
$page = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 1;
if (isset($_GET['page']))
{
if (!(ctype_digit($_GET['page']) and $_GET['page']>0 and $_GET['page']<=$pagecount))
{
die ("You are not autorised to viw this page");
}
}
$start= ($page*15)-15;
$query = "SELECT * FROM gallery ORDER BY imageid DESC LIMIT $start , 15";
$res = query($query);
$count= mysql_num_rows($res);
$index=0;
for($i=1;$i<=3;$i++)
{
?>
<TR>
<?php
for($j=1;$j<=5;$j++)
{
if(!$raw=mysql_fetch_array($res)) {break;}
$index++;
$imageid=$raw['imageid'];
$thumbpath= $raw['thumbpath'];
$largepath=$raw['largepath'];
$caption=$raw['caption'];
?>
<A name=1 href="image.php?imageid=<?php echo $imageid ?>&imageindex=<?php echo ((($page-1)*15)+$index-1) ; ?> "><IMG border=0 src="<?php echo $thumbpath; ?>" height="75" width="75" alt="<?php echo $caption ?>" title="<?php echo $caption ?>" ></A><BR>
<?php
echo "<a href='delete.php?delete=yes&imageid=".$imageid.'&page='.$page."'>Delete </a>"
echo "<a href='replace.php?update=yes&imageid=".$imageid.'&page='.$page."'>Replace </a>"
?>
</table>
</td>
<?php } ?>
</TR>
<?php
}
if ($page!=1)
{
?>
<td width=17><a href=" <?php echo $_SERVER['PHP_SELF'].'?page='.($page-1); ?> "><IMG SRC="images/previous.gif" BORDER=0></a></td>
<?php } ?>
<td align=middle class="pagenums"><?php echo "page ".$page." of ".$pagecount ?></td>
<?php
if ($page!=$pagecount)
{
?>
<td align=right width=17><a href=" <?php echo $_SERVER['PHP_SELF'].'?page='.($page+1); ?> "><img border=0 src="images/next.gif"></a></td>
<?php } ?>
</table>
</body>
</html>
您可以使用AJAX爲了這。計算一次計數並在您的AJAX請求中使用iot。 – 2013-03-16 06:52:27
查詢如何?數據庫中有多少行?代碼是怎麼樣的? – 2013-03-16 06:56:36
有一些有用的文章和討論http://www.ovaistariq.net/404/mysql-paginated-displays-how-to-kill-performance-vs-how-to-improve-performance/。檢查博客文章末尾的建議。 – Wasif 2013-03-16 07:26:31