Pagination

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Dingbats
Forum Commoner
Posts: 25
Joined: Fri Dec 05, 2003 10:53 am

Pagination

Post by Dingbats »

I'm making a forum for my site, and for the pagination at the page that shows the threads (forumview.php), I have followed this tutorial. But it doesn't work! Maybe it's because I use joins for selecting all the stuff I need, but I have no clue how I would solve the problem.
The table with the threads in is called f_threads, and the table with the posts in (including the first) is called f_posts.
This is the code for the pagination (tabs don't seem to work well):

Code: Select all

<?php
if(isset($_GET["page"])) //forumview.php?id=1&[b]page=1[/b], for ex.
{
$page = $_GET["page"];
$limit = 3; //A weird limit, just for testing
$lstart = $page * $limit - ($limit);
}
else
{
$page = 1;
$limit = 3;
$lstart = $page * $limit - ($limit);
}
	
$sql = "SELECT f_threads.*, f_posts.date, f_posts.thread FROM f_threads INNER JOIN f_posts ON f_posts.thread = f_threads.id AND f_threads.forum = ".$_GET['id']." ORDER BY date DESC LIMIT ".$lstart.", ".$limit; //Selecting everything I need to output later, isn't shown in this part of the page
$result = mysql_query("$sql");
$sqlc = "SELECT COUNT(*) FROM f_threads"; //FROM HERE
$resultc = mysql_query("$sqlc");
$totalrows = mysql_num_rows($resultc); //TO HERE is what I think messes things up
	
echo $totalrows."<table width='100%' class='sometables'><tr><td class='bsometds' align='center' colspan='5'>"; //Start the output table, ended later in another part of the page. I echoed $totalrows just to see what it is
	
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++) //This for loop prints the page numbers
{
if($i == $page)
{
echo "[".$i."]";
}
else
{
echo "[<a href='forumview.php?id=".$_GET['id']."&page=".$i."'>".$i."</a>]";
}
}
if($totalrows % $limit != 0) //This if prints the last number in some cases
{
if($i == $page)
{
echo "[".$i."]";
}
else
{
echo "[<a href='forumview.php?id=".$_GET['id']."&page=".$i."'>".$i."</a>]";
}
}
echo $numofpages."</td></tr>"; //I echoed $numofpages just to see what it is
?>
What is wrong with this? I have followed the tutorial perfectly well.
Can you help?
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Code: Select all

$totalrows = mysql_num_rows($resultc); //TO HERE is what I think messes things up
Yep that looks like the problem. Try...

Code: Select all

$totalrows = mysql_result( $resultc, 0, 0 );
Dingbats
Forum Commoner
Posts: 25
Joined: Fri Dec 05, 2003 10:53 am

Post by Dingbats »

Nope, doesn't work. I get four pages when I do like that, it should only be three.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

try this...

Code: Select all

$numofpages = ($totalrows % $limit) ? ($totalrows / $limit) + 1 : $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++) //This for loop prints the page numbers
{
if($i == $page)
{
echo "[".$i."]";
}
else
{
echo "[<a href='forumview.php?id=".$_GET['id']."&page=".$i."'>".$i."</a>]";
}
}
echo $numofpages."</td></tr>"; //I echoed $numofpages just to see what it is
That snippet above should replace yours from and including the line...

Code: Select all

$numofpages = $totalrows / $limit;
Dingbats
Forum Commoner
Posts: 25
Joined: Fri Dec 05, 2003 10:53 am

Post by Dingbats »

Thanks, I'll try. The only difference was the first row... :?
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

no, you had more unneeded code:
yours -

Code: Select all

$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++) //This for loop prints the page numbers
{
if($i == $page)
{
echo "[".$i."]";
}
else
{
echo "[<a href='forumview.php?id=".$_GET['id']."&page=".$i."'>".$i."</a>]";
}
}
if($totalrows % $limit != 0) //This if prints the last number in some cases
{
if($i == $page)
{
echo "[".$i."]";
}
else
{
echo "[<a href='forumview.php?id=".$_GET['id']."&page=".$i."'>".$i."</a>]";
}
}
echo $numofpages."</td></tr>";
modified -

Code: Select all

$numofpages = ($totalrows % $limit) ? ($totalrows / $limit) + 1 : $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++) //This for loop prints the page numbers
{
if($i == $page)
{
echo "[".$i."]";
}
else
{
echo "[<a href='forumview.php?id=".$_GET['id']."&page=".$i."'>".$i."</a>]";
}
}
echo $numofpages."</td></tr>"; //I echoed $numofpages just to see what it is
Dingbats
Forum Commoner
Posts: 25
Joined: Fri Dec 05, 2003 10:53 am

Post by Dingbats »

Sorry, doesn't work. I get 1.3333333 pages (1 shows up). :(
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

What is the value of $totalrows when echoed?
Post Reply