Prev/Next buttons/Paging with Many Query Results????????

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
FidoDido
Forum Newbie
Posts: 2
Joined: Tue Oct 29, 2002 3:36 am

Prev/Next buttons/Paging with Many Query Results????????

Post by FidoDido »

Help with this code...!!!!
But when i click the next link, it did not show the pages 11-19 and so on.....
I gonna have a query results number about 10000 records and might increase.
I intend to make 10 data per page, with 10 stage of pages, with the prev/next button as well?

Can anyone help me out with this code???
Thanx in advance!!!


<?

$connection = mysql_connect("host", "user", "password");
mysql_select_db("felix5");

$limit=10;
$Prev = "Previous";
$Next = "Next";

//$numresults=mysql_query("SELECT * FROM artist WHERE keterangan='$id' ORDER BY id ASC");
$numresults=mysql_query("SELECT id,keterangan FROM artist ORDER BY id ASC");
$numrows=mysql_num_rows($numresults);

if (empty($offset)) {
$offset=0;
}

echo "<table width=100% bgcolor=#FFFFFF border=0 cellpadding=2 cellspacing=1>";
$sql = "SELECT id,keterangan FROM artist ORDER BY id ASC limit $offset,$limit ";
$result = mysql_query($sql,$connection) or die("Could not execute query.");

while ($row = mysql_fetch_array($result)) {
echo "
<tr bgcolor=#C0C0C0>
<td align=center><font face=tahoma size=1 color=#000000 size=1><b>$row[FieldName]</b></font></td>
<td><font face=tahoma size=1 color=#000000 size=1><b>$row[FieldName]</b></font></td>
</tr>
";
}
mysql_free_result($result);

echo "</table>";

$pages=intval($numrows/$limit);

$pages1=intval($numrows/$limit);
if ($pages1 > 10) {
$pages=10;
}
else {
$pages=$pages1;
}

if ($numrows%$limit) {
$pages ;
}

if ($offset>1) {
$prevoffset=$offset-$limit;
print "<a href=\"$PHP_SELF?offset=$prevoffset\">$Prev</a> \n";
}

for ($i=1;$i<=$pages;$i ) {
$newoffset=$limit*($i-1);
print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n";
}

if ($numrows>($offset $limit)) {
$nextoffset=$offset $limit;
print "<a href=\"$PHP_SELF?offset=$nextoffset\">$Next</a><p>\n";
}
?>
:roll: :roll:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Where do variables like $offset come from? If it's coming from the query string have you tried:

Code: Select all

$offset = (!empty($_GET&#1111;'offset'])) ? $_GET&#1111;'offset'] : 0;
instead of

Code: Select all

if (empty($offset)) { 
$offset=0; 
}
(Try $HTTP_GET_VARS instead of $_GET for PHP <= version 4.0.6)

Mac
FidoDido
Forum Newbie
Posts: 2
Joined: Tue Oct 29, 2002 3:36 am

Paging with the prev/next button problem?

Post by FidoDido »

Oh I see, thanx!! :)
I am kind of new to PHP, so still need lots of practice!!
I am trying to make this paging with the prev/next button with many query results, 10000 ......
For this code, I can only view 1-10 with the prev/next button, it can view the continuous results by clicking the prev/next button, but the 11-20, 21-30 and so on did not show up!!!

Can you help me with the problem??I still could not understand the concepts!!! :(

Thanx!!!!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I am assuming that you end up with links that look something like http://www.mysite.com/mypage.php?offset=10 - is this right, if not where does $offset come from?

One thing that may make things simpler for you is to put the following line at the top of your code:

Code: Select all

extract($_GET);
What is apparently happening is that no value is being passed for $offset that means that here:

Code: Select all

if (empty($offset)) {  
$offset=0;  
}
$offset keeps getting set to 0 so you always end up with records 1-10.

Mac
smesquita
Forum Newbie
Posts: 7
Joined: Wed Oct 23, 2002 5:53 am

Post by smesquita »

Hi,

With this code:

Code: Select all

$numresults=mysql_query("SELECT id,keterangan FROM artist ORDER BY id ASC"); 
$numrows=mysql_num_rows($numresults);
you are selecting all the record from the database just for calculating the numrows, right? Try something like this:

Code: Select all

$numresults=mysql_query("SELECT count(id) FROM artist"); 
$data=mysql_fetch_array($numresults);
$numrows=$data&#1111;0];
So, if your select returned 10000 records, now only returns 1.

Also,

Code: Select all

$pages=intval($numrows/$limit); 

$pages1=intval($numrows/$limit); 
if ($pages1 &gt; 10) { 
$pages=10; 
} 
else { 
$pages=$pages1; 
}
you can turn to this:

Code: Select all

$pages=intval($numrows/$limit); 

if ($pages &gt; 10) { 
$pages=10; 
}
Post Reply