how to make links like <previous 1|2|3|4|5|6|7 next>?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kenny1192
Forum Newbie
Posts: 1
Joined: Sun Oct 15, 2006 6:16 pm

how to make links like <previous 1|2|3|4|5|6|7 next>?

Post by kenny1192 »

I need to make links like "1|2|3|4|5|..."
I believe you've ever seen this kind of stuff in google or
any other blogs.

I made a diary like blog by php.
but i already posted more than 10 diaries.
so i need to make links like google or any other blogs...
otherwise the page is too long and looks not good.

but unfortunately i have no idea how to make it.
If you know. please teach me how or give me some hints.

Code: Select all

include "conf/conf.php";
$myConn=mysql_connect($host,$uname,$pword);
$mydb=mysql_select_db($dbname,$myConn);
$sql="select * from p_news";
$rs=mysql_query($sql,$myConn);
$num_rows=mysql_num_rows($rs);
for($i=0;$i<=$num_rows-1;$i++)
	{
	    $row=mysql_fetch_array($rs);

           
            // Do i need to code some IF statements here? 


         }

Thanks in adavance.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

the term your looking for is "pagination"
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post by ryuuka »

this site has a good tutorial for that:
http://www.phpnoise.com/tutorials/9/1

linked from:
http://www.tutorialized.com
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Are you looking for something like http://stesbox.co.uk/php/db/log.php?page=1 ?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Here's how I do it.

Code: Select all

function pagination_links($page, $num_rows, $results_per_page, $each_direction = 5)
{
	$word_for_previous = 'anterior';
	$word_for_next = 'siguiente';
	$total_pages = $num_rows ? ceil($num_rows / $results_per_page) : 1 ;
	if($total_pages < 2)
	{
		return null;
	}
	$page = ((is_numeric($page)) and ($page >= 1) and ($page <= $total_pages)) ? (int)$page : 1 ;
	$output = null;
	if($page > 1)
	{
		$output .= '<a href="'.htmlentities($_SERVER['PHP_SELF']).'?page='.($page - 1).'">'.$word_for_previous.'</a> | '."\n";
	}
	for($i = $page - $each_direction; $i <= $page + $each_direction; $i++)
	{
		if(($i > 0) and ($i <= $total_pages))
		{
			$output .= isset($spacer) ? $spacer : null ; 
			$spacer = ' | '."\n";
			if($page != $i)
			{
				$output .= '<a href="'.htmlentities($_SERVER['PHP_SELF']).'?page='.$i.'">'.$i.'</a>'."\n";
			}
			else
			{
				$output .= $i."\n";
			}
		}
	}
	if($page < $total_pages)
	{
		$output .= ' | <a href="'.htmlentities($_SERVER['PHP_SELF']).'?page='.($page + 1).'">'.$word_for_next.'</a>'."\n";
	}
	return '<p class="pagination-links">'."\n".$output."\n".'</p>';
}
Post Reply