Page view counter, version 0.1b - small addition
If you want to display view counter inside infoline when viewing full article, install mod version 0.1 and add this code
6. function center() (snews.php)
- around line 531 find this snippet and add the blue part
foreach ($tag as $tag ) {
switch ($tag) {
case 'date': echo $a_date_format; break;
case ($tag == 'views'): echo ' '.$r['views'].' '.l('views').' '; break;
------------------
Version 0.1b issues:
Counter increment is visible only in the next roundtrip to the monitored page. The original article query starts somewhere around line 487 (function center() inside snews.php file) and its results are used throughout the program flow:
- displaying list of articles dependable on category/page
- displaying full article along with its comments
These results come unchanged via single
select * from article command and
the database update is outside that flowYou can see the issue in action at my site
www.ni5ni6.com; in the upper middle box there is a recent readings section with the view counter that remains unchanged (not incremented) when you click on readmore link.
Workaround:
- instead of using
$r['views'] result from the basic program flow (step 6), use this:
$view_count = retrieve('views', 'articles', 'id', $r['id']);
- note: step 4 displays view counter BEFORE database update action takes place
Changes:
step 5.
else if (substr($position, 0, 1) != '2' && empty($currentPage)) {
if ($_SESSION[db('website').'Logged_In'] != token()) {
$views = $r['views']; $views++;
$queryUpdate = "UPDATE ".db('prefix')."articles SET views = $views WHERE id = $r[id]";
mysql_query($queryUpdate);
}
$view_count = retrieve('views', 'articles', 'id', $r['id']);
step 6.
foreach ($tag as $tag ) {
switch ($tag) {
case 'date': echo $a_date_format; break;
case ($tag == 'views'): echo ' '.$view_count.' '.l('views').' '; break;
Personal note:
retrieve() function is technically yet another database call (an that is considered to be the slowest part of any php application), and I've chosen not to use it* in order to maintain the initial speed of article displaying.
___________
* Server stats usually gives far more accurate data.... and the visit counters are mostly redundant-pimping-show-off features you can live without.
EDIT: update sql command -
WHERE id = $artid changed to
WHERE id = $r[id]