TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   about pagination !! (http://www.talkphp.com/absolute-beginners/1987-about-pagination.html)

webtuto 01-17-2008 03:08 PM

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?>


danielneri 01-17-2008 04:39 PM

just because I'm too lazy to debug your code :-P

Simple and Easy Pagination - Viper Creations

webtuto 01-17-2008 04:44 PM

thanks im gonna see it

Wildhoney 01-17-2008 05:32 PM

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.

Alan @ CIT 01-17-2008 06:04 PM

Quote:

Originally Posted by Wildhoney (Post 8763)
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


All times are GMT. The time now is 09:58 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0