News:

Wondering if this will always be free?  See why free is better.

Main Menu

Re: Read XX Times No longer Shown on Top of Threads

Started by jsx, October 29, 2022, 07:38:13 AM

Previous topic - Next topic

jsx

I also like to have the number of thread views next to the thread name. Thanks @Arantor for providing the solution.

I noticed a bug. The thread has 8k views and I see this number of views on the board on the right where the number of views and replies are. But when I enter a thread, it does not show 8k views, but 8 views. Anyone know how to fix this?

I've split this off. Even though the problem is the same as in the original topic, it was getting difficult to follow along with so many replies. ~ Steve

jsx

#1
I repeat the question. Anyone understand why this counter doesn't show the correct number of views when the topic has more than 1000 views?

Arantor

Because it's actually broken in SMF under the hood.

The change I gave you just displays a value already calculated by SMF, you'll see that no actual new thing was created, it's *just* output a thing that's already there.

Underlying code in Display.php which is unchanged:
// Set the topic's information for the template.
$context['subject'] = $context['topicinfo']['subject'];
$context['num_views'] = comma_format($context['topicinfo']['num_views']);
$context['num_views_text'] = $context['num_views'] == 1 ? $txt['read_one_time'] : sprintf($txt['read_many_times'], $context['num_views']);

Well, the issue here is the content of $txt['read_many_times'].

Specifically:
$txt['read_many_times'] = 'Read %1$d times';
%1$d means a number - just digits. But by the time this has happened it's been shoved through comma_format so you get it with a comma attached so by *that* point, '1,000' gets shortened to '1'.

The fix, replace the %1$d with %1$s in your language strings (not just index.english.php but whichever language you're using)

jsx

@Arantor, thank you for your involvement to help with this topic.

Quote from: Arantor on March 25, 2023, 09:03:48 AMThe fix, replace the %1$d with %1$s in your language strings (not just index.english.php but whichever language you're using)

I replaced %1$d with %1$s and it still shows 1.

Arantor

Which language are you using and which file did you edit?

jsx


jsx

Arantor, but that doesn't work in index.english.php either.

Arantor

Well, you'll just have to wait until Saturday now because I'm on a work trip and I don't have my personal laptop handy.

Oldiesmann

The problem is we're comparing the number of views to 1 after we pass the value through comma_format. Because of the way the == operator works in PHP, the "$context['num_views'] == 1" comparison will evaluate to true when there's a comma in there.

To fix it, change the first $context['num_views'] in the $context['num_views_text'] line in Display.php to $context['topicinfo']['num_views'] instead.
Michael Eshom
Christian Metal Fans

jsx

@Oldiesmann do you mean this line in Display.template.php?

echo '
<p>', $context['num_views_text'], '</p>';

I entered this:

echo '
<p>', $context['topicinfo']['num_views'], '</p>';

And the counter now correctly shows that the topic has 1000 views, but the word 'Read' is not displayed, can it be fixed?

And should I implement this solution that Arantor gave me or not?

Oldiesmann

Implement the solution Arantor gave you plus the one I gave you. I meant Display.php, not Display.template.php.

You want to change this line:
$context['num_views_text'] = $context['num_views'] == 1 ? $txt['read_one_time'] : sprintf($txt['read_many_times'], $context['num_views']);
To this:
$context['num_views_text'] = $context['topicinfo']['num_views'] == 1 ? $txt['read_one_time'] : sprintf($txt['read_many_times'], $context['num_views']);
Michael Eshom
Christian Metal Fans

Arantor

I do find it funny that this is broken in core and should just have been the original one line fix to Display.template.php.

Advertisement: