【问题标题】:Paginate my Html / Php / MySql script为我的 Html / Php / MySql 脚本分页
【发布时间】:2012-12-28 15:39:39
【问题描述】:

如何分页此脚本以在一页上显示 5 行,在另一页上显示 5 行,依此类推。我已经为此尝试了教程和更多内容,但仍然无法获得它。

以下是我的代码:

<?php require "manybr.htm" ?>
<style>
<?php require "styles.css" ?>
</style>
<?php

$host="XXXXX"; // Host name 
$username="XXXX"; // Mysql username 
$password="XXXXX"; // Mysql password 
$db_name="XXXX"; // Database name 
$tbl_name="tylted"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// select record from mysql 
$sql="SELECT * FROM $tbl_name order by id desc";
$result=mysql_query($sql);
?>
<table background='images/view.png' width='50%' align='center'>
<tr>
<th align='center'>Group</th><th align='center'>Submition By</th><th align='center'>Submition On</th><th align='center'>ScreenName</th><th     align='center'>Password</th><th align='center'>Does This Work?</th><th align='center'>Vote</th>
</tr>
<tr>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td background='transparent' align='center'><b><a href="http://aol.cellufun.com/p/grp/grp.asp?v=??&grp=<? echo $rows['group']; ?>">{<? echo $rows    ['group']; ?>}</a> </b></td>
<td background='transparent' align='center'><b><a href="http://aol.cellufun.com/p/player.asp?v=&p=<? echo $rows['yname']; ?>"><? echo $rows['yname'];     ?><a> </b></td>
<td background='transparent' align='center'><b><? echo $rows['date']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['username']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['password']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['works']; ?>% Yes <font color='transparent'>||||</font>&nbsp; <? echo $rows['dworks']; ?>%     No</b></td>
<td background='transpatent' align='center'><b><a href='works.php?id=<? echo $rows['id']; ?>'><img src='images/ThumbsUp.png' height='30'     width='30'></a>&nbsp;&nbsp;&nbsp;<a href='dworks.php?id=<? echo $rows['id']; ?>'><img src='images/ThumbsDown.png' height='30' width='30'></a>

</td> 
</tr>

<?php
// close while loop 
}
?>

<?php
// close connection; 
mysql_close();
?>
</table>

我已将LIMIT 0 , 5 添加到我的查询中,现在出现了 5 个。我如何处理带有第 2 页链接的部分?

我还不知道MySqliPDO。我很快就会学会,所以请不要发表这种评论。我将很快将我的内联代码移动到 CSS 中,但还没有。我需要先完成这项工作。

<?php

$host="XXX"; // Host name 
$username="XXX"; // Mysql username 
$password="XXX"; // Mysql password 
$db_name="XXX"; // Database name 
$tbl_name="tylted"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


// select record from mysql 
 $offset=$_GET['p'];
 $sql="SELECT * FROM $tbl_name order by id desc LIMIT 4 OFFSET ".$offset;
 $result=mysql_query($sql);
?>
<table background='images/subbg.png' width='70%' align='center'>
<tr><th><?php require "links.php" ?></th></tr>
<tr>
<th align='center'>Group</th><th align='center'>Submition By</th><th    align='center'>Submition On</th><th align='center'>ScreenName</th><th   align='center'>Password</th><th align='center'>Does This Work?</th><th   align='center'>Vote</th>
</tr>
<tr>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td background='transparent' align='center'><b><a     href="http://aol.cellufun.com/p/player.asp?v=&p=<? echo $rows['yname']; ?>"><? echo   $rows['yname']; ?><a> </b></td>
<td background='transparent' align='center'><b><? echo $rows['date']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['username']; ?></b></td>
 <td background='transparent' align='center'><b><? echo $rows['password']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['works']; ?>% Yes <font    color='transparent'>||||</font>&nbsp; <? echo $rows['dworks']; ?>% No</b></td>
