Jump to content

[SOLVED] Pagination


Aureole

Recommended Posts

I'm wanting to do a pagination, but the way I want to do it is different to the ways it's done in all the tutorials I found (on PHP Freaks and Google).

 

If you look here, that's pretty much how I'd like to have my pagination.

 

Images for first page, previous page, next page and last page, which most tutorials have but then the way they show the pages in the middle is the part I'm interested in... I can't find anything like it.

 

If there are 7 pages or more then it shows those little dots as you can see, but in cases where there aren't 7 pages it just shows them all.

 

Anyone have any idea how I could approach something like this?

Link to comment
Share on other sites

try

<?php
$total_pages = 12;
$curent_page = 11;
if ($total_pages <  {
$start = 1;
$end = $total_pages;
}
else {
if ($curent_page > 3){
	if ($total_pages - $curent_page < 3) $end = $total_pages;
	else $end = $curent_page + 3;
	$start = $end - 7;
} else {
	$start = 1;
	$end = 7;
}
}
if ($start>1) echo '...';
for ($i = $start; $i <= $end; $i++){
if ($i == $curent_page) echo " $i "; else echo " <a href=''> $i </a>";
}
if ($end < $total_pages) echo '...';
?>

Link to comment
Share on other sites

Hmm, that's almost it! Is there any chance someone could help me get the above built into what I have now.

 

I stripped down my script to the bare basics (all the HTML and including/requiring and a crap load of other things are gone) to make it easier. I have tried myself and I just can't get it working.

 

<?php
$page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;

$query = "SELECT count(*) FROM `members` WHERE `mem_online` = '1'";
$result = mysql_query( $query );
$query_data = mysql_fetch_row( $result );
$numrows = $query_data[0];

$rows_per_page = 20;
$lastpage = ceil( $numrows / $rows_per_page );

$page = (int)$page;

if( $page < 1 )
{
   $page = 1;
}
elseif( $page > $lastpage )
{
   $page = $lastpage;
} 

$limit = 'LIMIT ' .($page - 1) * $rows_per_page .',' .$rows_per_page;

?>

<table class="main_table" cellspacing="2">
<?php
$query = "SELECT * FROM `members` INNER JOIN `groups` ON `groups` . `group_id` = `members` . `mem_group` WHERE `mem_online` = '1' $limit";
$resultA = mysql_query( $queryA );
while( $rowA = mysql_fetch_assoc( $resultA ) )
{
$member->format_name_apos( $rowA['mem_dname'] );
$apos = $member->output;
$member->timezone( $_SESSION['mem_timezone'], $rowA['mem_last_action'] );
$last_action = $member->output;
$last_action = date( 'l  jS M Y, g:i A', $last_action );
?>

                    	<tr>
                    		<td width="30%" class="black_td"><p class="content"><a href="http://www.veraci7y.net/revolution/profile.rev?id=<?php echo( $rowA['mem_id'] ); ?>" class="nav_container_current_a" title="View <?php echo( $apos ); ?> Profile."><span style="color:<?php echo( $rowA['group_color'] ); ?>;"><?php echo( $rowA['mem_dname'] ); ?></span></a> <?php if( !isset( $_GET['online'] ) ) { echo( $rowA['mem_online'] == 1 ) ? '(Online)' : '(Offline)'; } ?></p></td>
                    		<td width="70%" class="gray_td"><p class="content"><?php echo( 'Last seen ' . $rowA['mem_where'] . ', ' . $last_action . '.'); ?></p></td>
                    	</tr>
<?php
}

if ($page == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?page=1'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>PREV</a> ";
} // if
echo " ( Page $page of $lastpage ) ";

if ($page == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $page+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$lastpage'>LAST</a> ";
} // if

?>
                    </table>

Link to comment
Share on other sites

<?php
if ($page == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?page=1'>FIRST</a> ";
   $prevpage = $page-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>PREV</a> ";
} // if
echo " ( Page $page of $lastpage ) ";


$total_pages = $lastpage;
$curent_page = $page;
if ($total_pages <  {
$start = 1;
$end = $total_pages;
}
else {
if ($curent_page > 3){
	if ($total_pages - $curent_page < 3) $end = $total_pages;
	else $end = $curent_page + 3;
	$start = $end - 6;
} else {
	$start = 1;
	$end = 7;
}
}
if ($start > 1) echo '...';
for ($i = $start; $i <= $end; $i++){
if ($i == $curent_page) echo " $i "; else echo " <a href='?page=$i'> $i </a>";
}
if ($end < $total_pages) echo '...';


if ($page == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $page+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?page=$lastpage'>LAST</a> ";
} // if
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.