Skip to content

Instantly share code, notes, and snippets.

@Surgo
Created May 22, 2009 12:42
Show Gist options
  • Save Surgo/116097 to your computer and use it in GitHub Desktop.
Save Surgo/116097 to your computer and use it in GitHub Desktop.
<?php
class Page {
var $object_list = array(); // ページング処理したいオブジェクト配列
var $num_page = 0; // 総ページ数
var $number = 0; // 現在のページ
var $per_page; // ページ毎のアイテム数
var $count = 0; // 総アイテム数
function Page($object_list, $per_page = 10) {
$this->object_list = $object_list;
$this->per_page = (int)($per_page);
$this->count = sizeof($object_list);
$this->num_page = ceil($this->count / $per_page);
}
function has_next() {
if ($this->number < $this->num_page) {
return 1;
}
return 0;
}
function has_previous() {
if ($this -> number > 1) {
return true;
}
return false;
}
function has_other_pages() {
if (has_next() and hasprevious()) {
return true;
}
return false;
}
function next_page_number() {
return $this->number + 1;
}
function previous_page_number() {
return $this->number + 1;
}
function start_index() {
return 0;
}
function end_index() {
return $this->num_page - 1;
}
function get_page() {
$item_from = $this->per_page * $this->number;
$item_to = $this->per_page;
if (($item_from + $item_to) > $this->count) {
$item_to = $this->count;
}
return array_slice($this->object_list, $item_from, $item_to);
}
function get_next() {
$this->number ++;
return $this -> get_page();
}
function get_previous() {
$this->number --;
return $this -> get_page();
}
}
/*
class Book {
var $isbn = "";
var $title = "";
var $count = 0;
function Book($isbn, $title, $count) {
$this->isbn = $isbn;
$this->title = $title;
$this->count = (int)($count);
}
}
class Publisher {
var $name = "";
var $books = array();
function Publisher($name, $books) {
$this->name = $name;
$this->books = $books;
}
}
$publishers = array(
new Publisher(
"TESTPUB1",
array(
new Book(
"00000000001",
"Book1",
3
),
new Book(
"00000000002",
"Book2",
4
),
new Book(
"00000000003",
"Book3",
5
),
)
),
new Publisher(
"TESTPUB2",
array(
new Book(
"00000000004",
"Book4",
4
),
new Book(
"00000000005",
"Book5",
5
),
new Book(
"00000000006",
"Book6",
6
),
new Book(
"00000000007",
"Book7",
7
),
new Book(
"00000000008",
"Book8",
8
),
new Book(
"00000000009",
"Book9",
9
),
new Book(
"00000000010",
"Book10",
10
),
new Book(
"00000000011",
"Book11",
11
),
new Book(
"00000000012",
"Book12",
12
),
new Book(
"00000000013",
"Book13",
13
),
new Book(
"00000000014",
"Book14",
14
),
new Book(
"00000000015",
"Book15",
15
),
new Book(
"00000000016",
"Book16",
16
),
new Book(
"00000000017",
"Book17",
17
),
new Book(
"00000000018",
"Book18",
18
),
new Book(
"00000000019",
"Book19",
19
),
new Book(
"00000000020",
"Book20",
20
),
new Book(
"00000000021",
"Book21",
21
),
new Book(
"00000000022",
"Book22",
22
),
new Book(
"00000000023",
"Book23",
23
),
new Book(
"00000000024",
"Book24",
24
),
new Book(
"00000000025",
"Book25",
25
),
new Book(
"00000000026",
"Book26",
26
),
new Book(
"00000000027",
"Book27",
27
),
)
)
);
foreach ($publishers as $publisher) {
$page = new Page($publisher->books, 10);
$page2 = new Page($publisher->books, 10);
print $publisher->name."while<br />";
// while 形式
while (true) {
if ($page->has_next() == 0) {
break;
}
$books = $page->get_page();
print "* ". $publisher->name." (".sizeof($page->get_page()).")<br />";
foreach ($books as $book) {
print " * ".$book->title."<br />";
}
$page->get_next();
}
print "<hr />";
print $publisher->name."for<br />";
// for 形式
for($i = $page2->start_index(); $i <= $page2->end_index(); $i++) {
$page2->number = $i;
$books2 = $page2->get_page();
print "* ". $publisher->name." (".sizeof($page2->get_page()).")<br />";
foreach ($books2 as $book) {
print " * ".$book->title."<br />";
}
}
print "<hr />";
}
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment