TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-17-2008, 03:08 PM   #1 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default about pagination !!

hi im trying to do pagination but the pages worked but it douesnt show next and previous buttons !!!!!
PHP Code:
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<?php
    
/*
        Place code to connect to your DB here.
    */
    
include('config.php');    // include your code to connect to DB.

    
$tbl_name="tuto";        //your table name
    // How many adjacent pages should be shown on each side?
    
$adjacents 3;
    
    
/* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    
$query "SELECT COUNT(*) as num FROM $tbl_name";
    
$total_pages mysql_fetch_array(mysql_query($query));
    
$total_pages $total_pages[num];
    
    
/* Setup vars for query. */
    
$targetpage "pagination.php";     //your file name  (the name of this file)
    
$limit 2;                                 //how many items to show per page
    
$page $_GET['page'];
    if(
$page
        
$start = ($page 1) * $limit;             //first item to display on this page
    
else
        
$start 0;                                //if no page var is given, set start to 0
    
    /* Get data. */
    
$sql "SELECT title FROM $tbl_name LIMIT $start$limit";
    
$result mysql_query($sql);
    
    
/* Setup page vars for display. */
    
if ($page == 0$page 1;                    //if no page var is given, default to 1.
    
$prev $page 1;                            //previous page is page - 1
    
$next $page 1;                            //next page is page + 1
    
$lastpage ceil($total_pages/$limit);        //lastpage is = total pages / items per page, rounded up.
    
$lpm1 $lastpage 1;                        //last page minus 1
    
    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    
$pagination "";
    if(
$lastpage 1)
    {    
        
$pagination .= "<div class=\"pagination\">";
        
//previous button
        
if ($page 1
            
$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
        else
            
$pagination.= "<span class=\"disabled\">« previous</span>";    
        
        
//pages    
        
if ($lastpage + ($adjacents 2))    //not enough pages to bother breaking it up
        
{    
            for (
$counter 1$counter <= $lastpage$counter++)
            {
                if (
$counter == $page)
                    
$pagination.= "<span class=\"current\">$counter</span>";
                else
                    
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
            }
        }
        elseif(
$lastpage + ($adjacents 2))    //enough pages to hide some
        
{
            
//close to beginning; only hide later pages
            
if($page + ($adjacents 2))        
            {
                for (
$counter 1$counter + ($adjacents 2); $counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                
$pagination.= "...";
                
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            
//in middle; hide some front and some back
            
elseif($lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
            {
                
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                
$pagination.= "...";
                for (
$counter $page $adjacents$counter <= $page $adjacents$counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                
$pagination.= "...";
                
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            
//close to end; only hide early pages
            
else
            {
                
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                
$pagination.= "...";
                for (
$counter $lastpage - (+ ($adjacents 2)); $counter <= $lastpage$counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
            }
        }
        
        
//next button
        
if ($page $counter 1
            
$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
        else
            
$pagination.= "<span class=\"disabled\">next »</span>";
        
$pagination.= "</div>\n";        
    }
?>

    <?php
        
while($row mysql_fetch_array($result))
        {
    
        
// Your while loop here
         
echo "<center>Title: " $row['title'] . "</a><br>";
    
        }
    
?>

<?=$pagination?>
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-17-2008, 04:39 PM   #2 (permalink)
The Contributor
 
Join Date: Dec 2007
Location: Florida
Posts: 73
Thanks: 12
danielneri is on a distinguished road
Default

just because I'm too lazy to debug your code

Simple and Easy Pagination - Viper Creations
Send a message via AIM to danielneri
danielneri is offline  
Reply With Quote
The Following User Says Thank You to danielneri For This Useful Post:
webtuto (01-17-2008)
Old 01-17-2008, 04:44 PM   #3 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

thanks im gonna see it
__________________
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-17-2008, 05:32 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

There is a lot of code there, Webtuto! I think over the years, an abundance of coders of all skills have tried to make pagination as simple as possible - to the point where we end up with many choices, but no standards. Pagination still remains one of those programming sub-topics that is often reinvented time and time again for each individual project - albeit some programmers may reuse their pagination classes, but they've still, at one time or another, reinvented the wheel by coding it from scratch.

With that said, it would be lovely to see a real solid pagination class that would become somewhat of a standard in the PHP world. For all I know, there could well be one - but when a search for a pagination class in Zend's own Zend Framework yields very few results, with the first result claiming it's not possible, it seems logical to assume that there is yet to be a pagination class that is both flexible and practical to have been the favoured method.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-17-2008, 06:04 PM   #5 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
but when a search for a pagination class in Zend's own Zend Framework yields very few results
If it helps, there is a proposel for Zend_Paginator in the works - doesn't look like it's going anywhere fast though :(

You are right though, pagination really is a custom job depending on the application layout, data source, etc.

Edit: And if you did need a pagination class for the Zend Framework, here's a view script that Matthew posted to the mailing list ages ago that should still work pretty well:

PHP Code:
<?php
   $pages 
$this->pages;
   
$count count($pages);
   if (
$count):
       
$startPage 1;
       
$endPage   $count;

       if (
$count 9) {
           
$startPage = ($this->curPage 6) ? $this->curPage 4;
           
$endPage   = ($this->curPage $count) ? $count $this->curPage 4;
           if ((
$startPage 5) && ($endPage $count)) {
               
$endPage $startPage 8;
           } elseif ((
$endPage == $count) && ($endPage $startPage 9)) {
               
$startPage $endPage -8;
           }
       }
   
?>
   <ul class="pager">
       <? if ($this->curPage): ?><li><a href="<?= $baseUrl ?>1" title="first page">&lt;&lt;</a></li><? endif ?>
       <? if ($this->curPage 1):?><li><a href="<?= $baseUrl ?><?= $this->curPage 1 ?>" title="previous page">prev</a></li><? endif; ?>
   <? for ($i $startPage$i <= $endPage$i++): ?>
       <? $page $i ?>
       <? if ($this->curPage == $page): ?>
       <li class="current"><?= $page ?></li>
       <? else: ?>
       <li><a href="<?= $baseUrl ?><?= $page ?>" title="page <?= $page ?>"><?= $page ?></a></li>
       <? endif; ?>
   <? endfor ?>
       <? if ($this->curPage $count):?><li><a href="<?= $baseUrl ?><?= $this->curPage 1 ?>" title="next page">next</a></li><? endif; ?>
       <? if ($count $this->curPage): ?><li><a href="<?= $baseUrl ?><?= $count ?>" title="last page">&gt;&gt;</a></li><? endif ?>
   </ul>
   <? endif ?>

/*
Usage is that you set the following:

   $view->baseUrl = $baseUrlToData;
   $view->pages   = $numPages;
   $view->curPage = $currentPage;

It creates a sliding pager -- i.e., it doesn't show *all* pages, but a window
of 8 pages, with links to the prev/next pages and first/last pages.

It's up to your controller, then, to determine (a) how many items to
display per page, and thus (b) how many pages there are, as well as (c)
the routing schema you'll use for getting to different pages (hence the
baseUrl). You determine the current page based on the page requested.
*/
Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:34 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design