0

在我的项目中,我使用 PHP 进行了分页,我做了如下操作:每个按钮(下一页和上一页)都是超链接,它们通过 GET 变量(PAGENUMBER)链接到同一页面。对于每个页面更改,都有脚本

  1. 连接到数据库。
  2. 查询指定页面的数据。
  3. 关上
  4. 数据库连接。显示数据。

问题是它工作缓慢。每次连接到数据库并查询数据并关闭数据库连接时,是否有任何替代方法。

此外,在分页期间,我只想重新加载数据库中的数据,页面中的所有其他内容保持不变,不想重新加载。如何做到这一点?

脚本如下

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

3 回答 3

1

有很多选择

  1. 使用 javascript 分页
  2. ajax 分页
  3. 尽量减少分页代码(优化代码)
于 2013-03-16T07:38:01.723 回答
0

您不需要为每个页面更改连接/关闭数据库

于 2013-03-16T07:14:21.990 回答
0

使用 ajax 脚本连接和获取数据库值并重新加载当前页面你可以做到 -- -- -- --- -- -- 尝试 ajax

于 2013-03-16T08:02:25.303 回答