Jump to content

MySQL Pagination


jfarthing

Recommended Posts

Does anybody know how to control the number of page links displayed with pagination?  I mean like, with the tutorial on this site, if I had, say 50 pages of results, it actually creates individual links for page 1-50.  How could I make it truncate say 1-5, and then an arrow to goto 6-11 and vice versa?  Heere is the code the tutorial uses

    for($i = 1; $i <= $numofpages; $i++){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
        } 
    } 

Link to comment
Share on other sites

Here is how I paginate my results like what you wanted

 

<?php 

include "includes/connection.php";

$webpage = basename(news); //need to be the name of the page i.e. news.php
function pagination_five($total_pages,$page){ 

    global $webpage; 

    $max_links = 10; 
    $h=1; 
    if($page>$max_links){ 
        $h=(($h+$page)-$max_links); 
    } 
    if($page>=1){ 
        $max_links = $max_links+($page-1); 
    } 
    if($max_links>$total_pages){ 
        $max_links=$total_pages+1; 
    } 
    if($total_pages>1){ 
echo '<div class="page_numbers"><ul>'; 
if($page>"1"){ 
    echo '<li class="current"><a href="/'.$webpage.'/1">First</a></li> 
        <li class="current"><a href="/'.$webpage.'/'.($page-1).'">Prev</a></li> 
    '; 
} 

if($total_pages!=1){ 
    for ($i=$h;$i<$max_links;$i++){ 
	if($i==$page){ 
	    echo '<li><a class="current">'.$i.'</a></li>'; 
	} 
	else{ 
	    echo '<li><a href="/'.$webpage.'/'.$i.'">'.$i.'</a> </li>'; 
	} 
    } 
} 

if(($page >="1")&&($page!=$total_pages)){ 
    echo '<li class="current"><a href="/'.$webpage.'/'.($page+1).'">Next</a></li> 
    	<li class="current"><a href="/'.$webpage.'/'.$total_pages.'">Last</a></li> 
    '; 
} 
echo '</ul></div>';
    }  
}

$result = mysql_query("Select count(*) from news WHERE archived='n'") 
or die (mysql_error()); 
$numrows = mysql_fetch_row($result); 
  
if(isset($_GET['pagenum'])?$page = $_GET['pagenum']:$page = 1); 
$entries_per_page = 1;   

$total_pages = ceil($numrows[0]/$entries_per_page); 
$offset = (($page * $entries_per_page) - $entries_per_page); 

    //after we have $total_pages and $page, we can include the  
    //pagination style wherever we want on the page. 
    //so far there is no output from the script, so we could have  
    //pagination before or after the pages results 
     
    //before the results  

$result = mysql_query("SELECT id,title,content, DATE_FORMAT(time, '%W %D %M %Y') as date, photo, alternate FROM news WHERE archived='n' ORDER BY id DESC LIMIT  
                       $offset,$entries_per_page");
  if(!$result) die(mysql_error()); 
     $err = mysql_num_rows($result); 
       if($err == 0) die("No matches met your criteria."); 

while($row=mysql_fetch_array($result)){ 

$title = htmlentities(strtoupper($row['title'])); 
$content = htmlentities($row['content']);

    if (!$row['photo']){ 
echo "<div id='right'><h3>".$title."</h3><p class='italic'>Date added: ".$row['date']."</p><p>".nl2br($content)."</p><br /></div>\n";	
    } else {
echo "<div id='right'><h3>".$title."</h3><p class='italic'>Date added: ".$row['date']."</p><p><img src='/images/large/".$row['photo']."' alt='".$row['alternate']."' class='right' />".nl2br($content)."</p><br /></div>\n";

    } 
} 

  //or after the results 
   
pagination_five($total_pages,$page); 

 

and here is the CCS file to style the links

 

.page_numbers { 
    width: 600px; 
    padding: 5px 0px; 
    float:left;
    clear:left;
    margin:0 auto; 
} 

.page_numbers ul { 
    margin: 0 auto; 
    list-style-type: none; 
    padding: 0px; 
    text-align: center; 
} 

.page_numbers li { 
    display: inline; 
    float: left; 
    margin:1px; 
    background: #a7a7a7; 
    width:25px; 
} 

.page_numbers li.current{ 
  width:50px; 
} 

.page_numbers li a { 
    background: #fff; 
    border: 1px solid #a7a7a7; 
    padding: 1px; 
    text-decoration: none; 
    color: #000; 
    font:bold 8px verdana,sans-serif; 
    display:block; 
} 

.page_numbers a.current, .page_numbers li a:hover { 
    background: #a7a7a7; 
    color: #fff; 
}

Link to comment
Share on other sites

Thanks but, its not working exactly as I was expecting. Page 1 shows 1 2 3, Page 2 shows 1 2 3 4, Page 3 shows 1 2 3 4 5...I want it to be like this

 

Page 1 (as well as Page 2, 3, 4 and 5)

1 2 3 4 5 >> (Clicking the >> will take you to page 6 to see the 6-10 menu)

 

Page 6

<< 6 7 8 9 10 (Clicking the << will take you to page 5 to see the 5-1 menu)

 

See what I mean?

Link to comment
Share on other sites

Here is an example of what your code does with the max_links variable set to 6, the variables are the final after all the manipulation and then the output....

 

Page 1

h=1

maxlinks=6

1 2 3 4 5

 

Page 5

h=1

maxlinks=10

1 2 3 4 5 6 7 8 9

 

Page 10

h=5

maxlinks=11

5 6 7 8 9 10

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.