页面正确加载,结果显示在表格中(变量/值从初始页面正确传递);根据设置的“限制”具有正确的记录数;分页正确地将记录分成页面。
我在代码中需要帮助的两件事:
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>