<td background='transpatent' align='center'><b><a href='works.php?id=<? echo   $rows['id']; ?>'><img src='images/ThumbsUp.png' height='30' width='30'>  </a>&nbsp;&nbsp;&nbsp;<a href='dworks.php?id=<? echo $rows['id']; ?>'><img   src='images/ThumbsDown.png' height='30' width='30'></a>

</td>
</tr>

<?php
// close while loop 
    }
?>

</table>

^^^^ 上面为 bozdoz 更新

【问题讨论】:

  • 请停止编辑我的 Q
  • 协作编辑是一项功能。请查看网站常见问题解答。 stackoverflow.com/faq#editing -- 请努力使用正确的拼写、语法、大写和标点符号,您会发现其他人需要不那么频繁地编辑您的帖子。
  • 在该示例中,您必须设置默认的 $offset。您还应该检查 $_GET['p'] 是否设置为 if(isset($_GET['p'])&&is_int($_GET['p'])){ ... }
  • @Charles 他们正在编辑以缩短我需要的内容,我感谢他们,但后来人们来回改变了它
  • 我完全是 php 新手,老实说,我已经拥有这个脚本大约 3 周了,让它看起来像我想要的,并且像我希望我从编辑中学到更多东西一样设置,但现在我不知道是什么你说:c

标签: php html mysql pagination


【解决方案1】:

在您的 SQL 查询中附加 LIMIT 5 OFFSET 1 以在第一页显示 5 个结果。为下一页增加偏移值。

例子:

OFFSET 2 为您提供接下来的 5 个结果,依此类推...

要放置下一页的链接,您可以尝试将偏移值设置为变量。

OFFSET $offset $offset 在 php 中递增。

index.php

<?php
 if(isset($_GET['p'])){
   if(is_numeric($_GET['p']))
      $offset=$_GET['p'];
   else 
      $offset=1;
  }
 else 
  $offset=1;

 $sql="SELECT * FROM $tbl_name order by id desc LIMIT 5 OFFSET ".$offset;
 $result=mysql_query($sql);
?>

然后为链接

<a href='index.php?p=2'>2</a>

应该显示 2 页等等你可以做到

【讨论】:

  • 我添加了 LIMIT 0 , 5 到它现在显示 5 个结果我如何做链接来显示其他 5 个结果?
  • //从mysql中选择记录 $offset=1; $sql="SELECT * FROM $tbl_name order by id desc LIMIT 5 OFFSET ".$offset; $result=mysql_query($sql); ?>
  • 警告:mysql_fetch_array():在对脚本执行此操作后,mysql_fetch_array():在第 53 行的 /home/a7972613/public_html/vp.htm 中提供的参数不是有效的 MySQL 结果资源 // 从 mysql 中选择记录 $偏移量=$_GET['p']; $sql="SELECT * FROM $tbl_name order by id desc LIMIT 5 OFFSET ".$offset; $result=mysql_query($sql); ?>
  • 返回 下一个
  • 我点击了 2 并加载了页面
  • 好的,所以我将限制更改为每页 2 个,并将其更改为 p=1 和 p=2 和 p=3 它有效,但如果我只是去没有 ?p=1 的 url 它给出我这个错误我也必须在 url 中手动输入它,它使链接自动判断有多少行和限制
  • 2
【解决方案2】:

使用$_GET 变量获取限制:

让 URL 类似于 index.php?p=5 吧。

PHP 可以像这样包含 $_GET 变量:

<?php

$startnum = 0; //set default
if(isset($_GET['p'])&&is_numeric($_GET['p'])){
  $startnum = $_GET['p'];
}
$sql="SELECT * FROM $tbl_name order by id desc LIMIT $startnum, 5";

?>

下一个和上一个链接可以是&lt;a href="?p=&lt;?php echo $startnum+5 ?&gt;"&gt;Next&lt;/a&gt;

完整的 PHP 在这里:

<style>
<?php require "styles.css" ?>
</style>
<?php

$host="XXXXX"; // Host name 
$username="XXXX"; // Mysql username 
$password="XXXXX"; // Mysql password 
$db_name="XXXX"; // Database name 
$tbl_name="tylted"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$startnum = 0; //set default
if(isset($_GET['p'])&&is_numeric($_GET['p'])){
  $startnum = $_GET['p']; //change offset
}

