2017-01-01 63 views
0

下面的代碼是通過「選擇選項表單」從數據庫中獲取一些值,我最近添加了分頁剪輯來限制結果,當我運行代碼時它獲取5個記錄器的定義,但沒有顯示剩餘頁數。 什麼即時做錯在這裏?PHP簡單分頁

<?php 

$per_page = 5; 
if (isset($_GET["page"])) { 
    $page = $_GET["page"]; 
} else { 
    $page = 1; 
} 

$start_from = ($page - 1) * $per_page; 

if (!empty($_POST['form_val']) && isset($_POST['form_val'])) { 
    $_POST['form_val'] = 0; 

    $sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e 
    where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id"; 

    if (!empty($_POST['Location']) && isset($_POST['Location'])) { 
     $sql = $sql . " AND location =" . $_POST['Location']; 
    } 
    $strtdate = $_POST['Sday']; 
    $enddate = $_POST['Eday']; 
    if (!empty($_POST['Sday']) && isset($_POST['Sday']) && !empty($_POST['Eday']) && isset($_POST['Eday'])) { 
     $sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'"; 
    } elseif (!empty($_POST['Sday']) && isset($_POST['Sday'])) { 
     $sql = $sql . " AND date>='" . $strtdate . "'"; 
    } elseif (!empty($_POST['Eday']) && isset($_POST['Eday'])) 
     $sql = $sql . " AND date<='" . $enddate . "'"; 
    } 

    if (!empty($_POST['Site']) && isset($_POST['Site'])) { 
     $sql = $sql . " AND u.site=" . $_POST['Site']; 
    } 

    $sql = $sql . " LIMIT $start_from, $per_page"; 

    if (mysqli_query($conn, $sql)) { 
     $result = mysqli_query($conn, $sql); 
     if (mysqli_num_rows($result) >= 1) { 
      $rowcount = mysqli_num_rows($result); 
      echo '<legend> ' . $rowcount . ' Records Found !!!</legend>'; 
      echo '<br><br>'; 
      echo "<table class='srchtable'> 
        <tr> 
        <th>Picture</th> 
        <th>Date</th> 
        <th>User Name</th> 
        <th>country</th> 
        <th>Location</th> 
        <th>Site</th> 
        <th>Comment</th> 
        </tr>"; 
      while ($row = mysqli_fetch_array($result)) { 
       echo "<tr>"; 
       echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>"; 
       echo "<td>" . $row['date'] . "</td>"; 
       echo "<td>" . $row['user_name'] . "</td>"; 
       echo "<td>" . $row['country'] . "</td>"; 
       echo "<td>" . $row['location'] . "</td>"; 
       echo "<td>" . $row['site'] . "</td>"; 
       echo "<td>" . $row['comment'] . "</td>"; 
       echo "</tr>"; 
      } 
      echo "</table>"; 
      $total_pages = ceil($rowcount/$per_page); 
      echo "<center><a href='?page=1'>" . 'First Page' . "</a> "; 
      for ($i = 1; $i <= $total_pages; $i++) { 

       echo "<a href='?page=" . $i . "'>" . $i . "</a> "; 

      } 
      echo "<a href='?page=$total_pages'>" . 'Last Page' . "</a></center> "; 
     } else { 
      echo '<p>No Results Found !!!</p>'; 
     } 
    } 
} 
?> 
+0

你就可以開始通過固定代碼的縮進?這將有助於(主要是你)瞭解你的代碼流程。 – Dekel

+2

看看你如何計算'$ rowCount',你計算*總行數*,但包含了'LIMIT'和'OFFSET'值。相反,您應該有一個像這樣的簡單查詢,'SELECT * FROM tablename WHERE ...',沒有任何'LIMIT'或'OFFSET'子句,以便您可以計算總頁數並相應地顯示分頁鏈接。 –

+0

對不起,編輯我的問題 – Mavia

回答

1

