2017-07-02 68 views
1

我想讓article-loop divs在while循環中的echo函數內部填充。我正在嘗試創建一個動態分頁,在每個頁面上動態地打印文章 - 循環div。這很簡單,例如,當用戶更改頁面時,我希望接下來的幾個div顯示在該頁面上等等。PHP-div裏面的回聲函數

<?php 
class Pagination { 
public $current_page; 
public $per_page; 
public $total_count; 

public function __construct($page=1, $per_page=20, $total_count=0) { 
    $this->current_page = (int)$page; 
    $this->per_page = (int)$per_page; 
    $this->total_count = (int)$total_count; 
} 

public function offset() { 
    return ($this->current_page - 1) * $this->per_page; 
} 

public function total_pages() { 
    return ceil($this->total_count/$this->per_page); 
} 

public function previous_page() { 
    return $this->current_page - 1; 
} 

public function next_page() { 
    return $this->current_page + 1; 
} 

public function has_previous_page() { 
    return $this->previous_page() >= 1 ? true : false; 
} 

public function has_next_page() { 
    return $this->next_page() <= $this->total_pages() ? true : false; 
} 
} 

$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1; 
$per_page = 2; 
$total_count = 10; 
$pagination = new Pagination($page, $per_page, $total_count); 
?> 

<html> 
<body> 
<div class="article-loop"> 
<img src="http://i.imgur.com/CmU3tnl.jpg"> 
</div> 
<div class="article-loop"> 
<img src="http://i.imgur.com/TDdxS9H.png"> 
</div> 
<div class="article-loop"> 
<img src="http://i.imgur.com/39rpmwB.jpg"> 
</div> 
<div class="article-loop"> 
<img src="http://i.imgur.com/1lBZQ1B.png"> 
</div> 
<div class="article-loop"> 
<img src="https://i.imgur.com/Y5Ld4Qfh.jpg"> 
</div> 
<div class="article-loop"> 
<img src="http://i.imgur.com/8HumESY.jpg"> 
</div> 
<div class="article-loop"> 
<img src="http://i.imgur.com/CqCZBvk.png"> 
</div> 
<div class="article-loop"> 
<img src="http://i.imgur.com/wQVPRVp.png"> 
</div> 
<div> 
    <?php 
     $i = $pagination->offset() + 1; 
     $limit = $pagination->per_page; 
     while($i<=$pagination->total_count && $limit>0) { 
      echo '**IN HERE POPULATE THE ARTICLE-LOOP DIVS**'; 
      $i++; 
      $limit--; 
     } 
    ?> 
</div> 
<ul> 
    <?php 
     if($pagination->has_previous_page()) { 
      echo '<li style="display:inline"><a href="index.php?page='.$pagination->previous_page().'">&laquo;</a></li>'; 
     } else { 
      echo '<li style="display:inline" class="disabled"><a href="#">&laquo;</a></li>'; 
     } 
    ?> 
    <?php 
     for($i=1; $i<=$pagination->total_pages(); $i++) { 
      echo '<a href="index.php?page='.$i.'"><li style="display:inline; margin-left:5px; margin-right:5px">'.$i.'</li></a>'; 
     } 
    ?> 
    <?php 
     if($pagination->has_next_page()) { 
      echo '<li style="display:inline"><a href="index.php?page='.$pagination->next_page().'">&raquo;</a></li>'; 
     } else { 
      echo '<li style="display:inline" class="disabled"><a href="#">&raquo;</a></li>'; 
     } 
    ?> 
</ul> 
</body> 
</html> 

回答

0

而不是

<?php 
    $i = $pagination->offset() + 1; 
    $limit = $pagination->per_page; 
    while($i<=$pagination->total_count && $limit>0) { 
     echo '**IN HERE POPULATE THE ARTICLE-LOOP DIVS**'; 
     $i++; 
     $limit--; 
    } 
?> 

,你可以使用類似

<?php 
    $i = $pagination->offset() + 1; 
    $limit = $pagination->per_page; 
    while($i<=$pagination->total_count && $limit>0) { 
?> 
     **IN HERE POPULATE THE ARTICLE-LOOP DIVS** // html content 

<?php 
     $i++; 
     $limit--; 
    } 
?> 

您應該使用第二個。許多時候第一個不能正常工作。

+0

我把html內容放在你說的地方,它不起作用,它一次顯示所有的div/imgs。它應該顯示每頁三個div/img。 –

+0

請確保您在適當的地方關閉每個if else語句 –

+0

您能否請您用正確的代碼更新您的答案,因爲我似乎無法得到它的工作。 –