2013-08-05 177 views
0

頁面正確加載表中的結果(從初始頁面正確傳遞的變量/值);根據設定的「限制」具有正確的記錄數量;分頁正確地將記錄分成頁面。僅在第一頁上顯示記錄

兩件事情,我需要與在代碼幫助:

1)分頁應該顯示 '1 2 3 ... 6 7 8下一個';而是顯示'Previous 1 2 3 4 5 6 7 8 Next'。代碼似乎對我來說很好 - 試着在網上查找各種帖子,但沒有找到任何解決辦法。

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']; ?>&nbsp;<?php echo $row['VehModel']; ?>&nbsp;(<?php echo $row['VehYear']; ?>)</h1> 
    <NormalText><br> 
     <?php echo $row['VehMileage']; ?>kms&nbsp;-&nbsp;<?php echo $row['VehTransmission']; ?>&nbsp;-&nbsp;<?php echo $row['VehEngine']; ?>cc 
     <br> 
     <?php echo $row['VehCategory']; ?>&nbsp;-&nbsp;<?php echo $row['VehColour']; ?>&nbsp;-&nbsp;<?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

你這裏給出的代碼是相當長的。提示:嘗試並專注於中心問題。然後是我想到的另一個「脫離主題」的提示:當你想爲表單變量設置默認值時,我發現以下方法有用:首先將變量設置爲它們的默認值,然後'提取'$ _REQUEST'或'$ _POST'數組,如'$ VehName ='noname'; $ VehModel = $ Price = $ Location ='%'; $ YearFrom = 1960; $ YearTo = date('Y'); 摘錄($ _ POST);'。 – cars10m

+0

謝謝,非常有用的建議 - 將適用於我的代碼 – Dipen

回答

0

休耕代碼爲我工作

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> 
+0

我曾嘗試用你的代碼,我收到以下錯誤: 解析錯誤:語法錯誤,意想不到的T_STRING在上Display.php的線118 管線118: if($ result = $ mysqli-> query(「」SELECT COUNT(*)as num FROM vehicledetails WHERE VehName LIKE'「)$ VehName。 我注意到你的代碼中缺少兩個右括號}但錯誤仍然存​​在 任何建議如何解決該錯誤? – Dipen

相關問題