Forum Moderators: coopster

Message Too Old, No Replies

Tutorial sytem pagination doesn't work.

         

celox

11:12 pm on May 9, 2006 (gmt 0)

10+ Year Member



Hi there!

I've been coming here alot when I need some things figured out, this is actually the first time I can't get it to work with all the previous posts and google searches, so here's my first post.

I have a tutorial system made from a tutorial (the site i got it from is down). Everything works fine except for the pagination, when you go to this link:

It nicely shows the newest 10 tutorials, but when you click on page 2 it doesn't show anything (it should give you 5 more results), and if you go back to page 1 from page 2 it's the same.

I've been frying my brains out with this one, also because I'm VERY new to PHP and MYSQL.

Here's my full source of category.php, this is the default code from the tutorial:

<?php
include ("dbconnect.php");
$id = $_GET['id'];

if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
$max_results = 10;
$from = (($page * $max_results) - $max_results);
?>
<h1>Newest Tutorials</h1>
<?php
$query = "SELECT * FROM `tutorials` WHERE category = '$id' ORDER BY id DESC LIMIT 0, 10";
$result = mysql_query($query)
or die("Error in query: $query . " . mysql_error());

if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_row($result))
{

$query1 = "SELECT * FROM category WHERE id = '".$row[4]."'";
$result1 = mysql_query($query1)
or die ("Error in query: $query1. " . mysql_error());
$row2 = mysql_fetch_object($result1);
$category = $row2->name;
?>
<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<td width="40"><a href="tutorial_viewtut.php?id=<?php echo $row[0];?>"><img src="../images/avatars/<?php echo $row[3];?>" alt="<?php echo $row[1];?>" height="40" width="40" border="0"></a></td>
<td valign="top" width="100%"><h2><a href="tutorial_viewtut.php?id=<?php echo $row[0];?>"><?php echo $row[1];?></a></h2><b>Category: </b><?php echo $category;?></td>
</tr>
<tr>
<td colspan="2"><?php echo $row[6];?></td>
</tr>
</table>
<hr>
<?php
}
}
?>
<?php
echo '<center>';

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tutorials WHERE category = '$id'"),0);

$total_pages = ceil($total_results/$max_results);
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo'['.$i.']';
}else{
echo "<a href='?page=$i'>$i</a> ";
} }
echo '<br>Page '.$page.' of '.$total_pages;
echo '</span>';

mysql_close($connection);
?>

Maybe someone could have a look at this, I'd really aprreciate it.

Thanks in advance!

[edited by: jatar_k at 11:31 pm (utc) on May 9, 2006]
[edit reason] no urls thanks [/edit]

scriptmasterdel

3:13 pm on May 10, 2006 (gmt 0)

10+ Year Member



here is your problem!

echo "<a href='?page=$i'>$i</a> ";

needs to be ....

echo "<a href='?id=$id&page=$i'>$i</a> ";

as you are not carrying the tutorial "id" to the next page the tutorial gets lost

also ....

in your select statement of the tutorials you need to set the start number and the limit number

$query = "SELECT * FROM `tutorials` WHERE category = '$id' ORDER BY id DESC LIMIT $from, $max_results";

Hope i have helped ;)

celox

5:52 pm on May 10, 2006 (gmt 0)

10+ Year Member



Thanks it works now :)

scriptmasterdel

8:37 am on May 11, 2006 (gmt 0)

10+ Year Member



No problem =)