Page 1 of 1

Pagination

Posted: Mon Jun 21, 2004 7:13 am
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?

Posted: Mon Jun 21, 2004 9:32 am
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 );

Posted: Mon Jun 21, 2004 10:04 am
by Dingbats
Nope, doesn't work. I get four pages when I do like that, it should only be three.

Posted: Mon Jun 21, 2004 10:32 am
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;

Posted: Tue Jun 22, 2004 3:09 am
by Dingbats
Thanks, I'll try. The only difference was the first row... :?

Posted: Tue Jun 22, 2004 3:12 am
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

Posted: Tue Jun 22, 2004 4:38 am
by Dingbats
Sorry, doesn't work. I get 1.3333333 pages (1 shows up). :(

Posted: Tue Jun 22, 2004 4:55 am
by redmonkey
What is the value of $totalrows when echoed?