如何对这个脚本进行分页,以便在一个页面上显示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> <? 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> <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页的部分?
我还不知道MySqli
或PDO
。我很快就会知道,所以请不要发表评论。我很快就会把我的内联代码移到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> <? 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> <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
在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页等等你可以这样做
使用$_GET
变量获取限制:
将URL设为index.php?p=5
。
<?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> <? 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> <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>
您可以在sqlquery中添加LIMIT
正如其他人已经说过的,您可以使用MySQL中的LIMIT offset, row_count
子句一次只提取几行(或者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 Syntax