News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Get total number of boards as int $board_info

Started by DionK, April 05, 2015, 10:43:52 AM

Previous topic - Next topic

DionK

Hey there,

I've looked into global variables a bit, but what I'm trying to achieve doesn't seem to be working.
I'm trying to get the total number of boards as an integer, which I suspect can be accomplished by using the "$board_info" variable. The problem is I can't seem to find what goes in the variable brackets. (excuse my poor language)

I've tried things like:
$context[total_boards']
$context['common_stats']['total_boards']
$board_info['total_boards']
$board_info['num_boards']

I've added "$board_info" to the global variable list.
Any ideas?

Arantor

There is no variable for this, because it's not a number SMF ever needs outside the stats area so it never calculates it and stores it in a general variable.

Getting it is potentially not cheap depending on whether it's the real number of boards, or the number of boards the current user can see.

I'm curious to know what benefit this would have, though.

karlbenson

A simple query can get this information from the database.  It wouldn't be too expensive (unless you had thousands of boards).

Kindred

#3
As Arantor mentions...  It would not be too expensive...  UNLESS the requestor wants the count to check whether the user viewing the stat can see a board or not...

THEN it starts to become expensive as it checks the user versus permissions versus access...
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

DionK

Quote from: Arantor on April 05, 2015, 02:07:08 PM
Getting it is potentially not cheap depending on whether it's the real number of boards, or the number of boards the current user can see.
Quote from: Kindred on April 05, 2015, 04:58:05 PM
As Arantor mentions...  It would not be too expensive...  UNLESS the request or wants the count to check whether the user viewing the stat can see a board or not...

THEN it starts to become expensive as it checks the user versus permissions versus access...

Thanks for all your responses. At the moment I'm just messing around with things locally using xampp.
If the site were live however, the way I have it configured now, there is no registration, everyone can post as guests, and everyone can see the same amount of boards.

Quote from: karlbenson on April 05, 2015, 03:46:33 PM
A simple query can get this information from the database.  It wouldn't be too expensive (unless you had thousands of boards).

How would I go about doing this?

Thanks.

Arantor

@karl: It's not such a simple query as it used to be.

Still would like to know what the benefit of having this number on every single page would be, or whether you'd want it more specifically on a few pages, or even whether you want to know whether it's all boards or only the boards the current user can see (because this is actually important and relevant to writing the query)

DionK

Quote from: Arantor on April 06, 2015, 03:30:33 AM
Still would like to know what the benefit of having this number on every single page would be, or whether you'd want it more specifically on a few pages, or even whether you want to know whether it's all boards or only the boards the current user can see (because this is actually important and relevant to writing the query)
It won't be on every page, just 1: the index. At the top right of the page, I want it to say something like "x posts in x topics under x boards", just a neat little indicator. It would be every single board, as I said before, everyone would be a guest and would see the same amount of boards.

Arantor

As a general rule, users don't actually care how many boards there are, x posts in y topics by z members is usually far more representational of content rather than x posts in y topics in z boards, because it actually encourages you to pointlessly rearrange the number of boards you have just to make the stat look better (as opposed to writing more posts to make the more meaningful stats look better)

DionK

I totally disagree. It's not about people caring how many boards there are, it's about how I want my forum to look. There are no members as I said twice, so displaying that isn't an option. It's also not about showing off how many posts can be jammed into however many boards, I simply want to display the total amount of boards.

So, back to the request of my post. How can this be achieved? Code snippets would be appreciated.

Pipke

Quote from: DionK on April 06, 2015, 11:53:46 AM
I totally disagree. It's not about people caring how many boards there are, it's about how I want my forum to look. There are no members as I said twice, so displaying that isn't an option. It's also not about showing off how many posts can be jammed into however many boards, I simply want to display the total amount of boards.

So, back to the request of my post. How can this be achieved? Code snippets would be appreciated.

here you go...


global $context, $txt, $smcFunc;

// Statistics such as topics, posts
    $result = $smcFunc['db_query']('', '
        SELECT
            SUM(posts) AS posts, SUM(topics) AS topics
        FROM {db_prefix}log_activity',
        array(
        )
    );
    $row = $smcFunc['db_fetch_assoc']($result);
    $smcFunc['db_free_result']($result);

    // Statistics such as number of boards, categories, etc.
    $result = $smcFunc['db_query']('', '
        SELECT COUNT(*)
        FROM {db_prefix}boards AS b
        WHERE b.redirect = {string:blank_redirect}',
        array(
            'blank_redirect' => '',
        )
    );
    list ($context['num_boards']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);

    $result = $smcFunc['db_query']('', '
        SELECT COUNT(*)
        FROM {db_prefix}categories AS c',
        array(
        )
    );
    list ($context['num_categories']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);

    // Format the numbers nicely.
   
    $context['num_boards'] = comma_format($context['num_boards']);
    $context['num_categories'] = comma_format($context['num_categories']);   
    $context['num_posts'] = comma_format($modSettings['totalMessages']);
    $context['num_topics'] = comma_format($modSettings['totalTopics']);
   
echo'<dl class="stats" >
                           
                            <dt>', $txt['total_posts'], ':</dt>
                            <dd>', $context['num_posts'], '</dd>
                            <dt>', $txt['total_topics'], ':</dt>
                            <dd>', $context['num_topics'], '</dd>
                            <dt>', $txt['total_cats'], ':</dt>
                            <dd>', $context['num_categories'], '</dd>
                            <dt>', $txt['total_boards'], ':</dt>
                            <dd>', $context['num_boards'], '</dd>
                           
</dl>';
"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

Jade Elizabeth

Interesting concept, from what I understand you're not allowing members? I'd like to see your project when you're done, it sounds different :D.
Once proud Documentation Writer and Help Squad Leader | Check out my new adult coloring career: Color With Jade/Patreon.

DionK

#11
Quote from: Pipke on April 13, 2015, 02:01:01 PM
Quote from: DionK on April 06, 2015, 11:53:46 AM
I totally disagree. It's not about people caring how many boards there are, it's about how I want my forum to look. There are no members as I said twice, so displaying that isn't an option. It's also not about showing off how many posts can be jammed into however many boards, I simply want to display the total amount of boards.

So, back to the request of my post. How can this be achieved? Code snippets would be appreciated.

here you go...


global $context, $txt, $smcFunc;

// Statistics such as topics, posts
    $result = $smcFunc['db_query']('', '
        SELECT
            SUM(posts) AS posts, SUM(topics) AS topics
        FROM {db_prefix}log_activity',
        array(
        )
    );
    $row = $smcFunc['db_fetch_assoc']($result);
    $smcFunc['db_free_result']($result);

    // Statistics such as number of boards, categories, etc.
    $result = $smcFunc['db_query']('', '
        SELECT COUNT(*)
        FROM {db_prefix}boards AS b
        WHERE b.redirect = {string:blank_redirect}',
        array(
            'blank_redirect' => '',
        )
    );
    list ($context['num_boards']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);

    $result = $smcFunc['db_query']('', '
        SELECT COUNT(*)
        FROM {db_prefix}categories AS c',
        array(
        )
    );
    list ($context['num_categories']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);

    // Format the numbers nicely.
   
    $context['num_boards'] = comma_format($context['num_boards']);
    $context['num_categories'] = comma_format($context['num_categories']);   
    $context['num_posts'] = comma_format($modSettings['totalMessages']);
    $context['num_topics'] = comma_format($modSettings['totalTopics']);
   
echo'<dl class="stats" >
                           
                            <dt>', $txt['total_posts'], ':</dt>
                            <dd>', $context['num_posts'], '</dd>
                            <dt>', $txt['total_topics'], ':</dt>
                            <dd>', $context['num_topics'], '</dd>
                            <dt>', $txt['total_cats'], ':</dt>
                            <dd>', $context['num_categories'], '</dd>
                            <dt>', $txt['total_boards'], ':</dt>
                            <dd>', $context['num_boards'], '</dd>
                           
</dl>';


Thank you! I'll let you know how it goes.
EDIT: Works a charm, thanks again.

Quote from: Jade Elizabeth on April 14, 2015, 12:17:26 AM
Interesting concept, from what I understand you're not allowing members? I'd like to see your project when you're done, it sounds different :D.

That's right, a forum based entirely on anonymity. It's just an idea at the moment, but I'd be happy to share it with you once it's done :)

Advertisement: