页面正确加载表格中的结果(从初始页面正确传递的变量/值);根据设置的'限制'具有正确的记录数;分页正确地将记录划分为页面。
我在代码中需要帮助的两件事:
1)分页应该显示'Previous 1 2 3 ... 6 7 8 Next';但显示'上一页1 2 3 4 5 6 7 8下一步'。代码对我来说似乎很好 - 尝试在线查找各种帖子但没有找到解决此问题的任何内容。
2)页面加载结果后,我点击下一页,它进入下一页但没有显示记录(空白表;但显示所有其他静态项目)。我查了一下,'page'变量被传递到下一页。它也没有出现任何错误。
我对PHP的了解有限,已阅读了几篇帖子但未能解决。
以下是代码 - 如果有人可以提供帮助,我会非常感激。 文件名:display.php 变量从以前的文件传递:index.php
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- INCLUDE THE CSS STYLESHEET FILE -->
<link href="Stylesheet/vehicles_stylesheet.css" rel="stylesheet" type="text/css">
<link href="Stylesheet/style.css" rel="stylesheet" type="text/css">
<?php
include("header.php");
?>
<script language="JavaScript" type="text/JavaScript">
====some javascript code here ===
</script>
</head>
<body bgcolor="#DADADA" onLoad="MM_preloadImages('images/ContactSellerOVR.gif','images/SearchAgainOVR.jpg')">
<?php
//Get Values from previous page
@$VehName = $_POST['VehName'];
@$VehModel = $_POST['VehModel'];
if ($VehModel=='')
{
$VehModel="%";
}
@$YearFrom = $_POST['YearFrom'];
if ($YearFrom=='')
{
$YearFrom=1960;
}
@$YearTo = $_POST['YearTo'];
if ($YearTo=='')
{
$YearTo=date('Y');
}
@$Price = $_POST['Price'];
if ($Price=='')
{
$Price="%";
}
@$Location = $_POST['Location'];
if ($Location=='')
{
$Location="%";
}
switch ($Price)
{
case "A":
$PriceSearch = "' AND VehPrice BETWEEN 0 AND 500000 ";
break;
case "B":
$PriceSearch = "' AND VehPrice BETWEEN 500000 AND 1000000 ";
break;
case "C":
$PriceSearch = "' AND VehPrice BETWEEN 1000000 AND 2000000 ";
break;
case "D":
$PriceSearch = "' AND VehPrice BETWEEN 2000000 AND 3000000 ";
break;
case "E":
$PriceSearch = "' AND VehPrice BETWEEN 3000000 AND 5000000 ";
break;
case "F":
$PriceSearch = "' AND VehPrice BETWEEN 5000001 AND 99000000 ";
break;
default:
$PriceSearch = "' AND VehPrice BETWEEN 0 AND 99000000 ";
}
?>
<table width="1000" cellspacing="0" cellpadding="4" align="center" border="0" bgcolor="#FFFFFF">
<tr>
<td align="center">
<a href="index.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Search Again','','images/SearchAgainOVR.jpg',1)"><img src="images/SearchAgain.jpg" name="Search Again" width="160" height="40" border="0" align="middle"></a>
</td>
</tr>
</table>
<?php
include('Connections/VehicleConn.php');
$tbl_name="vehicledetails"; // table name
$adjacents = 4; // How many adjacent pages should be shown on each side
// Get total number of rows in data table
$query = "SELECT COUNT(*) as num FROM vehicledetails WHERE VehName LIKE '" . $VehName .
"' AND VehModel LIKE '" . $VehModel . "'
AND VehYear BETWEEN '" . $YearFrom . "' AND '" . $YearTo .
$PriceSearch . " ORDER BY VehDateadded DESC" ;
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
/* Setup vars for query. */
$targetpage = "display.php"; //the name of this file
$limit = 3; // how many items to show per page
$page = isset($_GET['page']) ? $_GET['page'] : 0;
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
// Get data
$sql = "SELECT * FROM vehicledetails WHERE VehName LIKE '" . $VehName .
"' AND VehModel LIKE '" . $VehModel . "'
AND VehYear BETWEEN '" . $YearFrom . "' AND '" . $YearTo .
$PriceSearch . " ORDER BY VehDateadded DESC LIMIT " . $start . ", " . $limit ;
$result = mysql_query($sql) or die(mysql_error());
// Setup page vars for display
if ($page == 0) $page = 1; //if no page var is given, default to 1
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Apply rules and draw the pagination object
Save the code to a variable in case required to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">Previous</a>";
else
$pagination.= "<span class=\"disabled\">Previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">Next</a>";
else
$pagination.= "<span class=\"disabled\">Next</span>";
$pagination.= "</div>\n";
}
?>
<table width="1000" cellspacing="0" cellpadding="4" align="center" border="1" bordercolor="#F7F7F7" bgcolor="#FFFFFF">
<?php
// While loop to display records
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td bgcolor="#FFFFFF" width="320">
<blockquote>
<h1><?php echo $row['VehName']; ?> <?php echo $row['VehModel']; ?> (<?php echo $row['VehYear']; ?>)</h1>
<NormalText><br>
<?php echo $row['VehMileage']; ?>kms - <?php echo $row['VehTransmission']; ?> - <?php echo $row['VehEngine']; ?>cc
<br>
<?php echo $row['VehCategory']; ?> - <?php echo $row['VehColour']; ?> - <?php echo $row['VehFuel']; ?>
<br>
Vehicle Location: <?php echo $row['VehLocation']; ?>
<br>
<strong>Price: KShs. <?php echo $row['VehPrice']; ?> </strong><br>
<br>
</NormalText>
<form name="displaycontact" action="contactseller.php" target="_blank" method="post">
<input type="hidden" name="ID" value="<?php echo $row['VehID']; ?>">
<input type="hidden" name="NAME" value="<?php echo $row['VehName']; ?>">
<input type="hidden" name="MODEL" value="<?php echo $row['VehModel']; ?>">
<input type="hidden" name="YEAR" value="<?php echo $row['VehYear']; ?>">
<input type="hidden" name="MILEAGE" value="<?php echo $row['VehMileage']; ?>">
<input type="hidden" name="TRANSMISSION" value="<?php echo $row['VehTransmission']; ?>">
<input type="hidden" name="ENGINE" value="<?php echo $row['VehEngine']; ?>">
<input type="hidden" name="CATEGORY" value="<?php echo $row['VehCategory']; ?>">
<input type="hidden" name="COLOUR" value="<?php echo $row['VehColour']; ?>">
<input type="hidden" name="FUEL" value="<?php echo $row['VehFuel']; ?>">
<input type="hidden" name="LOCATION" value="<?php echo $row['VehLocation']; ?>">
<input type="hidden" name="PRICE" value="<?php echo $row['VehPrice']; ?>">
<input type="hidden" name="CONTACTNAME" value="<?php echo $row['VehContactname']; ?>">
<input type="hidden" name="CONTACTEMAIL" value="<?php echo $row['VehContactemail']; ?>">
<input type="hidden" name="CONTACTTEL" value="<?php echo $row['VehContacttel']; ?>">
<input type="submit" name="Submit" value="Contact Seller"> </form>
</blockquote>
</td>
<td bgcolor="#FFFFFF" width="680" align="center">
<a href="images/<?php echo $row['VehImage1']; ?>" rel="lightbox" title=""><img src="images/<?php echo $row['VehImage1']; ?>" align="middle" border="0" width="100" height="100"></a><a href="images/<?php echo $row['VehImage2']; ?>" rel="lightbox" title=""><img src="images/<?php echo $row['VehImage2']; ?>" align="middle" border="0" width="100" height="100"></a><a href="images/<?php echo $row['VehImage3']; ?>" rel="lightbox" title=""><img src="images/<?php echo $row['VehImage3']; ?>" align="middle" border="0" width="100" height="100"></a><a href="images/<?php echo $row['VehImage4']; ?>" rel="lightbox" title=""><img src="images/<?php echo $row['VehImage4']; ?>" align="middle" border="0" width="100" height="100"></a>
</td>
</tr>
<?php
}
?>
</table>
<table width="1000" cellspacing="0" cellpadding="4" align="center" bgcolor="#FFFFFF">
<tr align="center">
<td align="center">
<?php
echo $pagination
?>
</td>
</tr>
</table>
<?php
include("footer.php");
?>
</body>
</html>
答案 0 :(得分:0)
Fallow代码为我工作
include('connect-db.php');
// number of results to show per page
$per_page = 150;
// figure out the total pages in the database
if ($result = $mysqli->query(""SELECT COUNT(*) as num FROM vehicledetails WHERE VehName LIKE '" . $VehName .
"' AND VehModel LIKE '" . $VehModel . "'
AND VehYear BETWEEN '" . $YearFrom . "' AND '" . $YearTo .
$PriceSearch . " ORDER BY VehDateadded DESC" ; "))
{
if ($result->num_rows != 0)
{
$total_results = $result->num_rows;
// ceil() returns the next highest integer value by rounding up value
if necessary
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated
.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p><a href='view.php'>View All</a> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo $i . " ";
}
else
{
echo "<a href='view-paginated.php?page=$i'>$i</a> ";
}
}
echo "</p>";
<your code to display data>