Hello

September 02, 2010, 05:01:49 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?
Login with username, password and session length
What sNews Is: - sNews is a simple, basic, customizable CMS tool suitable for developers with beginner-to-advanced PHP skills. It is also useful to have a good working knowledge of how to work with, set up and manage MySQL databases. sNews is not - geared towards the end-user who knows little or nothing about building and developing PHP-MySQL based websites.
News: Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: [MOD] Page view counter (sNews 1.5)  (Read 4465 times)
Mika
Administrator
ULTIMATE member
******

Karma: 9
Posts: 1408


WWW
« on: February 13, 2007, 09:03:31 AM »

Page view counter, version 0.1, tested in 1.5.31

1. database change
- add the following line in your phpmyadmin SQL view and run it
- it will create a new numeric column named views with zero as a default value
Quote
--standard mysql query
ALTER TABLE `articles` ADD `views` INT( 11 ) NOT NULL DEFAULT '0';
Quote
--mysql query using table PREFIX_
ALTER TABLE `PREFIX_articles` ADD `views` INT( 11 ) NOT NULL DEFAULT '0';
2. language variable (snews.php)
- add it somewhere in your language variables section
- it will be used inside article's infoline
Quote
$l['views'] = 'Views';
3. function tags() (snews.php)
- edit function tags() by adding a new element views in the array
Quote
function tags($tag) {
$tags = array();
$tags['infoline'] = '

,readmore,comments,views,date,edit,

';
4. function center() (snews.php)
- around line 512 find this snippet and add the blue part
Quote
if ($infoline == true) {
foreach ($tag as $tag) {
switch (true) {
case ($tag == 'date'): echo $a_date_format; break;
case ($tag == 'views'): echo ' '.$r['views'].' '.l('views').' '; break;
5. function center() (snews.php)
- a couple of lines below find this snippet and add the blue part
- this will increment view counter every time visitor hits full article view
- when logged in, view counter is disabled (otherwise all admin views/edits would be counted as well, but that would make the counter even less precise*)
Quote
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);
}
___________
* Counter is also active when a visitor hits comments link.


EDIT: update sql command - WHERE id = $artid changed to WHERE id = $r[id]
Logged

http://www.ni5ni6.com/ - Tutorials, Mods and How-To's about sNews CMS
sNews 1.6 Developers Edition - commented sNews 1.6 version
Mika
Administrator
ULTIMATE member
******

Karma: 9
Posts: 1408


WWW
« Reply #1 on: February 13, 2007, 05:18:37 PM »

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
Quote
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 flow

You 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:
Quote
$view_count = retrieve('views', 'articles', 'id', $r['id']);
- note: step 4 displays view counter BEFORE database update action takes place

Changes:

step 5.
Quote
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.
Quote
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]
Logged

http://www.ni5ni6.com/ - Tutorials, Mods and How-To's about sNews CMS
sNews 1.6 Developers Edition - commented sNews 1.6 version
codetwist
Hero Member
*****

Karma: 50
Posts: 955


« Reply #2 on: February 13, 2007, 09:38:08 PM »

Technically most correct for UPDATE would be something like this in SQL statement:
 views = views + 1

And no need to use $r['views'] or additional $views variable.

« Last Edit: September 24, 2007, 12:46:46 PM by codetwist » Logged
Mika
Administrator
ULTIMATE member
******

Karma: 9
Posts: 1408


WWW
« Reply #3 on: February 14, 2007, 08:08:13 AM »

Post-increment operator is used in my mod
$views++;
which is equivalent to
$views += 1;
or
$views = $views + 1;

Feel free to use it on your own accord Wink

EDIT: ahaa... I see now what you mean:
Quote
$queryUpdate = "UPDATE ".db('prefix')."articles SET views = views+1 WHERE id = $r[id]";
Why not? Both options are sufficient enough..
Logged

http://www.ni5ni6.com/ - Tutorials, Mods and How-To's about sNews CMS
sNews 1.6 Developers Edition - commented sNews 1.6 version
codetwist
Hero Member
*****

Karma: 50
Posts: 955


« Reply #4 on: February 14, 2007, 08:37:13 AM »

Mmm ... 5 stars  analytical explanation of incremental operation   :|  Smiley  Cheesy  :lol:

Another point of view is plain one - if site is intensively visited then $views variable is outdated the moment it was retrieved. That operation $views = $views++ might means actual reducing of real value at the time it gets to SQL.

And there is more to it from third point of view ... extra variable ... extra row or even two of php code ... whooooshhh - it was sound of performance just going down the drain :lol:

P.S. Just kidding Cheesy
« Last Edit: September 24, 2007, 12:47:18 PM by codetwist » Logged
Mika
Administrator
ULTIMATE member
******

Karma: 9
Posts: 1408


WWW
« Reply #5 on: February 14, 2007, 05:07:42 PM »

You've got the point - if the total size of the processing code is the only performance parameter taken into consideration. Then the extra rows could make a difference on a heavy traffic sites.

However, PHP processing is always faster than db processing - previously prepared (incremented) value should be faster than sql increment routine.
Logged

http://www.ni5ni6.com/ - Tutorials, Mods and How-To's about sNews CMS
sNews 1.6 Developers Edition - commented sNews 1.6 version
Armen
Sr. Member
****

Karma: 41
Posts: 334



WWW
« Reply #6 on: February 18, 2007, 04:19:00 PM »

Mika, please help us with "function popular_articles($num, $length)" for 1.5.31.
Logged

Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.
Mohd Fadli Saad
Newbie
*

Karma: 0
Posts: 26


business as usual


WWW
« Reply #7 on: December 05, 2007, 04:58:07 AM »

hi, i used this mode in v1.5, it WORKING!
Then i upgrade to v1.6 MU, it gone BAD!
It keep statistics count at 1, and flooding my email with 1 hit everytime people visit my site.

Any idea?
I use the same SQL table structure from v1.5, other functions, mod, articles is OK.
Only this stats bugging me to hell..

Please help me..
Logged

Pages: [1]
  Print  
 
Jump to:  

English Steel 1.6 © Saxon North Technologies
Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!