FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Pagination





AOP Web Development
hello to everyone.. i just practice on how to do a pagination... and i already did it.. the problem is how i can limit to view the numbers of the pagination


for example if it display

1 2 3 4 5

and after that if i want to go to page 6 so the numbering will be

6 7 8 9 10

but displaying this is depend on how many pagination i want if i want 7 number links then

suppose it is 1 2 3 4 5 then.. if want to make it 7 so the display will be

1 2 3 4 5 6 7


Is there any better idea.. hope you can help me on it.. thanks a lot in advance...
Laughing Laughing Laughing Laughing Laughing Razz Razz Razz
Manofgames
Could you please paste your source code??
Thanks
ammonkc
It would be really simple if you used a prebuild pager class. there are some decent pager libraries in the pear repository.
AOP Web Development
Manofgames wrote:
Could you please paste your source code??
Thanks



Code:
<?
session_start();
require_once "libclass.php";
$lib = new libclass();
$lib->connect_db();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style>
body{
font-family:Arial, Helvetica, sans-serif;
font-size:11px;
}
a{
font-family:Arial, Helvetica, sans-serif;
font-size:11px;
text-decoration:none;
}
</style>
</head>

<body>
<?
/*THIS IS WHERE THE PAGINATION START*/
global $lastpage;

$pagerow = 2;


if(!isset($page))
$page =1;


$sql="Select * from tblmember";
$result=$lib->sql_execute($sql);

$lastpage = ceil($result[1]/$pagerow);


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

$limitvalue = ($page-1) * $pagerow;


$sql="Select * from tblmember limit $limitvalue,$pagerow";
$result1= $lib->sql_execute($sql);
/*START IN PAGINATION DISPLAY ABOVE*/

echo ($_REQUEST['page']==1)?'<<&nbsp; ':"<a href=\"$_SERVER[PHP_SELF]?page=1\"><<&nbsp; </a>";
if($page==1){
echo "<&nbsp; ";
}elseif($page>1){
$page--;
echo "<a href=\"$_SERVER[PHP_SELF]?page=$page\"><&nbsp; </a>";
}

/*HERE WHERE THE PAGINATION NUMBER DISPLAY
LIKE 1 2 3 4 5
*/
   for($ctr=1;$ctr<=$lastpage;$ctr++){
      if($ctr== $_REQUEST['page'])
         echo "<b>&nbsp;$ctr&nbsp;</b>";
      else
         echo "<a href=\"$_SERVER[PHP_SELF]?page=$ctr\">&nbsp;$ctr&nbsp;</a>";         

   }


if($_REQUEST['page'] >= $lastpage){
echo " &nbsp;>";
}elseif($page<$lastpage){
$page+1;
$pagenext=$_REQUEST['page']+1;
echo "<a href=\"$_SERVER[PHP_SELF]?page=$pagenext\"> &nbsp;></a>";
}

echo ($_REQUEST['page']==$lastpage)?' &nbsp>>':"<a href=\"$_SERVER[PHP_SELF]?page=$lastpage\"> &nbsp;>></a>";


/*END IN PAGINATION DISPLAY ABOVE*/
while($row=mysql_fetch_array($result1[0])){
$bgcolor=($bgcolor=="#e0e0e0")?"#f0f0f0":"#e0e0e0";
?>
<table style="border:1px solid #202020;" width="400" border="0" cellspacing="0" cellpadding="0">
  <tr bgcolor="<?=$bgcolor?>">
    <td width="150">User Name: </td>
    <td width="350"><?=$row['u_username']?></td>
  </tr>
  <tr>
    <td width="150">Password:</td>
    <td width="350"><?=$row['u_password']?></td>
  </tr>
  <tr>
    <td width="150">Rights:</td>
    <td width="350"><?=$row['u_rights']?></td>
  </tr>
  <tr>
    <td>Status:</td>
    <td width="350"><?=$row['u_status']?></td>
  </tr>
  <tr>
    <td height="5" width="150"></td>
    <td height="5" width="350"></td>
  </tr>
</table>
<?
}
echo "</br></br></br>";
?>
</body>
</html>







Here is the code that i done.. will actually i just got it in a tutorial.


this is the link

http://www.phpfreaks.com/tutorials/43/5.php


hope you can share or revise it.......
ALostSoul
Oh my god the horror.....That is horribly coded by my looking.
Manofgames
I'd suggest you get a new set of code, there's loads of mistakes in it.

try this tutorial / script:
http://www.hotscripts.com/Detailed/41435.html
ammonkc
Take a look at these pager classes.

http://www.phpclasses.org/browse/package/3352.html

http://phpclasses.pub.ayion.com/browse/package/92.html

http://www.phpclasses.org/trackback/browse/package/3713.html

From the looks of the code above, I think it would be a lot simpler for you to use one of these free packages. It will probably work a lot better also. I've written my own pagination scripts before, and its a lot of work for something so simple.
AOP Web Development
Well thanks for all the links that you give but i just want to create my own pagination just to extend my knowledge in doing it..
jinger89
I have kind of a similar question.