$sql="SELECT * FROM $tbl_name order by id desc LIMIT $startnum, 5";
$result=mysql_query($sql);
?>


See the <a href="?p=<?php echo $startnum+5; ?>">next page</a>
<table background='images/subbg.png' width='70%' align='center'>

<tr><th><?php require "links.php" ?></th></tr>
<tr>
<th align='center'>Group</th><th align='center'>Submition By</th><th    align='center'>Submition On</th><th align='center'>ScreenName</th><th   align='center'>Password</th><th align='center'>Does This Work?</th><th   align='center'>Vote</th>
</tr>
<tr>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td background='transparent' align='center'><b><a     href="http://aol.cellufun.com/p/player.asp?v=&p=<? echo $rows['yname']; ?>"><? echo   $rows['yname']; ?><a> </b></td>
<td background='transparent' align='center'><b><? echo $rows['date']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['username']; ?></b></td>
 <td background='transparent' align='center'><b><? echo $rows['password']; ?></b></td>
<td background='transparent' align='center'><b><? echo $rows['works']; ?>% Yes <font    color='transparent'>||||</font>&nbsp; <? echo $rows['dworks']; ?>% No</b></td>
<td background='transpatent' align='center'><b><a href='works.php?id=<? echo   $rows['id']; ?>'><img src='images/ThumbsUp.png' height='30' width='30'>  </a>&nbsp;&nbsp;&nbsp;<a href='dworks.php?id=<? echo $rows['id']; ?>'><img   src='images/ThumbsDown.png' height='30' width='30'></a>

</td>
</tr>

<?php
// close while loop 
    }
?>

</table>

【讨论】:

  • 我用这个替换了旧的,但页面仍然没有改变页面 url,但它显示了相同的帖子,代码如下:// select record from mysql $startnum = 0; //设置默认值 if(isset($_GET['p'])&&is_int($_GET['p'])){ $startnum = $_GET['p']; } $sql="SELECT * FROM $tbl_name order by id desc LIMIT $startnum, 5"; $result=mysql_query($sql); ?>
  • 警告! 您的代码示例包含SQL injection vulnerability——您将原始、未经过滤、未经验证的用户输入直接传递到 SQL 字符串中。请更正您的示例以消除漏洞。
  • Charles,什么漏洞?
  • @Charles,什么漏洞?
  • @bozdoz,哦,我完全错过了那里的is_int。我期待的是演员阵容,而不是验证检查。没关系。伙计,我不是机器人。 *哔声*
  • 下一步
【解决方案3】:

你可以把 LIMIT 放在你的 SQLQuerry 中

【讨论】:

  • 如果我设置一个限制说 5 是它我如何包含到第二页的链接 5 更多??
  • 如果我添加 SELECT * FROM tylted LIMIT 0, 5 我将如何链接其他页面??
  • 使用 $_GET 变量从 URL 中获取页码。如果页面是 home.html?page=5 那么限制可以是 LIMIT $_GET['page'], 5 也就是限制 5,从第 5 行开始。
【解决方案4】:

正如其他人已经说过的,您可以使用LIMIT offset, row_count clause in MySQL to extract only a few rows at a time (or alternatively LIMIT row_count OFFSET offset)

要获得该偏移量,您可以从可以命名为 page 的 URL 中获取一个值,并将该变量放入到下一页和上一页的链接中。

例如假设页面上显示了五个项目。在第一页上,获取 5 行,以及所有项目的计数。如果还有更多项目,请添加指向results.php?page=2 的链接。从那个变量,我们知道我们的偏移量是多少。由于第一页的偏移量(隐式)为零,第二页为 5,第三页为 10,我们可以提取一个简单的公式:offset = ($_GET["page"]-1) * items_per_page

还有更多细节需要介绍,例如空结果集以及是否存在 page 变量,但我会留给你。

参考:

MySQL :: MySQL 5.6 Reference Manual :: 13.2.9 SELECT Syntax

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签