【发布时间】:2016-02-26 02:25:42
【问题描述】:
嘿,我正在尝试为一家书店设置分页结果,但我不知道为什么我的其他页面的链接不起作用。当我运行它时,原始页面会加载结果,但是当我转到下一页时,它会显示未找到项目。
<?php
$host = '???'; //IP Address on domain name of the host for the database
$user ='???'; //the name of the user
$pass="???";//Our Password
//make the connection to the database
$con=mysql_connect($host,$user,$pass) or
die("Error connecting to Database");
$dbname = "???"; //we had a database name ???
mysql_select_db($dbname);
?>
<?php
echo "<br><h3><u>Available Books for Sell</u></h3><br>"; // promps user to order a book
$per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
$query="select * from Books limit $start_from,$per_page"; // create query using the $_GET 'id' sent
$result=mysql_query($query); //results from executing the mysql query
if(!$result) // if not results
{
echo" get Items error"; // print error
exit;
}
while($row=mysql_fetch_array($result)) // while there are results
{
echo
"<br>----------------------------------------------------------------------
<br>Title: ".$row['Title'].
"<br>Author: ".$row['Author']. //print row price, name, author
"<br>ISBN: ".$row['ISBN'].
"<br>Condition: ".$row['BookCondition'].
"<br>Price: ".$row['Price'].
"<br>Sellers Username: ".$row['Uname'];
echo"<br>";
echo"----------------------------------------------------------------------";
}
//Now select all from table
$query = "select * from Books";
$result = mysql_query($query);
// Count the total records
$total_records = mysql_num_rows($result);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<center><a href='getItems.php?page=1'>".'First Page'."</a> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='getItems.php?page=".$i."'>".$i."</a> ";
};
// Going to last page
echo "<a href='getItems.php?page=$total_pages'>".'Last Page'."</a></center> ";
?>
【问题讨论】:
-
停止使用
mysql已贬值 -
尝试回显您的查询并检查您获得的页面值。
标签: php mysql pagination