2013-10-25 32 views
-5
 <?php 
     // Including data base file for connection 
     require_once 'config.php'; 
     require 'db.php'; 
     $data = array(); 
     //creating array for alphabet 
     $navigation = array(
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' 
    ); 

    $rec_limit = 3; 
    $limit = 3; 
    if (isset($_GET['page'])) { 
     $page = $_GET['page'] + 1; 
     $limit = $page * $limit; 
     $offset = $limit - 1; 
    } 
    else { 
     $page = 1; 
     $offset = 0; 
    } 
    print "SELECT * FROM user $query LIMIT $offset, $limit"; 

    $query =isset($_GET['alphabet'])? " WHERE lname like '" . $_GET['alphabet'] . "%'":''; 
    $count_query = mysql_query("SELECT count(*) as count_data FROM user $query"); 
    $result = mysql_query("SELECT * FROM user $query LIMIT $offset, $limit"); 
    $count_data = mysql_fetch_array($count_query); 

    while ($estrogen_limit_user = mysql_fetch_array($result)) { 
     $data[] = $estrogen_limit_user; 
    } 

    $rec_count = $count_data['count_data']; 
    $total_page = ceil($rec_count/$rec_limit); 

    $left_rec = $rec_count - ($page * $rec_limit); 
    ?> 
       <?php 
      //alphabet showing on the page 
      foreach ($navigation as $value) { 
      ?> 
      <a href="directory.php?alphabet=<?php print $value; ?>"><?php print $value; ?></a> 
      <?php } ?> 

     <a href="admin-section.php?alphabet=<?php print $_GET['alphabet'];?>"> Click here for edit the records</a> 

      <?php 
      //if data will found from the db 
      if (count($data) > 0) { 
      foreach ($data as $row) { 
      ?> 

      <p class="name"><a href="directoryedit.php?ID=<?php print $row['id']?>"><?php print $row['fname'] . ' ' . $row['lname'];//print fname and lastname ?></a></p> 
      <p><?php print $row['address'];//print address ?></p> 
      <p><?php print $row['city']; //print city?></p> 
      <p><?php print $row['phone']; //print phone?></p> 
      <p><?php print $row['email']; //print email?></p> 

      <?php 
      } 

      } //if data will not found 
      else { 
      print "NOT FOUND"; 
      } 

      if($page > 1) { 
      $last = $page - 2; 
      echo "<a href=\"$_PHP_SELF?page=$last\">Last 3 Records</a>"; 
      if($total_page < $page) 
      echo "| <a href=\"$_PHP_SELF?page=$page\">Next 3 Records</a>"; 
      } 
      else if($page == 1) { 
      echo "<a href=\"$_PHP_SELF?page=1\">Next 3 Records</a>"; 
      } 
      else if($left_rec < $rec_limit) { 
      $last = $page - 2; 
      echo "<a href=\"$_PHP_SELF?page=$last\">Last 3 Records</a>"; 
      } 
      ?> 

我已經嘗試了很多,但我無法解決它不能正常工作。 我張貼我的整個頁面的內容,因爲我剛剛開始了我的職業生涯,我在這裏是新的,所以不知道它。 它是我現在工作的項目的目錄頁面。我們如何在php代碼中創建分頁?

+2

什麼是錯誤?它是做什麼的。另一個注意事項是你的代碼容易受到SQL注入的影響 – Cfreak

回答

1

只是一些三分球在正確的方向:

  1. 更改頁面並不意味着你要改變在頁面右側的項目數?所以你的$限制應該始終保持3.你的$偏移= $頁* 3; (如果$頁從0開始)

  2. 無關尋呼機:直接使用$ _GET變量您查詢

  3. 使用一些簡單的變量,可以使您的生活更輕鬆照顧SQL注入:

像:

$page; // always start at 0 (display can of course be + 1, same for get/post) 
$limit; // use for items on the page 
$count; // total count from the sql count query 

$firstPage = 0; 
$prevPage = $page - 1; 
$nextPage = $page + 1; 
$lastPage = ceil($count/$limit); 

現在的輸出,你可以創建4個簡單的if語句:

if ($page != $firstPage) { 
    // link to first page 
} 

if ($prevPage >= 0 && $page != $prevPage) { 
    // link to prev page 
} 

if ($nextPage <= $lastPage && $page != $nextPage) { 
    // link to next page 
} 

if ($page != $lastPage) { 
    // link to last page 
} 

希望這有助於!