I need help with pagination.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

I need help with pagination.

Post by Maluendaster »

hey all.. I'm still a n00b in this, i have a problem and don't know how to do it, i've been trying to do it since a week and 0 results, can anyone help me? I need to show the :

< Previous - 1 2 3 4 5 .. 10 - Next >

I think i have to insert it in this code (show_gals.php) This file shows all galleries posted in a MySQL Databes

Code: Select all

<?php
   include("insertar/conex.php");
   $link=Conectarse();
   $result=mysql_query("select * from galeria ORDER by ID_Prueba DESC",$link);
?>
   <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width=100% style="BORDER-RIGHT: #373737 1px solid; BORDER-TOP: #373737 1px solid; BORDER-LEFT: #373737 1px solid; BORDER-BOTTOM: #373737 1px solid">

<?php      

   while($row = mysql_fetch_array($result)) {
      printf("<tr><td bgcolor=373737><center><font face=verdana size=2 color=white><b>%s</b></center></td></tr><tr><td><center><br><img src=%s><br></center><center><a href=out.php?dire=%s target=_blank><br><img src=common/view_gallery.gif></a></center></td></tr>", $row["Nombre"], $row["Imagen"],$row["Url"]);
   }
   mysql_free_result($result);
   mysql_close($link);
?>
Can anyone??? please..

It's for my site, don't know if i can put the url since it's a erotic site....

Thank you.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

This isn't really that hard to do. The basic concept is this:

Code: Select all

<?php 
   include("insertar/conex.php"); 
   $link=Conectarse(); 
   $limit = 25;
   if (!$page)
	$page = 1;
	
   $limitvalue = $page * $limit - ($limit); 
   $result=mysql_query("select * from galeria ORDER by ID_Prueba DESC LIMIT $limitvalue, $limit",$link); 
?> 
   <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width=100% style="BORDER-RIGHT: #373737 1px solid; BORDER-TOP: #373737 1px solid; BORDER-LEFT: #373737 1px solid; BORDER-BOTTOM: #373737 1px solid"> 

<?php       

   while($row = mysql_fetch_array($result)) { 
      printf("<tr><td bgcolor=373737><center><font face=verdana size=2 color=white><b>%s</b></center></td></tr><tr><td><center><br><img src=%s><br></center><center><a href=out.php?dire=%s target=_blank><br><img src=common/view_gallery.gif></a></center></td></tr>", $row["Nombre"], $row["Imagen"],$row["Url"]); 
   } 

if ($page != 1)
{  
        $pageprev = $page - 1; 
        echo "<a href=?page=$pageprev><<Previous</a>"; 
}

    $numofpages = round(mysql_num_rows($result) / $limit); 
     
for($i = $page; $i <= $numofpages; $i++)
{ 
        
           if($i == $page)
                   echo $i;
           else
            {
         
              echo "<a href=?page=$i>$i</a>";

               if ($i % 15 == 0)
               {
                    echo "...";
	     $i = $numofpages+1;
	}//end if
             } //end else
} //end for

    if(($count - ($limit * $page)) > 0)
   { 
        $pagenext = $page + 1; 
          
        echo "<a href=?page=$pagenext>Next >></a>";  
    }

   mysql_free_result($result); 
   mysql_close($link); 
?>
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

thank you! i'll try that and tell you how it went... you are a nice human... :)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

you're welcome. We've all been there before.
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

it woked, but the pagination shows just a 1 , no next or something.... did i do something wrong? , I got the limit at 5 and i have like 40 entries in the db.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Do a forum search for pagination. You might find something helpful in the other topics about pagination.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

I Can't make it work!!!
Jade please, help me.. here's the code....

Code: Select all

<?php 
   include("insertar/conex.php"); 
   $link=Conectarse(); 
   $limit = 500; 
   $page = $_GET['page'];
   if (!$page) 
   $page = 1; 
     
   $limitvalue = $page * $limit - ($limit); 
   $result=mysql_query("select * from galeria ORDER by ID_Prueba DESC LIMIT $limitvalue, $limit",$link); 
?> 
   <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width=100% style="BORDER-RIGHT: #373737 1px solid; BORDER-TOP: #373737 1px solid; BORDER-LEFT: #373737 1px solid; BORDER-BOTTOM: #373737 1px solid"> 

<?php        

   while($row = mysql_fetch_array($result)) { 
      printf("<tr><td bgcolor=373737><center><font face=verdana size=2 color=white><b>%s</b></center></td></tr><tr><td><center><br><img border=0 src=%s><br></center><center><a href=out.php?dire=%s target=_blank><br><img src=common/view_gallery.gif></a></center></td></tr>", $row["Nombre"], $row["Imagen"],$row["Url"]); 
   } 

if ($page != 1) 
{   
        $pageprev = $page - 1; 
        echo "<a href=?page=$pageprev>Previous</a>"; 
} 
	$numofpages = round(mysql_num_rows($result) / $limit); 
      
for($i = $page; $i <= $numofpages; $i++) 
{ 
         
           if($i == $page) 
                   echo $i; 
           else 
            { 
          
              echo "<a href=?page=$i>$i</a>"; 

               if ($i % 15 == 0) 
               { 
                    echo "..."; 
         $i = $numofpages+1; 
    }//end if 
             } //end else 
} //end for 

    if(($count - ($limit * $page)) > 0) 
   { 
        $pagenext = $page + 1; 
           
        echo "<a href=?page=$pagenext>Next</a>";   
    } 

   mysql_free_result($result); 
   mysql_close($link); 
?>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

viewtopic.php?t=38830

a very good pagentation class. if you cant figure out how to work that then you should really just search this forum for pagentation and you will find many results with answers
Post Reply