...

View Full Version : Displaying database data over X No. of pages (PHP help)



XtremeGamer99
10-20-2004, 02:41 AM
I know I am going to confuse all of you now. I'll try to explain the best I can.

I am thinking about making a message baord, and I think I have the knowledge now in order to do it, except this one tiny thing: I don't know how to code a "Page" script. Meaning, if there are a certain amount of topics on a page, lets say 50, and I want to limit it to 10 topics a page, and have something like "Page: 1 | 2 | 3 | 4 | 5 | 6" at the bottom displaying the number of pages. Now, I already know it isn't the MySQL Query LIMIT, because that cuts it all off, no matter if it has more. What i want to do is display 10, then hit page "2" and it displays the next 10. I have tried several different things. I've tried the limit, I've tried looking at other message board sources (no, not the complex kind tlike phpBB. The simple kind), and just about everything. Right now, i have this code on my test script:



else if (isset($letter))
{
if ($letter == "number")
{
$sql="SELECT gameID, gamename, platform FROM games WHERE games.platform = 'gcn' AND games.gamename REGEXP '^[0-9]' ORDER BY gamename asc";
}
else
{
$sql="SELECT gameID, gamename, platform FROM games WHERE games.platform = 'gcn' AND games.gamename LIKE '".$letter."%' ORDER BY gamename asc";
}
if (!isset($page) || !is_numeric($page))
{
$page = 1;
}

$result=mysql_query($sql) or die ('Queryproblem');
$numofitems = mysql_num_rows($result);

$display_top_nav = 'true';

if ($numofitems > 2)
{
$prev = $page-1;
$next = $page+1;
$nop = ceil($numofitems/2);
echo '<b><i>';
echo $page > 1 ? "<a href=\"{$_SERVER['REQUEST_URI']}&page=1\">First Page</a> | " : '';
echo $page > 2 ? "<a href=\"{$_SERVER['REQUEST_URI']}&page={$prev}\">Previous Page</a> | " : '';
echo "Page $page of $nop";
echo $page < $nop-1 ? " | <a href=\"{$_SERVER['REQUEST_URI']}&page={$next}\">Next Page</a>" : '';
echo $page < $nop ? " | <a href=\"{$_SERVER['REQUEST_URI']}&page={$nop}\">Last Page</a>" : '';
echo '</i></b>';}

echo ('
<table width="100%">
<tr>
<td class="B"><font size="7">' . $database . '</font></td>
</tr>
<tr>
<td class="A">Games by Alpha: ' . $letter . '</td>
</tr>
');
$i = 0;
while ($row=mysql_fetch_assoc($result))
{
$class = $i&1 ? ' class="shade"' : '';
echo ('
<tr' . $class . '>
<td>
<b><a href="' . $_SERVER['PHP_SELF'] .'?game='. $row['gameID'] .'">'. $row['gamename'] .'</a></b></td></tr>
');
$i++;
}
if ($numofitems > 2)
{
$prev = $page-1;
$next = $page+1;
$nop = ceil($numofitems/2);
echo '<tr><td align="center">Jump to Page: ';

for ($i = 1; $i < $nop+1; $i++)
{
echo ${$page_var} != $i ? "<a href=\"{$_SERVER['REQUEST_URI']}&page={$i}\" class=\"menu\">$i</a>" : $i;
echo $i < $nop ? ' | ' : '';
}

echo '</td></tr>';
}



echo ('

</tr>
</table>
');
}

then at the top of the page:


$page=$_GET['page'];

Change in this code: Instead of having 50 items, and limiting it to 10, I have g6 items and limiting it to 2 (or tring to, at leat). So, some of it works, like the page navigation (the first, next, prev, and last). No matter what page link I click, it gives me the same amount of items on the page. Although the Next, Prev, First, and Last links at the top change (like they should) iI get the same number of items. i have also notices that the number of pages that show up is however many item there are in total divided by 2. the problem is is that it doesn't limit it. And I can't use the MySQL LIMIT Query. Can somebody please check the WHOLE code inch-by-inch (cause I have, and I can't find anything) and tell me what to add, subtract, change, anything to make this working. If you don't understand what I am tring to do, then please, fell free to tell me. thanks.

Kurashu
10-20-2004, 03:31 AM
Try using:

$page = ($page - 1) * 10;

And in your SQL:
LIMIT $page, 10

I also reccomend reading this tutorial for pagination:
Page Numbering With PHP And MySQL Results (PHPFreaks.com) (http://www.phpfreaks.com/tutorials/73/0.php)

I also uses this code for my pagination (I realise it is PsotGreSQL, but the PHP should work the same)



$limit = 20;
$maxpages = ceil($total / $limit);
$page = $_GET[page];
if(empty($page) || $page > $maxpages) {$page = 1;}
$offset = ($page - 1) * $limit;


In this case, $total is define by the num_rows function, and $offset would be the same as your $page.

trib4lmaniac
10-20-2004, 11:33 AM
http://www.google.com/search?q=php+pagination



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum