Getting the total number of topics in specific boards

Started by spiros, June 05, 2013, 05:13:06 AM

Previous topic - Next topic

spiros

Is there a way to get this with SSI or something? I.e. a one-liner where one could add the desired boards and which would produce the total number of topics (a numeric value) contained in them.

Acans

Not be default unfortunately.

I was hoping you could specify a specific board or boards and only output the total count of those boards similar to ssi_boardNews but you can't :(.

ssi_boardStats

I've moved your topic to SMF Coding Discussion. Maybe one of the blokes who hang around in this board might be able to help :)
"The Book of Arantor, 17:3-5
  And I said unto him, thy database query shalt always be sent by the messenger of $smcFunc
  And $smcFunc shall protect you against injections and evil
  And so it came to pass that mysql_query was declared deprecated and even though he says he is not
  dead yet, the time was soon to come to pass when mysql_query shall be gone and no more

margarett

I *think* it's doable...

The total number of topics is obtained in Subs.php:

// Get the number of topics - a SUM is better for InnoDB tables.
// We also ignore the recycle bin here because there will probably be a bunch of one-post topics there.
$result = $smcFunc['db_query']('', '
SELECT SUM(num_topics + unapproved_topics) AS total_topics
FROM {db_prefix}boards' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
WHERE id_board != {int:recycle_board}' : ''),
array(
'recycle_board' => !empty($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0,
)

It should be possible to duplicate this function, changing the "WHERE" clause to fit your needs.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

spiros

Thanks, pardon my ignorance, any more specific instructions?

margarett

I have to do some tests here... I will let yo know the outcome.

Just to know: do you want to access that information via SSI or inside SMF?
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

margarett

Hi.

After some delay (big one) I managed to get something out of this :)
It's probably not the best way to do it, but it serves the purpose and was a nice way for me learn something. That said:

SSI.php, put the following function somewhere.

// Count number of posts in a board
function ssi_countTopicsInABoard($output_method = 'echo', $xxboard_id = 0)
{
global $db_prefix, $txt, $scripturl, $modSettings, $smcFunc;

if ($xxboard_id != 0)
{
$result = $smcFunc['db_query']('', '
SELECT SUM(num_topics + unapproved_topics) AS total_topics
FROM {db_prefix}boards
WHERE id_board = '.$xxboard_id
);
$row = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);
if ($row['total_topics'] === null)
$xxtotalTopics = 0;
else
$xxtotalTopics = $row['total_topics'];
}
else
$xxtotalTopics = 0;

if ($output_method != 'echo')
return $xxtotalTopics;

echo '
Total topics in board ', $xxboard_id,': ',$xxtotalTopics;
}


I created it before:

// Show some basic stats:  Total This: XXXX, etc.
function ssi_boardStats($output_method = 'echo')

But I think you can put it anywhere.

Then, on your page that is already using SSI, you can use it one of the following ways:

ssi_countTopicsInABoard('echo',2);

That will echo:
Quote
Total topics in board 2: 3
Or:

$NumPostsInABoard = ssi_countTopicsInABoard('',1);
echo 'Blablablabla!!! ', $NumPostsInABoard;

That will echo
Quote
Blablablabla!!! 6
(of course here you can echo whatever you want or use the result in any way you want)

Anyway, this was fun for me since I am just a curious. Not sure if it can be done in another way, though, but this works :P
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

thisisedy

I'm trying to display 'n topics in x board' on boardindex page ( info center ) . Would this ssi method work for me ? If not , the steps with the required modifications would be highly appreciated . ::)

margarett

It will not. You are not supposed to use SSI functions inside SMF...

To have it in BoardIndex should be easier because that info was already displayed in the boards list, so it would be just a matter of grabbing it when rendering the boards and then display it in info center
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Arantor

Quote from: thisisedy on December 04, 2013, 05:32:02 AM
I'm trying to display 'n topics in x board' on boardindex page ( info center ) . Would this ssi method work for me ? If not , the steps with the required modifications would be highly appreciated . ::)

You mean like it is here?

thisisedy

Quote from: margarett on December 04, 2013, 06:37:36 AM
It will not. You are not supposed to use SSI functions inside SMF...

To have it in BoardIndex should be easier because that info was already displayed in the boards list, so it would be just a matter of grabbing it when rendering the boards and then display it in info center

', $board['is_redirect'] ? '' : comma_format($board['topics']) . ' ' . $txt['board_topics'], '

How could i specify the id of that board which i need display ?

Quote from: Arantor Beeblebrox the First on December 04, 2013, 08:07:34 AM
You mean like it is here?
Just the string required to output a specific board total topics . I also found this , but don't know what to do with that function  :D

margarett

There are at least 3 ways to do this that I can think of. I think that the most safe one in in BoardIndex.php because it already filters the boards that the user can and cannot see and it shows the value wether or not the category is collapsed...
But it's also the least effective one, as we have to go through each category and board again, when that is being done already in the template...
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Arantor

I'm struggling to understand why you would want this, or indeed what you're trying to do and where you're trying to do it.

margarett

As I understand it, he just wants to show in the InfoCenter the number of posts in a specific board.

And now that I think about it, it's a lot easier than I was trying to do :P
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

thisisedy

Quote from: Arantor Beeblebrox the First on December 05, 2013, 10:38:20 AM
I'm struggling to understand why you would want this, or indeed what you're trying to do and where you're trying to do it.
I have a specific board , where user can create his topic with his site/service ( advertising stuff ) . And i want to highlight that total number on boardindex page , something like this : Our users have posted x sites .

Arantor

So what board id, and where on the board index page do you want to list this? Does this board have any sub-boards? Is it a sub-board of something else? Fortunately we know you're using 2.0, that's something.

thisisedy

#15
Oh , i've forgot about this topic . Sorries for not providing all the details .

This is only for stats purposes . I have 2 structure types :

board 1 >child board>topics
             >child board>topics
             >child board>topics

board 2 >redirects*
             >child board>topics

*the redirects should be counted as topics too

So the total number of topics in board 1 or board 2 , to be displayed near menu on index.template page.
Something like how the current topics are being displayed in stats page , but for a specific board ID .

$context['num_topics'] = comma_format($modSettings['totalTopics'])

Advertisement: