2017-10-11 3 views
1

페이지 매김과 함께 결과를 생성하는 PHP 코드가 있습니다.SQL 페이지 결과에서 행 수를 유지하는 방법은 PHP 페이지 매김에서 계속됩니까?

이 코드의 경우 각 페이지마다 5 행을 설정합니다. 하지만 문제는 입니다. 다음 페이지를 클릭하면 계속 증가하지 않습니다.

내가 달성하고자하는 것은 첫 페이지에 도달하면 숫자가 (1-5), 두 번째 페이지 (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

<?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> 

사용하면 첫 페이지에있는 경우 $page이 평가됩니다 첫 번째 루프를 들어 1이 될 것이다

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

같은 :

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

어느 페이지 2에 대한 1을 인쇄하면 출력은에서 시작됩니다.

+0

은 숫자 0로 시작합니다. 그것을 고치는 방법? – AlotJai

+0

쿼리 문자열에서 0 값을 얻지 않는 한 코드 당 $ 페이지의 값을 확인하십시오. 0이 될 수 없습니다. 또한 ($ page - 1) * 5 + $ i를 (empty ($ page)? 1 : $ page) - 1) * 5 + $ i로 변경할 수 있습니다. $ page의 값이 0이면 1을 사용합니다. – Urvish

+0

고마워요. 그것은 작동 :). – AlotJai

1

숫자 열에 이것을 사용하십시오. 아래 for 루프.

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

내부

echo $output;

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

감사합니다. 그것은 또한 작동합니다. – AlotJai