<?php
include('pagination.php');
if(isset($_GET['page']) and is_numeric($_GET['page'])){
$page = $_GET['page'];
}else{
$page = 1;
}
$query = mysql_query("SELECT * FROM messages");
$total_users = mysql_num_rows($query);
$per_page = 10;
$offset = (($per_page * $page) - $per_page);
$total_pages = ceil($total_users / $per_page);
$pagination = pagination("ieraksti.php?page=", $page, $total_pages);
$result = mysql_query("SELECT * FROM messages ORDER BY id DESC LIMIT ".$offset.", ".$per_page."");
$myrow = mysql_fetch_array($result);
do{
echo "<a href='page.php?id=".$myrow['id']."'>".$myrow['name']."</a><br>";
}
while($myrow = mysql_fetch_array($result));
echo "<p>".$pagination."</p>";
?>
šis ir kopēts ārā no manas lapas, skaties uzbūvi.
fails kurš tiek inclūdots:
<?php
function pagination($link, $page, $total_pages) {
$return = "";
// show first and previous links
// disable if we're on the first page or if there's only 1 page
if ($page != 1 && $total_pages > 1) {
$return .= "<a href=\"".$link."1\">« Pirmā lapa </a> ";
$return .= "<a href=\"".$link.($page - 1)."\">< Iepriekšējā lapa </a> ";
} else {
$return .= "<< Pirmā lapa ";
$return .= "< Iepriekšējā lapa ";
}
// don't bother looping if there's only 1 page
if ($total_pages == 1) {
$return .= "1";
} else {
// placeholders so we know if we've already shown dots already or not
$dots_before = 0;
$dots_after = 0;
// loop through all the pages starting at 1
for ($i = 1; $i <= $total_pages; $i++) {
// are we at the current page?
if ($i == $page) {
if ($i == 1) { // if this is page 1, don't put in a comma before the number
$return .= $i;
} else { // not the first page, put in a comma
$return .= ", ".$i;
}
} else { // not at the current page, lets display links
if (($i <= 2) || ($i < $page && $i >= ($page - 2)) || ($i > $page && $i <= ($page + 2)) || ($i > ($total_pages - 2))) {
if ($i == 1) { // if this is page 1, don't put in a comma before the number
$return .= "<a href=\"".$link.$i."\">".$i."</a>";
} else { // not the first page, put in a comma
$return .= ", <a href=\"".$link.$i."\">".$i."</a>";
}
} else {
if ($i < $page) { // dots before the current page
if ($dots_before == 0) {
$dots_before = 1;
$return .= ", ...";
}
} else if ($i > $page) { // dots after the current page
if ($dots_after == 0) {
$dots_after = 1;
$return .= ", ...";
}
}
}
}
}
}
// show next and last links
// disable if we're on the last page or if there's only 1 page
if ($page != $total_pages && $total_pages > 1) {
$return .= " <a href=\"".$link.($page + 1)."\"> Nākamā lapa ></a>";
$return .= " <a href=\"".$link.$total_pages."\"> Pēdējā lapa »</a>";
} else {
$return .= " Nākamā lapa >";
$return .= " Pēdējā lapa >>";
}
return $return;
}
?>