1 (edited by Supercharged 2004-01-17 14:42)

Topic: Number of posts on frontpage?

Could someone please tell me how I can show the number of posts for a specified topic on my frontpage?
The frontpage is in the root directory and the forum is in a seperate directory '/forum/'...

Example: Lets say this topic has 4 posts total. I want my frontpage to show me automatically how many posts this topic has, like this: (Topic has 4 Posts)

What code do I need to use? I tried using the extern.php file, but I'm not too experienced with php so I don't know exactly how to get this information...

Thanx in advance!
Supercharged.

Re: Number of posts on frontpage?

Here's an example of how you could do it:

<?php

$topic_id = 3160;

$pun_root = './forums/';
require $pun_root.'include/common.php';

$result = $db->query('SELECT num_replies+1 FROM '.$db->prefix.'topics WHERE id='.$topic_id) or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());
$num_posts = intval($db->result($result, 0));

exit('Topic has '.$num_posts.' posts.');

?>

Just replace the value 3160 with the ID for the topic.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

3 (edited by Supercharged 2004-01-17 19:06)

Re: Number of posts on frontpage?

It works, but I get this error:


Warning: Cannot modify header information - headers already sent by (output started at ROOT/index.php:10) in ROOT/forum/include/functions.php on line 82
Topic has # posts.

Any idea on what this could be?

Re: Number of posts on frontpage?

Do this. Include the following piece of code at the very top of your page (before <html> and all that).

<?php

$topic_id = 3160;

$pun_root = './forums/';
require $pun_root.'include/common.php';

$result = $db->query('SELECT num_replies+1 FROM '.$db->prefix.'topics WHERE id='.$topic_id) or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());
$num_posts = intval($db->result($result, 0));

?>

Then, when you want to display the text, just include this code:

<?php echo('Topic has '.$num_posts.' posts.'); ?>
"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: Number of posts on frontpage?

This works perfectly! smile

Is there any way of getting the number of posts of several different topics on the same page?

Thanx in advance!

Re: Number of posts on frontpage?

Yes. Change

$topic_id = 3160;

to

$topic_id = '3160, 1222, 1446';

and

$result = $db->query('SELECT num_replies+1 FROM '.$db->prefix.'topics WHERE id='.$topic_id) or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());

to

$result = $db->query('SELECT num_replies+1 FROM '.$db->prefix.'topics WHERE id IN('.$topic_id.')') or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());

Then just edit $topic_id. It's a comma-separeted list of topic IDs.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

7 (edited by Supercharged 2004-01-18 14:17)

Re: Number of posts on frontpage?

That's not completely what I meant.

I want to display the number of posts in the different topics like this:
Topic number 3160 has ... posts
Topic number 3170 has ... posts
and so on...

Is there a way of getting the $topic_id into

<?php echo('Topic has '.$num_posts.' posts.'); ?>

this line of code, so I only have to run

<?php $pun_root = './forums/';
require $pun_root.'include/common.php';

$result = $db->query('SELECT num_replies+1 FROM '.$db->prefix.'topics WHERE id='.$topic_id) or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());
$num_posts = intval($db->result($result, 0));?>

this code once at the top of the page and it will read the different topics?

Thanx in advance!

Re: Number of posts on frontpage?

Yes, just add the variable into the line like this:

<?php echo('Topic: '.$topic_id.' has '.$num_posts.' posts.'); ?>

Re: Number of posts on frontpage?

Might I ask why you want to display the topic ID and the number of posts in that topic on the front page?

"Programming is like sex: one mistake and you have to support it for the rest of your life."

10

Re: Number of posts on frontpage?

Maybe he is making a news script or a portal thing?

---------> PLEASE REMEMBER I GOT THE FIRST EVER POST OF PUNBB 1.1! <---------
---------> PLEASE REMEMBER I GOT THE FIRST EVER POST OF PUNBB 1.1! <---------