正如我在我的評論說,對於顯示分頁鏈接:

  • 你計算行的總數,但在你的SELECT查詢納入LIMITOFFSET條款,這不會給正確的行數。您的SELECT查詢不應包含此部分,... LIMIT $start_from, $per_page
  • 由於您根據多個$_POST數據過濾結果,因此您應該將這些條件合併到分頁鏈接中,否則當您訪問其他頁面時(通過分頁鏈接),您將無法獲得理想的結果,這是因爲當你頁面跳轉時,$_POST數據不會被保留。 更好的是,您將<form>methodPOST更改爲GET,因爲通過這種方式,當您使用分頁鏈接從一個頁面跳轉到另一個頁面時,您可以更容易地捕捉和操作內容。

因此,基於以上幾點,你的代碼應該是這樣的:

$per_page = 5; 
if (isset($_GET["page"])) { 
    $page = $_GET["page"]; 
} else { 

    $page = 1; 

} 

$start_from = ($page - 1) * $per_page; 
if (!empty($_GET['form_val']) && isset($_GET['form_val'])) { 
    $_GET['form_val'] = 0; 

    $sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e 
    where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id"; 

    if (!empty($_GET['Location']) && isset($_GET['Location'])) { 

     $sql = $sql . " AND location =" . $_GET['Location']; 

    } 
    $strtdate = $_GET['Sday']; 
    $enddate = $_GET['Eday']; 
    if (!empty($_GET['Sday']) && isset($_GET['Sday']) && !empty($_GET['Eday']) && isset($_GET['Eday'])) { 
     $sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'"; 
    } elseif (!empty($_GET['Sday']) && isset($_GET['Sday'])) { 
     $sql = $sql . " AND date>='" . $strtdate . "'"; 
    } elseif (!empty($_GET['Eday']) && isset($_GET['Eday'])) { 
     $sql = $sql . " AND date<='" . $enddate . "'"; 
    } 
    if (!empty($_GET['Site']) && isset($_GET['Site'])) { 
     $sql = $sql . " AND u.site=" . $_GET['Site']; 
    } 

    $data_query = $sql . " LIMIT $start_from, $per_page"; 

    $result = mysqli_query($conn, $data_query); 
    if (mysqli_num_rows($result) >= 1) { 
     $rowcount = mysqli_num_rows($result); 
     echo '<legend> ' . $rowcount . ' Records Found !!!</legend>'; 
     echo '<br><br>'; 
     echo "<table class='srchtable'> 
     <tr> 
     <th>Picture</th> 
     <th>Date</th> 
     <th>User Name</th> 
     <th>country</th> 
     <th>Location</th> 
     <th>Site</th> 
     <th>Comment</th> 
     </tr>"; 
     while ($row = mysqli_fetch_array($result)) { 
      echo "<tr>"; 
      echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>"; 
      echo "<td>" . $row['date'] . "</td>"; 
      echo "<td>" . $row['user_name'] . "</td>"; 
      echo "<td>" . $row['country'] . "</td>"; 
      echo "<td>" . $row['location'] . "</td>"; 
      echo "<td>" . $row['site'] . "</td>"; 
      echo "<td>" . $row['comment'] . "</td>"; 
      echo "</tr>"; 
     } 
     echo "</table>"; 

     $query_result = mysqli_query($conn, $sql); 
     $total_rows = mysqli_num_rows($query_result); 
     $total_pages = ceil($total_rows/$per_page); 

     parse_str($_SERVER["QUERY_STRING"], $url_array); 
     unset($url_array['page']); 
     $url = http_build_query($url_array); 
     ?> 
      <center><a href="?page=1<?php echo isset($url) && !empty($url) ? "&" . $url : ""; ?>">First Page</a> 
     <?php 
     for ($i = 1; $i <= $total_pages; $i++) { 
      ?> 
      <a href="?page=<?php echo $i; echo isset($url) && !empty($url) ? "&" . $url : ""; ?>"><?php echo $i; ?></a> 
      <?php 
     } 
     ?> 
     <a href="?page=<?php echo $total_pages; echo isset($url) && !empty($url) ? "&" . $url : ""; ?>">Last Page</a></center> 
     <?php 
    } else { 
     echo '<p>No Results Found !!!</p>'; 
    } 
} 
+0

我不知道'parse_str',會閱讀更多關於它的信息。非常感謝你 – Mavia

+0

@Mavia你很受歡迎!很高興我能幫上忙。 :-) –