<?php
function get_paginator_link($start_msg, $text, $link) {
if($start_msg > 2)
$page = ceil($start_msg/10);
else
$page = 1;
$count = $start_msg - 1;
$result = "<A HREF=\"".$_SERVER['PHP_SELF']."?"
. "start_msg=$start_msg&$link&count=$count&page=$page\" "
. ">$text</A>";
return $result;
}
/*
* This function computes the paginator string.
*/
function get_paginator_str($start_msg, $num_msgs, $show_num, $link) {
if(!$start_msg){
$start_msg = 1;
}
/* Initialize paginator string chunks. */
$prv_str = '';
$nxt_str = '';
$pg_str = '';
/* Create simple strings that will be creating the paginator. */
$spc = ' '; /* This will be used as a space. */
$sep = '|'; /* This will be used as a seperator. */
/* Get some paginator preference values. */
$pg_sel = 10;
$pg_max = 13;
/* Make sure that our start message number is not too big. */
$start_msg = min($start_msg, $num_msgs);
/* Compute the starting message of the previous and next page group. */
$next_grp = $start_msg + $show_num;
$prev_grp = $start_msg - $show_num;
/*Compute the basic previous and next strings. */
if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
$prv_str = get_paginator_link($prev_grp, "<<", $link);
$nxt_str = get_paginator_link($next_grp, ">>", $link);
} else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
$prv_str = get_paginator_link($prev_grp, "<<", $link);
$nxt_str = ">>"."\n";
} else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
$prv_str = "<<";
$nxt_str = get_paginator_link($next_grp, ">>", $link);
}
/* Page selector block. Following code computes page links. */
if ($pg_sel && ($num_msgs > $show_num)) {
/* Most importantly, what is the current page!!! */
$cur_pg = intval($start_msg / $show_num) + 1;
/* Compute total # of pages and # of paginator page links. */
$tot_pgs = ceil($num_msgs / $show_num); /* Total number of Pages */
$vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */
/* Compute the size of the four quarters of the page links. */
/* If we can, just show all the pages. */
if (($tot_pgs - 1) <= $pg_max) {
$q1_pgs = $cur_pg - 1;
$q2_pgs = $q3_pgs = 0;
$q4_pgs = $tot_pgs - $cur_pg;
/* Otherwise, compute some magic to choose the four quarters. */
} else {
/*
* Compute the magic base values. Added together,
* these values will always equal to the $pag_pgs.
* NOTE: These are DEFAULT values and do not take
* the current page into account. That is below.
*/
$q1_pgs = floor($vis_pgs/8);
$q2_pgs = round($vis_pgs/6, 0);
$q3_pgs = ceil($vis_pgs/2);
$q4_pgs = round(($vis_pgs - $q2_pgs)/8, 0);
/* Adjust if the first quarter contains the current page. */
if (($cur_pg - $q1_pgs) < 1) {
$extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
$q1_pgs = $cur_pg - 1;
$q2_pgs = 0;
$q3_pgs += ceil($extra_pgs / 2);
$q4_pgs += floor($extra_pgs / 2);
/* Adjust if the first and second quarters intersect. */
} else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
$extra_pgs = $q2_pgs;
$extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 0.75);
$q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 0.75);
$q3_pgs += ceil($extra_pgs / 2);
$q4_pgs += floor($extra_pgs / 2);
/* Adjust if the fourth quarter contains the current page. */
} else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
$extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
$q3_pgs = 0;
$q4_pgs = $tot_pgs - $cur_pg;
$q1_pgs += floor($extra_pgs / 2);
$q2_pgs += ceil($extra_pgs / 2);
/* Adjust if the third and fourth quarter intersect. */
} else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
$extra_pgs = $q3_pgs;
$extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
$q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 0.75);
$q1_pgs += floor($extra_pgs / 2);
$q2_pgs += ceil($extra_pgs / 2);
}
}
/*
* I am leaving this debug code here, commented out, because
* it is a really nice way to see what the above code is doing.
* echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
* . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>';
*/
/* Print out the page links from the compute page quarters. */
/* Start with the first quarter. */
if (($q1_pgs == 0) && ($cur_pg > 1)) {
$pg_str .= "...$spc";
} else {
for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
$start = (($pg-1) * $show_num) + 1;
$pg_str .= get_paginator_link($start, $pg, $link) . $spc . $spc;
}
if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
$pg_str .= "...$spc";
}
}
/* Continue with the second quarter. */
for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
$start = (($pg-1) * $show_num) + 1;
$pg_str .= get_paginator_link($start, $pg, $link) . $spc . $spc;
}
/* Now print the current page. */
$pg_str .= $cur_pg . $spc . $spc;
/* Next comes the third quarter. */
for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
$start = (($pg-1) * $show_num) + 1;
$pg_str .= get_paginator_link($start, $pg, $link) . $spc . $spc;
}
/* And last, print the fourth quarter page links. */
if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
$pg_str .= "...$spc";
} else {
if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
$pg_str .= "...$spc";
}
for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
$start = (($pg-1) * $show_num) + 1;
$pg_str .= get_paginator_link($start, $pg, $link) . $spc . $spc;
}
}
}
/* Put all the pieces of the paginator string together. */
/**
* Hairy code... But let's leave it like it is since I am not certain
* a different approach would be any easier to read. ;)
*/
$result = '';
$result .= ($prv_str != '' ? $prv_str . $spc . $spc : '');
$result .= ($pg_str != '' ? $pg_str : '');
$result .= ($nxt_str != '' ? $nxt_str . $spc : '');
/* If the resulting string is blank, return a non-breaking space. */
if ($result == '') {
$result = ' ';
}
/* Return our final magical paginator string. */
return $result;
}
if (!isset($_GET['start_msg'])) {
//echo 'not set';
echo (get_paginator_str(1, 2000, 10, 'pageing.php'));
} else {
//echo 'set';
echo (get_paginator_str($_GET['start_msg'], 2000, 10, 'pageing.php'));
}
?>