Customizing SMF > SMF Coding Discussion

Simple code help please - Total posts into a php variable

(1/2) > >>

Chas Large:
I'm hoping someone can give me a simple bit of php code, please.

I want quite simply to display the difference between a fixed number and the total number of posts. eg., 1000 - 800 = 200

I have this code

--- Code: ---// test addition code
\$first_number = 10;
\$second_number = 20;
\$sum_total = \$first_number + \$first_number;

\$direct_text = 'The two variables added together = ';

print (\$direct_text . \$sum_total);
--- End code ---

which will display...

"The two variables added together = 10 "

Yes, I know it's basic, very basic, I'm still learning ;)

Anyway, what I want to achieve is something like this.

--- Code: ---// test subtraction code
\$first_number = 1000;

\$sum_total = \$first_number - \$context['num_posts'];

\$direct_text = 'The number of posts to go to reach the target is = ';

print (\$direct_text . \$sum_total);
--- End code ---

So if the number of posts = 800 the display will show

"The number of posts to go to reach the target is = 200"

My problem is how to SIMPLY get the total number of posts into a variable so I can do the subtraction. I know I can use other methods to display the result but for now I'm staying low level to get the result I need.

Can it be done without complex functions etc?

K@:
'allo, Chas!

Something like...

--- Code: ---\$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum(\$a) . "\n";

\$b = array("a"=>1.2, "b"=>2.3, "c"=>3.4);
echo "sum(b) = " . array_sum(\$b) . "\n";
--- End code ---

perhaps?

Arantor Beeblebrox the First:
It sort of depends where you're doing this.

Either way, the total post count for the forum is available from \$modSettings['totalMessages'], but if you're doing it outside of SMF you need to have loaded SSI.php first, and if you're inside SMF at the time, make sure global \$modSettings; has been called first.

Chas Large:

--- Quote from: K@ on May 28, 2012, 04:23:45 PM ---'allo, Chas!

Something like...

--- Code: ---\$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum(\$a) . "\n";

\$b = array("a"=>1.2, "b"=>2.3, "c"=>3.4);
echo "sum(b) = " . array_sum(\$b) . "\n";
--- End code ---

perhaps?

--- End quote ---
Hallo matey,
Thanks for that but what I need is the ability to look up the total posts available in the forum, like from the stats page.

Chas Large:

--- Quote from: Arantor on May 28, 2012, 04:25:43 PM ---It sort of depends where you're doing this.

Either way, the total post count for the forum is available from \$modSettings['totalMessages'], but if you're doing it outside of SMF you need to have loaded SSI.php first, and if you're inside SMF at the time, make sure global \$modSettings; has been called first.

--- End quote ---
Ah yes, should have made that clearer.
It's within a SimplePortal php block, so just putting php code in it works.

Using the variable \$modSettings['totalMessages'] is exactly what I want to do but I'm not knowledgeable enough (yet) as to how I get access to it. I really am new to using SMF variables, can you please explain simply to me how to use the global \$modSettings; so I can get at the contents of \$modSettings['totalMessages']

I did try lots of searches before I posted this topic, honest.

Thanks.