...

View Full Version : anyone has an example of pagination?



che_anj
08-29-2007, 03:16 PM
Hello,

Anyone has example of pagination?

I have made but its very basic procedural.. Id like to make it functional

this is my code .. accept critics.. ..


if(isset($_GET['start']))
{
$start=$_GET['start'];
$limit=$_GET['limit'];
}
else
{
$start=0;
$limit=15;
}


$query="SELECT * FROM users LIMIT $start, $limit";
$queryall="SELECT * FROM users";
$result = dbquery($link,$query);
$resultall=dbquery($link,$queryall);
$numrows=mysql_num_rows($resultall);
if(mysql_num_rows($result) < 1 )
{
echo 'no results found';
}
else
{

echo $num = $row[0];
while($row=mysql_fetch_array($result))
{
<tr>

<td><? echo $row['username'] ?> </td>
<td><? echo $row[firstname] ?></td>
<td><? php echo $row[lastname] ?></td>
<td><?php echo $row[email] ?></td>

</tr>
<?php
}
<tr><td colspan="6" align="center" height="20" class="table-list-title"><b>Result Page &nbsp;</b>
<?
$numofpages=$numrows/$limit;
if(($numrows % $limit)!=0)
$numofpages=$numofpages+1;
for($i=1,$start=0;$i<=$numofpages;$i++)
{
echo '<span class="char"><A href="'; echo $_SERVER["PHP_SELF"]; echo '?click=userlist.php&start='.$start.'&limit='.$limit.'">'.$i.' |&nbsp;</a></span>';
$start=$start+$limit;
}
?>

</td></tr>
<?php
}
?>



This code runs but, if you have any suggestions to make it more comprehensive, its really a big help.. thanks..

alex375
08-29-2007, 04:56 PM
For a while i also looked for some solution how the split the query results into a number of pages (i needed it to show my search results) but i didn't find any better solution then with LIMIT XX, XY;

And think think this solution is probably not so bad as you think, because every mysql query in cached on the server, so if you are on the second page, it schould not take so long for calculation results as on the first page.

che_anj
08-29-2007, 05:04 PM
that code display by number, id like to have pagination that displays Previous and Next only...

alex375
08-29-2007, 05:24 PM
Look, here is a nice example how to do it:
http://www.php-mysql-tutorial.com/php-mysql-paging.php

Fumigator
08-29-2007, 07:06 PM
Full blown pagination class, highly flexible:

http://www.firepages.com.au

rafiki
08-29-2007, 07:58 PM
Full blown pagination class, highly flexible:

http://www.firepages.com.au

also found here
http://www.codingforums.com/showthread.php?t=74614

moos3
08-29-2007, 09:25 PM
heres my two cents on simple pagination



if(!isset($_GET['page'])) {
$page = 1;
}
else {
$page = $_GET['page'];
$var = 4;
}

$max_news = 10; // How many results per page
$start = ($page * $max_news) - $max_news;
$i = 0;
$query = mysql_query("SELECT * FROM gmap ORDER BY id DESC LIMIT $start, $max_news");
while($row = mysql_fetch_array($query))
{
if($i % 2 == 0){
echo '<tr><td class="alt">'.$row['commentUsr'].'</td><td class="alt">'.$row['town'].','.$row['state'].'</td><td class="alt"><a id="mytable" class="link" href="map?id='.$row['id'].'">Veiw on Map</a></td></tr>';
}
else{
echo '<tr><td>'.$row['commentUsr'].'</td><td>'.$row['town'].','.$row['state'].'</td><td><a id="mytable" class="link" href="map?id='.$row['id'].'">Veiw on Map</a></td></tr>';
}
$i++;
}
echo "</table>";
$sql = mysql_query("SELECT * FROM gmap");
$result = mysql_num_rows($sql);

$total_pages = ceil($result / $max_news);
echo "<div class='pages'>";
if($page > 1)
{
$previous = $page - 1;
echo '<a href="?page=' . $previous . '" class="nextprev">Previous</a>';
}
else{
echo "<span span class=\"nextprev\">&laquo; Previous</span>";
}

for($i = 1; $i <= $total_pages; $i++){
if($i == $page){
echo "<span class=\"current\">".$i."</span>";
}
else{
echo '<a href="?page=' . $i . '">' . $i . '</a> ';
}
}

if($page < $total_pages){
$next = $page + 1;
echo "<a href='?page=" . $next . "/' class=\"nextprev\">Next &raquo;</a>";
}
else{
echo "<span class=\"nextprev\">Next &raquo;</span>";
}
echo "</div>";

you can see it live at http://labs.guthnur.net/gmapAPI/map.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum