2017-10-11 4 views
1

私はこれらのPHPコードを使用して結果を生成します。PHPのページネーションでSQLの結果の行数を維持する方法は?

このコードでは、ページごとに5行ずつ設定します。しかし、問題は数字は次のページをクリックすると連続的に増加していません。

私が達成したいのは、1ページ目を押すと、数字は(1-5)、2ページ目(5-10)になります。

ここに私のコードです:

のindex.php

<?php include "config.php"; ?> 
    <div> 
     <table> 
     <thead> 
      <tr> 
      <th>Number</th> 
      <th>name</th> 
      <th>address</th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php for($i = 0; $i < count($results->data); $i++) : ?> 
      <tr> 
       <td><?php echo $page++; ?></td> 
       <td><?php echo $results->data[$i]['name']; ?></td> 
       <td><?php echo $results->data[$i]['address']; ?></td> 
      </tr> 
      <?php endfor; ?> 
     </tbody> 
     </table> 
    </div>              
    <?php echo $Paginator->createLinks($links); ?> 

config.phpの

<?php 
$user = "root"; 
$pass = ""; 
try { 
    $connect = new PDO('mysql:host=localhost;dbname=database', $user, $pass); 
} catch (PDOException $e) { 
    print "Error!: " . $e->getMessage() . "<br/>"; 
    die(); 
} 
$limit = (isset($_GET['limit'])) ? $_GET['limit'] : 5; 
$page = (isset($_GET['page'])) ? $_GET['page'] : 1; 
$links = (isset($_GET['links'])) ? $_GET['links'] : 7; 
$query = "SELECT * FROM table ORDER BY id DESC"; 

require_once 'Paginator.php'; 
$Paginator = new Paginator($connect, $query); 
$results = $Paginator->getData($limit, $page); 
?> 

Paginator.php

は、 210
<?php 
class Paginator { 
    private $konek; 
    private $batas; 
    private $halaman; 
    private $habeit; 
    private $semua; 

    public function __construct($conn, $query) { 

     $this->konek = $conn; 
     $this->habeit = $query; 

     $rs= $this->konek->prepare($this->habeit); 
     $rs->execute(); 
     $this->semua = $rs->rowCount(); 

    } 

    public function getData($limit = 10, $page = 1) { 

     $this->batas = $limit; 
     $this->halaman = $page; 

     if ($this->batas == 'all') { 
      $query = $this->habeit; 
     } else { 
      $query = $this->habeit . " LIMIT " . (($this->halaman - 1) * $this->batas) . ", $this->batas"; 
     } 
     $rs = $this->konek->prepare($query); 
     $rs->execute(); 
     while ($row = $rs->fetch(PDO::FETCH_ASSOC)) { 
      $results[] = $row; 
     } 

     $result   = new stdClass(); 
     $result->page = $this->halaman; 
     $result->limit = $this->batas; 
     $result->total = $this->semua; 
     $result->data = $results; 

     return $result; 
    } 

    public function createLinks($links) { 
     if ($this->batas == 'all') { 
      return ''; 
     } 
     $last  = ceil($this->semua/$this->batas); 

     $start  = (($this->halaman - $links) > 0) ? $this->halaman - $links : 1; 
     $end  = (($this->halaman + $links) < $last) ? $this->halaman + $links : $last; 

     $html  = '<ul class="pagination">'; 

     $class  = ($this->halaman == 1) ? "disabled" : ""; 
     $html  .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=1">&laquo;</a></li>'; 
     $html  .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=' . ($this->halaman - 1) . '">&lsaquo;</a></li>'; 

     if ($start > 1) { 
      $html .= '<li><a href="?limit=' . $this->batas . '&page=1">1</a></li>'; 
      $html .= '<li><span class="titik">...</span></li>'; 
     } 

     for ($i = $start ; $i <= $end; $i++) { 
      $class = ($this->halaman == $i) ? "active" : ""; 
      $html .= '<li class="' . $class . '"><a href="?limit=' . $this->batas . '&page=' . $i . '">' . $i . '</a></li>'; 
     } 

     if ($end < $last) { 
      $html .= '<li class="disabled"><span>...</span></li>'; 
      $html .= '<li><a href="?limit=' . $this->batas . '&page=' . $last . '">' . $last . '</a></li>'; 
     } 

     $class  = ($this->halaman == $last) ? "disabled" : ""; 
     $html  .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=' . ($this->halaman + 1) . '">&rsaquo;</a></li>'; 
     $html  .= '<li class="' . $class . ' symbol"><a href="?limit=' . $this->batas . '&page=' . ($last) . '">&raquo;</a></li>'; 

     $html  .= '</ul>'; 

     return $html; 
    } 

} 

誰かが私にこの問題の解決を手伝ってもらえることを感謝します。ありがとうございました。

答えて

2

代わりの

<td><?php echo $page++; ?></td> 

使用

<td><?php echo ($page - 1) * 5 + $i; ?></td> 

あなたが最初のページにある場合は、$pageは、それは次のように評価します最初のループ1.次のようになります。

<td><?php echo (1 - 1) * 5 + 1 ?></td> 

これは1を印刷し、ページ2の場合は出力はから開始します

+0

の内側には番号0で始まります。どのようにそれを修正するには? – AlotJai

+0

$ pageの値をチェックします。コードごとに0にすることはできません。ただし、クエリ文字列から0の値を取得している場合を除きます。また、($ page - 1)* 5 + $ iを(空($ page)?1:$ page) - 1)* 5 + $ iに変更することもできます。 $ pageの値が0の場合は1を使用します。 – Urvish

+0

ありがとうございます。それは働く:)。 – AlotJai

1

数字の列にこれを使用します。下のループのために。

$output = ($page - 1) * 5 + $i + 1; 

だけecho $output;

<td><?php echo $output; ?></td> 
+1

ありがとうございます。それはまた働く。 – AlotJai

関連する問題