News:

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

Main Menu

How to display total page views

Started by spiros, January 03, 2020, 02:35:06 PM

Previous topic - Next topic

spiros

I have the code below in BoardIndex.template.php and I want to add an option to display total page views.

I tried both the ones below, but none worked:

$context['common_stats']['num_hits']
$context[num_hits']['hits']


Here is full code:

// Show some statistics if stat info is off.
if (!$settings['show_stats_index'])
echo '
<div id="index_common_stats" class="taphoNone">
<div class="layr">
<div class="blck"><i class="fas fa-user-friends"></i><strong class="tppi">', $context['common_stats']['total_members'], '</strong><span class="phoneNone">', $txt['members'], '</span></div>
</div>
<div class="layr">
<div class="blck"><i class="fas fa-comments"></i><strong class="tppi">', $context['common_stats']['total_posts'], '</strong><span class="phoneNone">', $txt['posts_made'], '</span></div>
</div>
<div class="layr">
<div class="blck"><i class="fas fa-copy"></i><strong class="tppi">', $context['common_stats']['total_topics'], '</strong><span class="phoneNone">', $txt['topics'], '</span></div>
</div>
', ($settings['show_latest_member'] ? '
<div class="layr phoneNone">
<div class="blck"><i class="fas fa-user-plus"></i><strong class="tppi">' . $context['common_stats']['latest_member']['link'] . '</strong>' . $txt['latest_member'] . '</div>
</div>' : '') , '
</div>';

Pipke

Quote from: spiros on January 03, 2020, 02:35:06 PM
I have the code below in BoardIndex.template.php and I want to add an option to display total page views.

I tried both the ones below, but none worked:

$context['common_stats']['num_hits']
$context[num_hits']['hits']


The vars you tryed not defined within standard smf 2.0.* as far as i know, you need a mod or costum script to achieve what you want, google will be your friend for that.

"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! ☕

spiros

I can see in Stats.php

$context['num_hits'] = comma_format($row['hits'], 0);

// This would be the amount of time the forum has been up... in days...
$total_days_up = ceil((time() - strtotime($row['date'])) / (60 * 60 * 24));

$context['average_posts'] = comma_format(round($row['posts'] / $total_days_up, 2));
$context['average_topics'] = comma_format(round($row['topics'] / $total_days_up, 2));
$context['average_members'] = comma_format(round($row['registers'] / $total_days_up, 2));
$context['average_online'] = comma_format(round($row['most_on'] / $total_days_up, 2));
$context['average_hits'] = comma_format(round($row['hits'] / $total_days_up, 2));

$context['num_hits'] = comma_format($row['hits'], 0);


And in Stats.template.php

if (!empty($modSettings['hitStats']))
echo '
</tr><tr>
<td nowrap="nowrap">', $txt['num_hits'], ':</td>
<td align="right">', $context['num_hits'], '</td>';

Antes

num_views is not defined in boardIndex but in other sections. Your best bet to calculate it in custom query and push to _boards table as something like total_views, what you want is easy in MessageIndex since its already there.

Antes

Try this dirty solution,

Open Subs-BoardIndex.php
Code (Find) Select
b.id_board, b.name AS board_name, b.description,
Code (Replace) Select
b.id_board, b.name AS board_name, b.description,
tpc.id_board AS tpid_board, tpc.num_views,


Code (Find) Select
// Run through the categories and boards (or only boards)....
while ($row_board = $smcFunc['db_fetch_assoc']($result_boards))
{

Code (Replace) Select
for ($i = 1; $i < 100; $i++) {
$context['total_views_' . $i] = 0;
}
// Run through the categories and boards (or only boards)....
while ($row_board = $smcFunc['db_fetch_assoc']($result_boards))
{
if ($row_board['id_board'] == $row_board['tpid_board']) {
$context['total_views_' . $row_board['id_board']] += $row_board['num_views'];
}


Now you can use $context['total_views_' . $board['id']] under category section in BoardIndex.template.php, I must admit not the best solution but this is what I came up within 10-15mins.

spiros


Advertisement: