2017-08-04 145 views
0

我寫了這個代碼表中的行搜索:搜索PHP/MySQL與分頁

<div class="container mx-auto"> 
    <!--Add class table-responsive for responsive table --> 
    <div class="row"> 
     <div class="col-md-6"> 
      <form method="post"> 
      <div class="input-group"> 
       <input type="text" class="form-control" placeholder="Search for..." name="searchValue"> 
       <span class="input-group-btn"> 
        <button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button> 
       </span> 
      </div> 
      </form> 
     </div> 
    </div> 

    <table class="table mx-auto" id="'table"> 
     <thead> 
     <tr> 
      <th>Name</th> 
      <th>Surname</th> 
      <th>Email</th> 
      <th>Phone</th> 
      <th>Company</th> 
      <th>More</th> 

     </tr> 
     </thead> 
     <tbody> 
     <?php 
     $page_max = 8; 
     if (isset($_POST['search'])){ 
      $search = $_POST['searchValue']; 

      $entriesInDatabase = $database->getData("SELECT count(id) FROM customers WHERE name LIKE '%$search%'"); 
      $numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max); 
      $numberOfRecords = $page_max; 
      if(isset($_GET['page'])) { 
       $page = $_GET['page']; 
       $start = $page * $page_max; 
      } 
      else{ 
       $page = 0; 
       $start = $page * $page_max; 

      } 
      $customers = $database->getUsers("SELECT * FROM customers WHERE name LIKE '%$search%' LIMIT $start, $numberOfRecords"); 
     } 
     else{ 
      $entriesInDatabase = $database->getData("SELECT count(id) FROM customers"); 
      $numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max); 
      $numberOfRecords = $page_max; 
      if(isset($_GET['page'])) { 
       $page = $_GET['page']; 
       $start = $page * $page_max; 
      } 
      else{ 
       $page = 0; 
       $start = $page * $page_max; 

      } 
      $customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords"); 
     } 





     foreach($customers as $customer){ 
      $name = $customer ['name']; 
      $surname = $customer['surname']; 
      $email = $customer['email']; 
      $phone = $customer['phone']; 
      $company = $customer['company']; 
      $id = $customer['id']; 
      echo "<tr> 
        <td>$name</td> 
        <td>$surname</td> 
        <td>$email</td> 
        <td>$phone</td> 
        <td>$company</td> 
        <td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td> 



        </tr>"; 
     } 
     ?> 
</tbody> 
</table> 
    <ul class="pagination"> 
<?php 

if(isset($_GET['page']) && $_GET['page'] > 0){ 
    $previous = $_GET['page'] - 1; 
    echo '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>'; 
} 


for($i = 0; $i < $numberOfPages; $i++){ 
    echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>'; 
} 
    if(isset($_GET['page']) && $_GET['page'] < $numberOfPages - 1){ 
     $page = $_GET['page']; 
     $next = $page + 1; 
     echo '<li class="page-item"><a class="page-link" href="?page='.$next.'">Next</a></li> '; 
    } 
    elseif(!isset($_GET['page'])){ 
     echo '<li class="page-item"><a class="page-link" href="?page=1">Next</a></li> '; 
    } 
?> 



    </ul> 
</div> 

搜索工作正常,當我第0頁上,但是當我在第2頁我只能搜索該頁面上的記錄(我認爲這是因爲我的查詢限制),除此之外,當我搜索'A'時,它返回7頁,但如果我點擊seconde頁面,它不會加載搜索結果,但主要列表與客戶在第2頁。

任何人都可以幫助我解決這些問題?

回答

0

首先:清理您的輸入。在數據庫查詢中使用POST或GET可能非常非常危險。

第二:DRY,你可能可以用你使用的一半的代碼來做到這一點。

你「失去」你的搜索的原因是因爲你的分頁不會延續搜索詞。

將其添加到從POST分頁和交換鏈接獲得:

<?php 
$searchString = ''; 
if($_GET['searchValue']){ 
    $searchString = '&searchValue=' . $_GET['searchValue']; 
} 

if(isset($_GET['page']) && $_GET['page'] > 0){ 
    $previous = $_GET['page'] - 1; 
echo '<li class="page-item"><a class="page-link" href="?page='. $previous.$searchString.'">Previous</a></li>'; 
} 
// more code 
'... href="?page='.$i.$searchString'" ...' 

'... href="?page='.$next.$searchString'" ...' 

請注意,我用$_GET['searchValue']而不是POST。在大多數情況下,使用POST進行搜索沒有任何優勢(完全相反,如果您使用GET,則可以在Google Analytics(分析)f.ex中進行跟蹤)。因此,請確保您更改表格方法以獲取並if (isset($_POST['search'])){$_GET['searchValue']。如果你堅持使用POST,你可以使用$_REQUEST,它保存來自GET和POST的值。