sorry for the indent issues... dreamweaver really likes to muck around with tabs...
Code: Select all
function queryPages ($limit, $show, $num_rows, $increment=5, $word='Page', $seperator='', $valSep='=', $varSep='&')
{
$linkStr = '';
$linkStr2 = '';
if($limit > 0 && $limit < $num_rows)
{
$morePages = ceil($num_rows / $limit);
$curPage = (floor($show / $limit)+1);
$middle = floor($increment / 2);
$curShow = $show;
$k = $curPage+1;
$l = $curPage-$middle;
# First Page
if($curPage > 1)
{
if(isset($limit))
$lim = "limit{$valSep}".$limit.$varSep;
else
$lim = '';
if(preg_match("/(.*)limit{$valSep}(\d+){$varSep}show{$valSep}(\d+)(.*)/",$_SERVER['QUERY_STRING'],$match))
{
$match[1] = !empty($match[1]) ? $match[1] : '';
$linkStr2 .= "<a href='?{$match[1]}{$lim}show{$valSep}0{$match[4]}'>{$word} 1</a> ... ";
}
else
$linkStr2 .= " <a href='?{$lim}show{$valSep}".($curShow2)."'>{$word} 1</a> ... ";
}
for($j=0;$j<$increment;$j++)
{
if(isset($limit))
$lim = "limit{$valSep}".$limit.$varSep;
else
$lim = '';
# Start of pages
if($j < $middle && 0 < $l)
{
if($morePages > $j && $j != 0 && $l > 0)
{
if(empty($seperator))
$linkStr2 .= ',';
else
$linkStr2 .= $seperator.' ';
}
$correction1 = ceil(($increment - $middle)-1);
$correction2 = $show - ($limit * ceil(($increment - $middle)-1));
$curShowLow = $correction2;
$curShow2 = $curShowLow + $limit*$j;
if(preg_match("/(.*)limit{$valSep}(\d+){$varSep}show{$valSep}(\d+)(.*)/",$_SERVER['QUERY_STRING'],$match))
{
$match[1] = !empty($match[1]) ? $match[1] : '';
$linkStr2 .= " <a href='?{$match[1]}{$lim}show{$valSep}".($curShow2)."{$match[4]}'>{$word} ".($l++)."</a>";
}
else
$linkStr2 .= " <a href='?{$lim}show{$valSep}".($curShow2)."'>{$word} ".($l++)."</a>";
}
# Middle Page
if($j == $middle)
{
if($l > 0)
$linkStr2 .= !empty($seperator) ? $seperator.' ' : ',';
$linkStr2 .= !empty($word) ? ' '.$word.' '.$curPage : ' '.$curPage;
}
# Pages after the middle mark
if($j > $middle && $k < $morePages)
{
if($morePages > $j && $j != 0 && $k < $morePages)
{
if(empty($seperator))
$linkStr2 .= ',';
else
$linkStr2 .= $seperator.' ';
}
$correction1 = ceil(($increment - $middle)-1);
$correction2 = $show - ($limit * ceil(($increment - $middle)-1));
$curShowLow = $correction2;
$curShow2 = $curShowLow + $limit*$j;
if(preg_match("/(.*)limit{$valSep}(\d+){$varSep}show{$valSep}(\d+)(.*)/",$_SERVER['QUERY_STRING'],$match))
{
$match[1] = !empty($match[1]) ? $match[1] : '';
$linkStr2 .= "<a href='?{$match[1]}{$lim}show{$valSep}".($curShow2)."{$match[4]}'>{$word} ".($k++)."</a>";
}
else
$linkStr2 .= "<a href='?{$lim}show{$valSep}".($curShow2)."'>{$word} ".($k++)."</a>";
}
}
# Last Page Link
if($curPage < $morePages)
{
$correction1 = $morePages - $curPage;
if(isset($limit))
$lim = "limit{$valSep}".$limit.$varSep;
else
$lim = '';
if(preg_match("/(.*)limit{$valSep}(\d+){$varSep}show{$valSep}(\d+)(.*)/",$_SERVER['QUERY_STRING'],$match))
{
$match[1] = !empty($match[1]) ? $match[1] : '';
$linkStr2 .= " ... <a href='?{$match[1]}{$lim}show{$valSep}".($show+($limit*$correction1))."{$match[4]}'>{$word} $morePages</a>";
}
else
$linkStr2 .= " ... <a href='?{$lim}show{$valSep}".($show+($limit*$correction1))."'>{$word} $morePages</a>";
}
}
return $linkStr2;
}Code: Select all
$show = !empty($_GET['show']) ? $_GET['show'] : '0';
echo queryPages(20, $show, 816, 5, 'Page', ',', '=', '&');
# 20 = the number of results to display on one page
# $show = the page we're on
# 816 = fake number of results. change this to the total number of results from your query
# 5 = the number of pages to link... eg: 1, 2, [3], 4, 5,
# Page = the word before the page number
# , = the seperator between pages
# = = the value seperator for the url link
# & = the variable seperator for the url link
# WORKING EXAMPLE:
# setup our $_GET vars for this
$limit = isset($get_vars['limit']) ? $get_vars['limit'] : 2;
$show = isset($get_vars['show']) ? $get_vars['show'] : 0;
# Do the query...
$query = "SELECT * FROM news, members WHERE news.author_id = members.member_id ORDER BY "$orderby" $order LIMIT $limit OFFSET $show";
$result = mysql_query($query) or die("<P>Query failed: ".mysql_error());
mysql_free_result($result);
# print the result of that query here...
# ...
# ...
$result = mysql_query("SELECT * FROM news");
$num_rows = mysql_num_rows($result);
mysql_free_result($result);
# now output the page link script thing aka "paginator"
echo queryPages($limit, $show, $num_rows, 9, 'Page', ',', '.', ';');