I'm interesting in making a page system that displays 10 posts per page. The posts are pulled from a MySQL database that is sorted by id and date. However, there may be posts missing so that I can't just read posts 1-10 for the first page, and 11-20 for the second, and so forth. It maybe be something like posts 1-12 for the first page, because posts 7 and 9 where deleted. Therefore the second page must start at post 13.

I'm having trouble developing a system to correctly move from page to page (forward and back buttons), and having trouble figuring out how to display page numbers because there is low consistency.

Any help would be great!
sonam
Quote:
It maybe be something like posts 1-12 for the first page, because posts 7 and 9 where deleted. Therefore the second page must start at post 13.


I don't know how is working Mysql pagination but if you are using PHP then you get results array from MySql, isn't it? In that case first clear this array from deleted id's and then sort it, count it and start pagination independent of new array key.

Sonam
jinger89
I like your idea of using an array to help me sort out the pages. However, I'm not quite clear on what you're specifically suggesting.

Are you saying that I should sort all the id's, insert them into an array, then use the array key to help me determine the page number and such?
gcaughill
jinger89 wrote:
I like your idea of using an array to help me sort out the pages. However, I'm not quite clear on what you're specifically suggesting.

Are you saying that I should sort all the id's, insert them into an array, then use the array key to help me determine the page number and such?


Shouldn't the pagination part be handled by MySQL?
sonam
gcaughill wrote:
Shouldn't the pagination part be handled by MySQL?

YES of course but we are talking about another way. If you are read my previous post you will see I am wrote "I don't know how is working Mysql pagination..."

jinger89 wrote:
Are you saying that I should sort all the id's, insert them into an array, then use the array key to help me determine the page number and such?

Yeah, I am saying that. For example:

Code:

