HuyPV
Wednesday, August 12, 2009
<?php
$arr = array();
for ($i = 1; $i <= 99; $i++) {
$name = md5($i);
$person = array(
"id" => $i,
"name" => $name,
"location" => "Viet Nam"
);
$key = $name;
$arr[$key] = $person;
}
krsort($arr);
// Số bản ghi
$total = count($arr);
$PAGE_SIZE = 100;
// Số trang
$TAIL_PAGE = ceil($total / $PAGE_SIZE);
$page = (!isset($_GET["page"])) ? 1 : $_GET["page"];
if ($page < 1 || $page > $TAIL_PAGE) $page = 1;
$HEAD = ($page - 1) * $PAGE_SIZE + 1;
$TAIL = $HEAD + $PAGE_SIZE - 1;
if ($TAIL > $total) $TAIL = $total;
$arr = array_slice($arr, $HEAD - 1, $PAGE_SIZE);
echo "Total: $total records ~ $TAIL_PAGE pages.<br />";
echo "Viewing: page $page ~ record $HEAD to $TAIL<br />";
echo '<table border="1">';
echo '<tr><td>ID</td><td>Name</td><td>Location</td></tr>';
foreach ($arr as $key => $person) {
$id = $person["id"];
$name = $person["name"];
$location = $person["location"];
echo '<tr><td>' . $id . '</td><td>' . $name . '</td><td>' . $location . '</td></tr>';
}
echo '</table>';
$query = $_SERVER["QUERY_STRING"];
$query = preg_replace("/[&]?page=[0-9]*&?/", "", $query);
if (empty($query)) {
} else {
$query .= "&";
}
if ($page > 1)
echo '<a href="?' . $query . 'page=' . ($page - 1) . '">PREVIOUS</a> ';
if ($page < $TAIL_PAGE) {
echo '<a href="?' . $query . 'page=' . ($page + 1) . '">NEXT</a>';
}
?>
Title:
Give me an array, I will show it with paging
Description:
<?php $arr = array(); for ($i = 1; $i <= 99; $i++) { $name = md5($i); $person = array( "id" => $i, ...
...
Rating:
4