[英]How to properly add pagination to a PHP file
I'm trying to add pagination to my query. 我正在尝试向我的查询添加分页。 For this example, I'm just using a previous and next link.
对于此示例,我仅使用上一个和下一个链接。 When I look at the page in the browser I get a Notice:
当我在浏览器中查看页面时,会收到一条通知:
Notice: Use of undefined constant url - assumed 'url' in C:\Apache24\htdocs\pagination-test.php on line 25.
My code is as follows: 我的代码如下:
<?php
$mysqli = new mysqli('localhost', 'root', 'myroot', 'corporate');
$pagesize = 4;
$recordstart = (isset($_GET['recordstart'])) ? $recordstart : 0;
$stmt = $mysqli->prepare("SELECT id AS `ID`, sku AS `SKU`, price AS `PRICE` FROM products ORDER BY id LIMIT ?, ?");
$stmt->bind_param("ii", $recordstart, $pagesize);
$result = $mysqli->query("SELECT count(id) AS count FROM products");
list($totalrows) = $result->fetch_row();
// Create the 'previous' link
if ($recordstart > 0) {
$prev = $recordstart - $pagesize;
$url = isset($_SERVER['PHP_SELF'])."?recordstart=$prev";
printf("<a href='%s'>Previous</a>", url);
}
// Create the 'next' link
if ($totalrows > ($recordstart + $pagesize)){
$next = $recordstart + $pagesize;
$url = $_SERVER['PHP_SELF']."?recordstart=$next";
printf("<a href='%s'>Next Page</a>", url);
}
Can anyone please help me solve this? 谁能帮我解决这个问题?
Thanks! 谢谢!
PS: Also, what is the ii used for in the bind_param statement? PS:此外,bind_param语句中使用的ii是什么? Shouldn't it just be $recordstart and $pagesize?
不应该只是$ recordstart和$ pagesize吗?
On line 25 you have 在第25行,您有
printf("<a href='%s'>Previous</a>", url);
You have defined $url but you missed the $ when you referenced it. 您已经定义了$ url,但是在引用它时错过了$。
You should have: 你应该有:
printf("<a href='%s'>Previous</a>", $url);
You have also made the same mistake in the next if
statement. 在下一个
if
语句中,您也犯了同样的错误。
Please try the following code, and if you don't understand I will provide you information. 请尝试以下代码,如果您听不懂,我会为您提供信息。
<?php
/* Please disable this before uploading to the remote server,
After you finished, then enable this so your application gain one more level of security...
http://php.net/manual/en/function.error-reporting.php
*/
error_reporting(0);
?>
<html>
<head>
<title>AwatITWork</title>
</head>
<body>
<?Php
require "config.php"; // All database details will be included here
$page_name="index2.php"; // If you use this code with a different page ( or file ) name then change this
$start=$_GET['start'];
if(strlen($start) > 0 and !is_numeric($start)){
echo "Data Error";
exit;
}
// This is the core of pagination and its simple :)
$eu = ($start - 0);
$limit = 5; // you can set how many record to be shown per page...
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
$nume = $dbo->query("select count(id) from products")->fetchColumn();
echo "<TABLE>";
echo "<tr><th>ID</th><th>SKU</th><th>Price</th></tr>";
$query=" SELECT * FROM products limit $eu, $limit ";
foreach ($dbo->query($query) as $row) {
$i=$i+1;
echo "<tr><td>$row[id]</td><td>$row[sku]</td><td>$row[price]</td></tr>";
}
echo "</table>";
if($nume > $limit ){
echo "<table align = 'center' width='50%'><tr><td align='left' width='30%'>";
if($back >=0) {
echo "<a href='$page_name?start=$back'>PREV</a>";
}
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $nume;$i=$i+$limit){
if($i <> $eu){
echo " <a href='$page_name?start=$i'>$l</a> ";
}
else {
echo "$l";
}
$l=$l+1;
}
echo "</td><td align='right' width='30%'>";
if($this1 < $nume) {
echo "<a href='$page_name?start=$next'>NEXT</a>";}
echo "</td></tr></table>";
}
?>
</body>
</html>
I am using PDO, 我正在使用PDO,
<?Php
$dbhost_name = "localhost";
$database = "stackoverflow";// database name
$username = "root"; // user name
$password = ""; // password
try {
$dbo = new PDO('mysql:host=localhost;dbname='.$database, $username, $password);
} catch (PDOException $e) {
echo "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
Please let me know if your problem doesn't solve yet. 如果您的问题仍未解决,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.