【发布时间】:2013-04-16 18:02:20
【问题描述】:
在我的项目中,我使用 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。
-
查询的样子如何?您的数据库中有多少行?代码长什么样子?
-
ovaistariq.net/404/… 的帖子上有一些有用的文章和讨论。请查看博文末尾的建议。
-
@Thomas Potaire 只有不到 1000 行。查询是最简单的查询,DB中只有4列,字符串,整数和文件系统中图像文件的路径。页面内容是图像和一些标题
-
我的意思是,如果您需要我们的帮助,我们确实需要查看一些代码/查询。
标签: php mysql performance pagination