如何正确将分页添加到PHP文件

时间:2018-10-03 18:04:51

标签: php pagination

我正在尝试向查询添加分页。对于此示例,我仅使用上一个和下一个链接。当我在浏览器中查看页面时,会收到一条通知:

Notice: Use of undefined constant url - assumed 'url' in C:\Apache24\htdocs\pagination-test.php on line 25.

我的代码如下:

<?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);
}

有人可以帮我解决这个问题吗?

谢谢!

PS:而且,bind_param语句中使用的ii是什么?应该不是$ recordstart和$ pagesize吗?

2 个答案:

答案 0 :(得分:1)

第25行有

printf("<a href='%s'>Previous</a>", url);

您已经定义了$ url,但是在引用它时错过了$。

您应该具有:

printf("<a href='%s'>Previous</a>", $url);

您在下一个if语句中也犯了同样的错误。

答案 1 :(得分:1)

请尝试以下代码,如果您不理解,我会为您提供信息。

<?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>

我正在使用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();
}
?> 

如果您的问题仍未解决,请告诉我。