// select all from table
$result = mysql_query("SELECT * FROM table); 
      // distribute results in new array
      while ($row = mysql_fetch_assoc( $result )) {
            $rows[] = $row;
     }
$count = count($rows);


Of course you didn't need to sellect all with * like in my example. Now you have one array what you can cut in 5, 6 or any number of result per page. In most situation I transfer this $rows in session and distribute thru pages without any problem and what is best it is very fast page download. Working example is here (go right down and click on one link in "Izbor knjiga prema nakladniku" or "Izbor stripova prema nakladniku"):
http://www.sipar.hr/knjizara.php

Sonam
mathiaus
Why select everything? If you only show say 10 on each page, you only really need to select those ten to be shown.

The logic I use on my pagination is;
1. Query - Select the count of what you want from the database
2. Work out how many pages, start row number (to be used in the query below), how many rows to display etc,
3. Select the relevant rows for this page
MySQL Select LIMIT wrote:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

With one argument, the value specifies the number of rows to return from the beginning of the result set:

SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.

For prepared statements, you can use placeholders (supported as of MySQL version 5.0.7). The following statements will return one row from the tbl table:

SET @a=1;
PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?';
EXECUTE STMT USING @a;

The following statements will return the second to sixth row from the tbl table:

SET @skip=1; SET @numrows=5;
PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?';
EXECUTE STMT USING @skip, @numrows;

For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.

If LIMIT occurs within a subquery and also is applied in the outer query, the outermost LIMIT takes precedence. For example, the following statement produces two rows, not one:

(SELECT ... LIMIT 1) LIMIT 2;




If you have a small amount of data then downloading everything to count is ok but when you want a flexible system to cope with large quantities of data, only take what you need.
jinger89
@Sonam -
I've implemented you're suggestion and it's working wonderfully. I have a wide range of control and the use of the array really made the system easier because I didn't have to deal with inconsistent post ID's. Thank you so much!

@mathiaus -
Interesting idea, I believe I've tried it before, but due to the way I want to display page numbers I ran into some problems figuring out exactly how many pages I had and how to properly jump to specific pages. Actually, now that I rethink this idea, there may be some hope. I'll rework my solution in the future when I time to incorporate your suggestion.
sonam
Quote:
I've implemented you're suggestion and it's working wonderfully. I have a wide range of control and the use of the array really made the system easier because I didn't have to deal with inconsistent post ID's. Thank you so much!


Great if you find good solution. Very Happy
mathiaus is right if you have large DB maybe is better to use MySql pagination but I am working with only 1000 rows and make limit with WHERE command.

Sonam
urtrivedi
I am posting my own code, you need to rework on it according to your need.



Code:

   <?php
   
   function linkstr($reccount,$rowperpage,$totalpages)
   {
   

      $showlinks=4; //how many links to show 1|2|3|4
      $frompage=$_POST['page'];
      $topage=$_POST['page']+$showlinks;
      if ($topage>$totalpages)
         $topage=$totalpages;
       
      if ($totalpages>$showlinks )
      {
         if (($topage-$frompage)<$showlinks)
            $frompage=$topage-$showlinks;
      }
      else
      {
         $frompage=1;
         $topage=$totalpages;
      }
       
      $prevpageno=$frompage-1;
      $nextpageno=$topage+1;
      if($nextpageno>$totalpages)
         $nextpageno=$totalpages;
       
      $linkstr=   "";
      $imgno="";
      $prevchar="";//chr(171);
      $nextchar="";//chr(187);

      if ($totalpages>1)
      {
           
          if ($frompage>1)
            $linkstr.= "\n<a class='text12' href='javascript:link_click(1);' title='First'><img src='../images/first{$imgno}.gif' border=0 >{$prevchar}{$prevchar}</a> | ";   
          if ($frompage>2)             
            $linkstr.=  "\n<a  class='text12' href='javascript:link_click({$prevpageno});' title='Previous'><img src='../images/previous{$imgno}.gif' border=0 >{$prevchar}</a> | ";   
         
         //for loop
         for ($i=$frompage;$i<=$topage;$i++)
         {
             if ($_POST['page']!=$i)
               $linkstr.= "\n<a  class='text12' href='javascript:link_click({$i});'><u><strong>";
            $linkstr.= $i;
             if ($_POST['page']!=$i)             
               $linkstr.= "</strong></u></a>";   
            if ($i<$topage)       
               $linkstr.= " | ";
         }
         //end for
         if ($topage<$totalpages-1)
            $linkstr.=   "\n | <a  class='text12' valign='bottom' href='javascript:link_click({$nextpageno});' title='Next'><img src='../images/next{$imgno}.gif' border=0>{$nextchar}</a>";
         if ($topage<$totalpages)
            $linkstr.=   "\n | <a  class='text12' href='javascript:link_click({$totalpages});' title='Last'><img src='../images/last{$imgno}.gif' border=0>{$nextchar}{$nextchar}</a>";   
             
      }
       
      return $linkstr;
   }

   $query="SELECT * FROM YOU TABLE WHERE YOUR CONDTIONS ";
   
   $reccount=execute query ("SELECT COUNT(*) FROM (".$query.") A ",$connection); // to find total no of records
   
   $rowperpage=10;
   
   $totalpages=ceil($reccount/$rowperpage);
   
   if (!isvalueset($_POST['page']))
      $_POST['page']=1;
       
   $fromrecord=((($_POST['page']-1)*$rowperpage)+1);
   $torecord=$_POST['page']*$rowperpage;
   
   $fromrecord=$fromrecord-1;
   $torecord=$rowperpage;


   $query=$query."  LIMIT {$fromrecord},{$torecord} ";


   //execute your query here;
   
?>
<html>
<head>
<script language='javscript'>
   function link_click(pageno)
   {
       document.frmsearch.target='_self';
       document.frmsearch.page.value=pageno;
      document.frmsearch.submit();
   }
</script>
</head>
<body>
<?php   
   print "\n<table width=100% border=1 cellspacing=0 cellpadding=0>";
   //show your records here
   print "\n</table>";   


   //display page navigaation
   print "\n<table width=100% border=1 cellspacing=0 cellpadding=0>";
   print "\n<tr>";
   

   if ($reccount>0)
   {
      print "\n<td align=left width=25%><b>Total Record(s): {$reccount}</b></td>";
      if ($showdesign)
      {       
         print "\n<td align=center width=50%>".linkstr($reccount,$rowperpage,$totalpages)." &nbsp;</td>";
         print "\n<td align=right width=25%><b>Page {$_POST['page']} of {$totalpages}</b> &nbsp;</td>";
      }
   }
   else
   {
      print "\n<td align=center width=100%><b><br>No Record found.<br></b></td>";
   }
   print "\n</tr>";
   print "\n</table>";   
   
?>
<form name=frmsearch id=frmsearch method=post>
<input  type=hidden name=page id=page value=1>
</form>
<body>
</html>
 



If you struck somewhere then do let me know.
I will explain any portion that you are not able to get.
gcaughill
Hello,

ammonkc wrote:
Take a look at these pager classes.

http://www.phpclasses.org/browse/package/3352.html

http://phpclasses.pub.ayion.com/browse/package/92.html

http://www.phpclasses.org/trackback/browse/package/3713.html

From the looks of the code above, I think it would be a lot simpler for you to use one of these free packages. It will probably work a lot better also. I've written my own pagination scripts before, and its a lot of work for something so simple.


It is noble you are creating your own pagination class to learn. But once you do, you might want to invest in one of the pagination classes ammonkc listed above.
pashmina
or you could use this paginating class from nettuts at http://net.tutsplus.com/tutorials/php/how-to-paginate-data-with-php/
its very helpful, once you know how to configure it.
jinger89
ugh, i wish i had the patience to sit through and read the code to figure out what it does. writing code = fine. reading code = aarrgghhh.
Related topics
Clock-javascript
PHP DB Error: syntax error
+++ 535 FUNNY VIDEOS +++
Managing php pages
Search !!!
Dotclear
help with pagination problem
Pagination with .html extension
PHP Pagination?
Very basic pagination
Need help - guestbook pagination like [1] [2] [3] etc.
PHP Pagination and Tables
Pagination of data from an UNION query
Pagination for the Frihost Blogs index
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.