Page 1 of 1

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

Posted: Sun Oct 15, 2006 6:26 pm
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.

Posted: Sun Oct 15, 2006 6:42 pm
by John Cartwright
the term your looking for is "pagination"

Posted: Mon Oct 16, 2006 4:13 am
by ryuuka
this site has a good tutorial for that:
http://www.phpnoise.com/tutorials/9/1

linked from:
http://www.tutorialized.com

Posted: Mon Oct 16, 2006 4:33 am
by impulse()
Are you looking for something like http://stesbox.co.uk/php/db/log.php?page=1 ?

Posted: Mon Oct 16, 2006 5:32 am
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>';
}