페이지 매김을 만들고 있습니다. 나는 또한 이런 식으로 사용하고페이지 매김 페이지 수
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=10, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
public function offset(){
// Assuming 20 items per page:
// page 1 has an offset of 0 (1-1) * 20
// page 2 has an offset of 20 (2-1) * 20
// in other words, page 2 starts with item 21
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;
}
}
:
<?php
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page
$per_page = 8;
// 3. total record count ($total_count)
$total_count = Song::count_all();
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
// for this page
$sql = "SELECT * FROM songs ";
$sql .= "ORDER BY dopeness DESC ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination->offset()}";
$songs = Song::find_by_sql($sql);
?>
<?php
if($pagination->total_pages() > 1){
if($pagination->has_previous_page()){
echo "<a href=\"all_songs.php?page=";
echo $pagination->previous_page();
echo "\">«Previous </a> ";
}
for($i=1; $i<=$pagination->total_pages(); $i++){
if($i == $page){
echo " <span class=\"selected\">{$i}</span> ";
} else {
echo " <a href=\"all_songs.php?page={$i}\">{$i}</a> ";
}
}
if($pagination->has_next_page()){
echo "<a href=\"all_songs.php?page=";
echo $pagination->next_page();
echo "\"> Next»</a> ";
}
}
?>
출력은 "이전 1 2 3 (그러나 많은 페이지) 다음"입니다 나는 매김 클래스가 있습니다. 내가 원하는 것은 10 페이지가 넘는 경우 11-20 페이지 ("이전 11 12 13 다음")로 이동하게하는 마지막 링크의 "..." 에. 아무도 내가이 문제를 해결할 수 있도록 도와 줄 수 있습니까?
당신의 코드에는이 행에 버그가 있습니다 :'$ page =! empty ($ _ GET [ 'page'])? (int) $ _ [ 'page'] : 1;' "haha"나 그 밖의 숫자를 보내지 않으면 $ page의 결과 값은 0이됩니다. – DampeS8N
고마워요. 나는 그것을 고칠 것이다 – user3836703