2016-08-30 120 views
1

當我在搜索框中輸入一個詞語時,雖然它給了我一個完美的結果,但我的分頁不起作用。 例如。如果我輸入"Laptops"它會給我帶有筆記本電腦的記錄,但我的分頁不符合流程。我不知道我錯過了什麼。 這裏是我的代碼:分頁無法正常工作 - PHP

搜索框:

<form action="" method="GET"> 

      Search: <input type="text" name="term" value="<?php echo @$_REQUEST['term']; ?>" /><br /> 
      <input type="submit" value="Submit" /> 
     </form> 

顯示數據:

<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover"> 
      <?php 
      include_once '../storescripts/connect_to_mysql.php'; 
      $num_rec_per_page = 5; 
      if (isset($_GET["page"])) { 
       $page = $_GET["page"]; 


      } else { 
       $page = 1; 
      }; 
      $start_from = ($page - 1) * $num_rec_per_page; 

      $sql = "SELECT * FROM products WHERE status='Active' LIMIT $start_from, $num_rec_per_page"; 
      ?> 
      <thead> 
       <tr class="success"> 
        <th>Id</th> 
        <th>Product Image</th> 
        <th>Product Name</th> 
        <th>Price</th> 
        <th>Status</th> 
        <th>Quantity</th> 
        <th>Details</th> 
        <!--<th>Category</th> 
        <th>Subcategory</th>--> 
        <th>Date Added</th> 
        <th colspan="2">Functions</th> 
       </tr> 
      </thead> 


<?php 
if (!empty($_REQUEST['term'])) 
{ 

    $term = mysql_real_escape_string($_REQUEST['term']); 

    $sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or status LIKE '%" . $term . "' or quantity LIKE '%" . $term . "' or details LIKE '%" . $term . "' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'"; 
} 
$r_query = mysql_query($sql); 
if ($r_query > 1) 
{ 

    while ($row = mysql_fetch_array($r_query)) 
    { 
     echo "<tr bgcolor=''>"; 
     echo "<td width='5%'>" . $row['id'] . "</td>";?> 
     <td width="10%"><img style='border:#666 1px solid;' width='70' src="<?php echo $row["productimage"]; ?>" alt="" /></td> 
     <?php echo "<td width='20%'>" . $row['product_name'] . "</td>"; 
     echo "<td width='5%'>" . $row['price'] . "</td>"; 
     echo "<td width='5%'>" . $row['status'] . "</td>"; 
     echo "<td width='5%'>" . $row['quantity'] . "</td>"; 
     echo "<td width='19%'>" . $row['details'] . "</td>"; 
     // echo "<td>" . $row['category'] . "</td>"; 
     // echo "<td>" . $row['subcategory'] . "</td>"; 
     echo "<td width='10%'>" . $row['date_added'] . "</td>"; 
     echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";?> 
     <td><a href="#" class="delete" onclick="dialogbox(<?php echo $row['id']; ?>)" >Delete</a> 
     <?php 
     //echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "' onclick='return show_confirm();' >Delete</a></td><tr>"; 



     echo "</tr>"; 
    } 
} 
else { 
    echo "Nothing should be displayed"; 
} 
?> 
     </table> 

分頁代碼:

<?php 
      //Pagination code starts here 
      $sql = "SELECT * FROM products"; 
      $rs_result = mysql_query($sql); //run the query 
      $total_records = mysql_num_rows($rs_result); //count number of records 
      $total_pages = ceil($total_records/$num_rec_per_page); 

      echo "<a href='product_listing.php?page=1'>".'|<'."</a> "; // Goto 1st page 

      for ($i=1; $i<=$total_pages; $i++) { 
         echo "<a href='product_listing.php?page=".$i."'>".$i."</a> "; 
      }; 
      echo "<a href='product_listing.php?page=$total_pages'>".'>|'."</a> "; // Goto last page 

      ?> 
+0

** 1。不要使用已棄用的mysql _ * - 函數。它們自PHP 5.5起棄用,並在PHP 7中完全刪除。請改用MySQLi或PDO。 ** 2。**如果在一個查詢中有'WHERE''子句('product_name LIKE ...'),那麼在分頁查詢中也需要它。 –

回答

1

你必須使用r_query找不在分頁行。現在您正在查找表中的總行數

<?php 
      //Pagination code starts here 

      $total_records = mysql_num_rows($r_query); //count number of records 
      $total_pages = ceil($total_records/$num_rec_per_page); 

      echo "<a href='product_listing.php?page=1'>".'|<'."</a> "; // Goto 1st page 

      for ($i=1; $i<=$total_pages; $i++) { 
         echo "<a href='product_listing.php?page=".$i."'>".$i."</a> "; 
      }; 
      echo "<a href='product_listing.php?page=$total_pages'>".'>|'."</a> "; // Goto last page 

      ?> 
+0

像我必須把條件嗎?如果(something_Searched){返回搜索分頁} else {選擇所有記錄並顯示分頁}正確? –

+0

好吧,那很好。如果沒有結果,你可以顯示「沒有結果」,所有的項目與分頁 – jophab