0

如何分页此脚本以在一页上显示 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

4

4 回答 4

3

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

例子 :

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

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

OFFSET $offset$offsetphp.ini 中增加的地方。

索引.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页等等你可以做到

于 2012-12-28T04:57:34.927 回答
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";

?>

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

完整的 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>
于 2012-12-28T05:01:00.783 回答
1

您可以将 LIMIT 放入您的 SQLQuerry

于 2012-12-28T04:51:32.113 回答
1

正如其他人已经说过的,您可以使用LIMIT offset, row_countMySQL 中的子句一次仅提取几行(或替代地LIMIT row_count OFFSET offset

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

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

还有更多细节需要介绍,例如空结果集以及page变量是否存在,但我将由您决定。

参考:

MySQL :: MySQL 5.6 参考手册 :: 13.2.9 SELECT 语法

于 2012-12-28T05:13:18.717 回答