我试图获得大约200条记录,每页50条记录。所以平均四页。现在这是我在pdo尝试它的第一次尝试。我知道我错过了一些东西,我无法弄清楚我做错了什么或遗漏了。所以任何帮助将不胜感激。代码将从第0行拉到第50行。但是当我点击页码时,除了前50个记录之外,它不会去任何地方。我已经移动了很多次变量,所以看看它们的名字大声笑。
//above this is just db connection and $condition variable
<table width="100%" cellpadding="4" cellspacing="6" align="left">
<colgroup>
<col span="3" style="background-color:black">
<col style="background-color:black">
</colgroup>
<tr>
<th width="5%">No.</th>
<th width="15%">Rank | Chain</th>
<th width="25%">Username</th>
<th width="15%">Last Sab</th>
<th width="15%">AAT</th>
<th width="20%">Sentry Rating</th>
</tr>
<?php
if (isset($_GET['pagenum'])) {
// $page = $_GET['page'];
$pagenum = $_GET['pagenum'];
} else {
//$page = 1;
$pagenum=1;
}
$pagenum = (int)$pagenum;
$query = $conn->prepare("SELECT `stats`.`name`, `stats`.`active`, `stats`.`id`, `stats`.`strike`, `stats`.`defense`, `stats`.`sentry`, `stats`.`spy`, `stats`.`rank`, `stats`.`maxed`, `chains`.`name`, `chains`.`tag`, ((`stats`.`bpm`*1000000)+(`stats`.`ch`*450000)+(`stats`.`is`*1000000)+(`stats`.`ds`*200000)+(`stats`.`nun`*1000000)+(`stats`.`lt`*1000000)+(`stats`.`key`*600000)+(`stats`.`dog`*250000)) AS `tvalue` FROM `stats`, `chains` WHERE ".$condition." ORDER BY `tvalue` DESC LIMIT :start_from, :items_per_page");
$query->bindParam(':start_from', $start_from);
$query->bindParam(':items_per_page', $items_per_page);
$start_from = 0;
$items_per_page= 50;
$query->execute();
$results = $query->fetchAll(PDO::FETCH_ASSOC);
$start_from = ($pagenum -1) * $items_per_page;
if ($query->rowCount() > $items_per_page) {
$start_from = ($pagenum -1) * $items_per_page;
}
// Loop through results
foreach($results as $data){
// Display results in HTML table
echo "<tr>";
// Add/Remove your column names here
echo "<td>". $num++ ."</td>";
//echo "<td>". $data['rank'] ."</td>";
echo "<td>".$data['rank']." |". $data['tag'] ."</td>";
echo "<td>". $data['name'] ."</td>";
echo "<td>$pagenum</td>";//Last sab column
echo "<td></td>";//AAT column
echo "<td><em><font color='yellow'>". number_format($data['sentry']) ."</font></em></td>";
// Close HTML table row
echo "</tr>";
}
$query2 = $conn->prepare("SELECT `stats`.`name`, `stats`.`active`, `stats`.`id`, `stats`.`strike`, `stats`.`defense`, `stats`.`sentry`, `stats`.`spy`, `stats`.`rank`, `stats`.`maxed`, `chains`.`name`, `chains`.`tag`, ((`stats`.`bpm`*1000000)+(`stats`.`ch`*450000)+(`stats`.`is`*1000000)+(`stats`.`ds`*200000)+(`stats`.`nun`*1000000)+(`stats`.`lt`*1000000)+(`stats`.`key`*600000)+(`stats`.`dog`*250000)) AS `tvalue` FROM `stats`, `chains` WHERE ".$condition." ORDER BY `tvalue` ");
$query2->execute();
$total_records = $query2->rowCount();
// Keep a record of total number of rows
$total_rows = $total_records;
// Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $items_per_page);
// Going to first page
echo "<center><a href='stats.php?id=newwartab&pagenum=1'>First Page</a> ";
// Showing number of pages in between last page
for ($i=1; $i<=$total_pages; $i++){
echo "<a href='stats.php?id=newwartab&pagenum=". $i ."'>". $i ."</a> ";
}
// Going to last page
echo "<a href='stats.php?id=newwartab&pagenum=". $total_pages ."'>Last Page</a></center> ";
// Calculate first and last item on current page
$first = $pagenum * $items_per_page - $items_per_page;
$last = $pagenum * $items_per_page;
// Make sure the number can never be more than the total numer of rows
if($last > $total_rows){
$last = $total_rows;
}
// Showing the results
echo "<br />";
echo "<center>Showing ". $first ." to ". $last ." in total sab targets ". $total_rows ."</center>";
?>
</table>
很多注释掉的东西。但希望无论谁帮助我,都可以查看一些不良编码,我会再次感谢所有可以提供的帮助,因为我在这一段代码上工作了太长时间,并希望完成它。
答案 0 :(得分:0)
您的查询错误
$query = $conn->prepare("SELECT ... DESC LIMIT $pagenum, $items_per_page");
不应该是页码,它应该是偏移量,
($pagenum -1) * $items_per_page
因此,在页面1
上(1-1)*50
或0
表示偏移量,页面为2
(2-1)*50
或50
等
然后限制为50,您应该获得记录0-49
,50-99
或类似内容。
除非页码不是页码,因为如果这是第一页的内容。
$query = $conn->prepare("SELECT ... DESC LIMIT 1, 50");
第2页
$query = $conn->prepare("SELECT ... DESC LIMIT 2, 50");
此外,您的查询应该在这里
//$pagenum = 1;
if (isset($_GET['pagenum'])) {
// $page = $_GET['page'];
$pagenum = $_GET['pagenum'];
} else {
//$page = 1;
$pagenum=1;
}
//$page=1;
//<-- query should be after getting the page stuff from URL
$results = $query->fetchAll(PDO::FETCH_ASSOC);
如果要将它们合并到查询中,至少应该将它们转换为int以防止SQL注入。
$pagenum = (int)$pagenum; //cast value to int.
使用预备语句进行查询以防止SQLInjection。