我想在20个条目后停止该行,并在下一侧的同一点返回。简而言之,我希望每个站点只有20个条目。
这是我的代码:
<html>
<head>
<title>server ranking</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="layout.css">
</head>
<body>
<?PHP
$verbindung = mysql_connect("localhost", "root", "") or die("Fehler bei der datenbank");
mysql_select_db("users") or die ("datenbank existiert nicht")
?>
<?PHP
$abfrage = "SELECT * FROM `ranking` ORDER BY `points` DESC ";
$ergebnis = mysql_query($abfrage);
$zaehler = 0;
?>
<table rules="all" style="90%;">
<caption>ViceVice Server Ranking</caption>
<tr style="text-align:center; vertical-align:middle;">
<td>Rang</td>
<td>Benutzername</td>
<td>Punkte</td>
</tr>
<?PHP
while($row = mysql_fetch_object($ergebnis))
{
$zaehler = $zaehler + 1;
?>
<tr>
<td style="text-align:center; vertical-align:middle; width: 50;">
<?PHP
echo $zaehler;
?>
</td>
<td style="text-align:center; vertical-align:middle; width: 50;">
<img src="https://minotar.net/helm/<?php echo $row->user; ?>/25.png">
</td>
<td style="text-align:left; vertical-align:middle; width: 200;">
<?PHP
echo $row->user;
?>
</td>
<td style="text-align:left; vertical-align:middle; width: 100;">
<?PHP
echo $row->points;
?>
</td>
</tr>
<?PHP
}
?>
</table>
</body>
</html>
I would thank you for any help ;D
这并不是一个明确的答案,因为有很多方法可以实现OP的要求。然而,它太长了,不能作为评论发布。此方法使用简单的URL参数来指定分页(start
显示要开始的记录,show
指定要显示的记录数量),并使用MySQL的LIMIT
子句来限制数据库输出。显然,您必须检查$_GET参数中的值是否合理。如果用户更改URL以读取page.php?start=hello&show=world
,则您的查询将失败。。。为了简洁起见,我省略了数据卫生。
page.php:
<?php
$sql = "SELECT id, description FROM mytable ORDER BY id";
// if there is the "start" url parameter, use it. Else, start from zero.
$start = isset($_GET['start']) ? $_GET['start'] : 0;
// if there is the "show" url parameter, user it. Else, use default 20 records.
$show = isset($_GET['show']) ? $_GET['show'] : 20;
$sql .= " LIMIT $start,$show";
// doDatabaseQuery is a fictitious function which we assume
// returns an array of objects. Comment this line if using test data, below
$records = doDatabaseQuery($sql);
// if you want to test this with some fake data, uncomment this code section
// which creates 200 fake records, and puts them into an
// array then array_slice acts like the "LIMIT" clause in the mysql query
/*
for($x=1;$x<=200;$x++) {
$o = new stdClass();
$o->id = $x;
$o->description = "Description for object $x";
$records[] = $o;
}
$records = array_slice($records, $start, $show);
*/
// set the "start" parameter value in the "Back" link
// if it's less than zero, we set it to zero.
$previous_start = $start - $show;
if($previous_start < 0) $previous_start = 0;
// set the "start" parameter value in the "Next" link
// if we wanted to do something more advanced, we would check
// this value is not greater than the number of records in the table
$next_start = $start + $show;
include "page.html";
?>
page.html:
<html>
<head>
<title>Test Page</title>
</head>
<body>
<h1>Test Pagination Page</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php for($i=0; $i < count($records); $i++): ?>
<tr>
<td><?php echo $records[$i]->id; ?></td>
<td><?php echo $records[$i]->description; ?></td>
</tr>
<?php endfor; ?>
</tbody>
<tfoot>
<tr>
<td><a href="page.php?start=<?php echo $previous_start;?>&show=<?php echo $show;?>">Back</a></td>
<td><a href="page.php?start=<?php echo $next_start;?>&show=<?php echo $show;?>">Next</a></td>
</tr>
</tfoot>
</table>
</body>
</html>
优点:允许非常基本的分页,但可以很容易地调整以提供"最后一页"one_answers"第一页"功能。
缺点:允许用户手动操作URL;想象一个有2000000条记录的表——用户可以将show
参数设置为2000000,并让服务器做大量工作来从表中提取所有记录。。。为了避免这种情况,您可以将代码中的$show
变量限制为最大值,或者将其从URL中完全删除并保持为固定值。