Need help with pagination

Hello everyone! I’ve been trying to add pagination to my forum for some days but without any success… So can someone help me? The database table is called topics and here’s my list_topics.php :

[php]<?php
//This page let display the list of topics of a category
include(‘config.php’);
if(isset($_GET[‘parent’]) and ($_GET[“page”])) { $page = $_GET[“page”]; } else { $page=1; };
{
$start_from = ($page-1) * 20;
$id = intval($_GET[‘parent’]);
$dn1 = mysql_fetch_array(mysql_query(‘select count(c.id) as nb1, c.name,count(t.id) as topics from categories as c left join topics as t on t.parent="’.$id.’" where c.id="’.$id.’" group by c.id’));
if($dn1[‘nb1’]>0)
{
?>

<?php echo htmlentities($dn1['name'], ENT_QUOTES, 'UTF-8'); ?> - Forum
Forum
<?php if(isset($_SESSION['username'])) { $nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="'.$_SESSION['userid'].'" and user1read="no") or (user2="'.$_SESSION['userid'].'" and user2read="no")) and id2="1"')); $nb_new_pm = $nb_new_pm['nb_new_pm']; ?>
<?php } else { ?>
<?php } if(isset($_SESSION['username'])) { ?> New Topic <?php } $dn2 = mysql_query('select t.id, t.title, t.authorid, u.username as author, count(r.id) as replies from topics as t left join topics as r on r.parent="'.$id.'" and r.id=t.id and r.id2!=1 left join account as u on u.id=t.authorid where t.parent="'.$id.'" and t.id2=1 group by t.id order by t.timestamp2 desc limit '.$start_from.', 20'); if(mysql_num_rows($dn2)>0) { ?> <?php if(isset($_SESSION['username']) and $_SESSION['username']==$admin) { ?> <?php } ?> <?php while($dnn2 = mysql_fetch_array($dn2)) { ?> <?php if(isset($_SESSION['username']) and $_SESSION['username']==$admin) { ?> <?php } ?> <?php } ?>
Topic Author RepliesAction
<?php echo htmlentities($dnn2['title'], ENT_QUOTES, 'UTF-8'); ?> <?php echo htmlentities($dnn2['author'], ENT_QUOTES, 'UTF-8'); ?> <?php echo $dnn2['replies']; ?>Delete
<?php } else { ?>
This category has no topic.
<?php } if(isset($_SESSION['username'])) { ?> New Topic <?php } else { ?>

You need to be logged in to browse the forum!

<?php } ?>
<?php } else { echo '

This category doesn\'t exist.

'; } } ?>[/php]

So basically it shows only 20 topics but I couldn’t managed to add the pages and i also get this error:
Notice: Undefined index: page in C:\xampp\htdocs\Forum\list_topics.php on line 4

you have forgotten “isset” before ($_GET[“page”])

[php]$page = isset($_GET[‘parent’]) && isset($_GET[“page”]) ? $_GET[“page”] : 1;[/php]


Parse error: syntax error, unexpected ‘?’ in C:\xampp\htdocs\Forum\list_topics.php on line 4

Firstly, id suggest you work on your formatting as its quite illegable and thats quite hard for debugging.

What i would do is do a barebones rebuild - take all the content out into another file then run something like the following… and im a bit confused with you… AND syntax???
[php]
if(isset($_GET[‘parent’]) && isset($_GET[‘page’])){
$page = $_GET[‘page’];
} else {
$page = 1;
}
[/php]

is there any particular reason you like to jump in and out of php so much?
All you’re doing is tying yourself up in knots! - No, seriously, this is bad :-[

I mean it’s rather pointless to leave PHP just to do this:
[php]if(isset($_SESSION[‘username’]) and $_SESSION[‘username’]==$admin)
{
?>

Action[/php]
Just stay in PHP and do:
[php]if(isset($_SESSION[‘username’]) and $_SESSION[‘username’]==$admin)
{
echo ‘Action’;[/php]

And you should move the php to the top of the file - or even better, another file completely!

Red :wink:

Which PHP version are you using…? The ternary operator (short if/else) should definitly work… ^^

I disagree with your first point. If you have to mix PHP and HTML then I definitly prefer to jump out of PHP to do HTML.

The real problem is the second point you make, that he haven’t separated the logic and the view properly. So if you finish all the php stuff up front then this is perfectly valid (in my book)

[php]<?php

// php stuff up here

?>

Users

<?php foreach ($users as $user) { ?>

<?= $user->name ?>

<?= $user->description ?>
<?php } ?>
[/php]

The version of my php is - 5.5.15. Without the ternary operator I don’t get this undefined index problem so thanks a lot guys.

I agree with you - to a certain extent.
I would leave PHP if i had to do several lines of HTML. And the example you wrote below is pretty much what i’d do, but i wouldn’t leave just to do a single line with a few chars.

[php]

<?php foreach($variable as $var) { ?>
<a href="<?php echo $var; ?>" title="a link">Link</a>
<?php } ?>

[/php]
Seems a bit of a trek to me - I know we can use shorthand <?= but lots i see still use the long version.

Either of these seems much better on the eye (IMO) - personally i use the latter.
[php]
foreach($variable as $var) {
echo ‘Link’;
}
foreach($variable as $var) {
printf(‘Link’, $var);
}
[/php]

Red :wink:

But that is a situation you will never have if you separate your logic/views :wink:

This is true my friend and a very good point for those that read this thread afterwards :slight_smile:

Personally i prefer to stay as much in php as possible.

Sponsor our Newsletter | Privacy Policy | Terms of Service