Loading...
 
Features / Usability

Features / Usability


Article Read count not working .... Update

How could this code ever work for anyone????????? I guess the offending code in /tiki/lib/articles/artlib.php is :-


''function add_article_hit($articleId)
{
global $prefs, $user;

if ($prefs'count_admin_pvs' == 'y' || $user != 'admin') {
$query = "update `tiki_articles` set `nbreads`=`nbreads`+1 where `articleId`=?";

$result = $this->query($query, array($articleId));
}

return true;
}''


Running the guts of it in a mysql session we get the following:-


mysql> update `tiki_articles` set `nbreads`=`nbreads`+1 where `articleId`=1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0

since the initial value of nbreads is null then adding 1 to null results in null doesn't it! So if we manually set it to 0 and try the code again.. Hey presto it works.

mysql> update tiki_articles set nbreads = 0 where articleid = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> update `tiki_articles` set `nbreads`=`nbreads`+1 where `articleId`=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Since there is no null test in the add_article_hit function how could this ever work. The code below might have a fighting chance of working .. at least that is what I am going to use until a tiki developer / expert explains why the initial value of nbreads on article creation is null. I might explore that a bit more deeply in a bit.


$query = "update `tiki_articles` set `nbreads` = if(`nbreads` is null,1,`nbreads`+1) where `articleId`=?";

so ..... I can't find where the sql is located in the source that inserts the record into tiki_articles but from my mysql logs below the value of nbreads does not get set but remains null. Look below, no nbreads field in the insert into field list.

vi mysql.jk
root@Newbarn tmp# grep tiki_articles mysql.jk
6 Query INSERT INTO `tiki_articles` (`title`, `authorName`, `topicId`, `topicName`, `size`, `useImage`, `image_name`, `image_type`, `image_size`, `image_data`, `isfloat`, `image_x`, `image_y`, `list_image_x`, `list_image_y`, `heading`, `body`, `publishDate`, `expireDate`, `created`, `author`, `type`, `rating`, `topline`, `subtitle`, `linkto`, `image_caption`, `lang`, `ispublished`) VALUES ('f', 'System Administrator', '0', NULL, '1', 'n', , , '0', , 'n', '0', '0', '0', '0', 'f', 'f', '1429661340', '1461283740', '1429661356', 'admin', 'Article', '7', , , , , , 'y')
8 Query select `tiki_articles`.*,
from `tiki_articles`
left join `tiki_article_types` ON `tiki_articles`.`type` = `tiki_article_types`.`type`
left join `users_users` on `tiki_articles`.`author` = `users_users`.`login`
where `tiki_articles`.`articleId`='6'
8 Query update `tiki_articles` set `nbreads`=`nbreads`+1 where `articleId`='6'

Someone who understands more how this works please enlighten me

Thanks J

OK so i've tested it and this code works. Here is my revised function in /tiki/lib/articles/artlib.php for anyone that wants to use it although really the source should be corrected to insert an initial value into tiki_articles.nbreads

              

function add_article_hit($articleId)
        {
                global $prefs, $user;

                if ($prefs'count_admin_pvs' == 'y' || $user != 'admin') {
                $query = "update `tiki_articles` set `nbreads` = if(`nbreads` is null,1,`nbreads`+1) where `articleId`=?";

                        $result = $this->query($query, array($articleId));
                }

                return true;
        }


Upcoming Events

No records to display

Why Register?

Register at tiki.org and you'll be able to use the account at any *.tiki.org site, thanks to the InterTiki feature. A valid email address is required to receive site notifications and occasional newsletters. You can opt out of these items at any time.