嘿我试图为书店分页结果,但我无法弄清楚为什么我的其他页面的链接不起作用。当我运行此页面时,原始页面会加载结果,但是当我转到下一页时,它会显示未找到的项目。
<?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> ";
?>
答案 0 :(得分:0)
这可能会对你有所帮助
<?php
$dbhost = 'localhost';
$dbuser = '---';
$dbpass = '---';
$rec_limit = 1;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('---');
/* Get total number of records */
$sql = "SELECT count(user_id) FROM table_name";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT company_name, e_mail, international_code, mob_number, nationality, current_loc, pref_location,
key_skills, address, user_dob, training, lang1, lang2, lang3, lang4 ".
"FROM se_job_contracts ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "<b>Company Name</b> :{$row['company_name']} <br> ".
"<b>Email-ID</b> : {$row['e_mail']} <br> ".
"<b>International code </b> : {$row['international_code']} "." <b>Mobile No </b> : {$row['mob_number']} <br> ".
"<b>Nationality </b> : {$row['nationality']} <br> ".
"<b>Current Location </b> : {$row['current_loc']} <br> ".
"<b>Preferred Location </b> : {$row['pref_location']} <br> ".
"<b>Key Skills </b> : {$row['key_skills']} <br> ".
"<b>Address </b> : {$row['address']} <br> ".
"<b>User DOB </b> : {$row['user_dob']} <br> ".
"<b>Training </b> : {$row['training']} <br> ".
"<b>Language known 1 </b> : {$row['lang1']} <br> ".
"<b>Language known 2 </b> : {$row['lang2']} <br> ".
"<b>Language known 3 </b> : {$row['lang3']} <br> ".
"<b>Language known 4 </b> : {$row['lang4']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Previous </a> |";
echo "<a href=\"$_PHP_SELF?page=$page\">Next >></a>";
}
else if( $page == 0 )
{
echo "<a href=\"$_PHP_SELF?page=$page\">Next >></a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Previous </a>";
}
mysql_close($conn);
?>