db results page script thing

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
timhortons
Forum Commoner
Posts: 29
Joined: Thu Jan 15, 2004 11:48 am
Location: Calgary, AB, Canada

db results page script thing

Post by timhortons »

i saw the "function: db results pagination" and thought i'd post the one i did... it's got flaws, it's messy, and could probably use cleaning, but what the heck... :P

sorry for the indent issues... dreamweaver really likes to muck around with tabs... 8O

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;
}
example usage:

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', ',', '.', ';');
?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

it's got flaws, it's messy, and could probably use cleaning
Then this isn't the forum to post it ;)
timhortons
Forum Commoner
Posts: 29
Joined: Thu Jan 15, 2004 11:48 am
Location: Calgary, AB, Canada

Post by timhortons »

well i'm always critical of my own work, but actually it works great as long as you have more than three pages :P
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

If you'd like to try a refactoring exercise, what are all the separate responsibilities of a pagination script?
timhortons
Forum Commoner
Posts: 29
Joined: Thu Jan 15, 2004 11:48 am
Location: Calgary, AB, Canada

Post by timhortons »

yeah thats a good idea to refactor it.... well i'll give it a go again sometime, and i'll post the new version :P
Post Reply