Page 1 of 2
{ART_READ} Display number of times an article has been read
Posted: Thu 9. Jun 2005, 14:35
by JensZ
Hello again,
yet another simple replacement tag to display the number of times an article has been read. This is an enhancement of {MOST_READ}, which is required to be installed. See other thread for instructions on that one.
To use, simply add the following to frontend_render:
Code: Select all
// AUTHOR: Jens Zetterström {ART_READ}
// DESCRIPTION: Returns the number of times an article has been read.
// REQUIREMENTS: {MOST_READ} enhancement: phpwcms_article_read_count table, etc
// USAGE: {ART_READ:article_id:category_id}
// I put it in my article template file so that
// it appears for each article:
// {ART_READ:<?php echo $GLOBALS["content"]["article_id"];?>:<?php echo $GLOBALS["content"]["cat_id"];?>}
function get_article_read_cnt($art_id, $cat_id, $dbcon)
{
$count = 0;
$sql ="SELECT count( article_id ) as art_cnt
FROM ".DB_PREPEND."phpwcms_article_read_count
WHERE article_id = $art_id
AND cat_id = $cat_id ";
if($result = mysql_query($sql, $dbcon))
{
$row = mysql_fetch_assoc($result);
$count = $row["art_cnt"];
mysql_free_result($result);
}
return $count;
}
// {ART_READ}
if( ! ( strpos($content["all"],'{ART_READ:')===false ) )
{
$content["all"] = preg_replace('/\{ART_READ:(\d+):(\d+)\}/e','get_article_read_cnt($1,$2,$db);',$content["all"]);
}
Cheers,
Jens
Posted: Fri 10. Jun 2005, 15:45
by trip
you have been busy indeed

Posted: Fri 10. Jun 2005, 22:04
by frold
well the most read thing doesnt work I get:
sql error : count_sql - insert into phpwcms_article_read_count values(now() , 1,0)
When I try to visit a article
Posted: Sun 12. Jun 2005, 12:45
by JensZ
Hi frold,
Did you add the table phpwcms_article_read_count to your mysql database?
I noticed that I added an ID column with autoincrement to the table as I was getting an error of duplicate records. See if that solves it. That also requires that you specify which columns you are inserting in content.article.inc.php:
Code: Select all
if ($count_str_sql=="")
{
$count_str_sql ="insert into " . DB_PREPEND . "phpwcms_article_read_count (date_read, article_id, cat_id) values(now() , " .$content["article_id"] . "," . $content["cat_id"] . ")";
$count_sql = mysql_query($count_str_sql,$db);
if (!$count_sql) echo "sql error : " . count_sql . " - " . $count_str_sql . " - " . mysql_error();
}
mysql_free_result($cresult);
See if that helps!
Jens
version
Posted: Sun 12. Jun 2005, 18:15
by snezko
which version of phpwcms do you use ?
tag work fine, but when i put there you PHP code ty make this tag more dynamic, this script return empty values. and so i doesn't work.
what I am doing bad ?
Posted: Mon 13. Jun 2005, 00:00
by frold
It will not work can you give a complete guide?
Posted: Mon 13. Jun 2005, 12:02
by JensZ
Strange that you cannot do a simple insert. What does the mysql_error() return?
Jens
Posted: Mon 13. Jun 2005, 12:23
by frold
JensZ wrote:Strange that you cannot do a simple insert. What does the mysql_error() return?
Jens
well later last night I saw it was writing to the database
But in my template it would only show:
{ART_READ) It wasnt substitued by a number...
Posted: Mon 13. Jun 2005, 13:06
by JensZ
frold,
You'll need to supply it with an article_id and category_id:
{ART_READ:article_id:cat_id}
You can use php variables if you put it in the article itself:
{ART_READ:<?php echo $GLOBALS["content"]["article_id"];?>:<?php echo $GLOBALS["content"]["cat_id"];?>}
I have it like that in my article template file, so that it appears for each article. If that doesn't work try using [PHP]echo ...[/PHP]. I haven't tried that yet.
I don't know why I implemented it with both id's, article_id should be enough. That was kinda stupid, but that's the way it is for now. Maybe I'll change it later...
Jens
Posted: Mon 13. Jun 2005, 13:11
by frold
Well I placed the regtags in the template not the article
When I used:
{ART_READ:<?php echo $GLOBALS["content"]["article_id"];?>:<?php echo $GLOBALS["content"]["cat_id"];?>}
I just got an empty space....
Maybe I should try to reinstall it :S
And then use: ?
{ART_READ:[PHP] echo $GLOBALS["content"]["article_id"];[/PHP]:[PHP] echo $GLOBALS["content"]["cat_id"];[/PHP]}
And then I can place it in the template? and the script will automaticly insert the number of times the article have been read?
Posted: Mon 13. Jun 2005, 14:43
by JensZ
Sorry for the trouble. I don't know what could be the problem. Try putting an echo statement in the function to see that it actually enters the function and go from there, for example:
Code: Select all
echo "article: " . $art_id . " category: " . $cat_id;
/jens
Posted: Wed 7. Dec 2005, 15:34
by pSouper
deleted for being silly (found answer)
Posted: Thu 22. Mar 2007, 10:00
by johnn
I'm using this mod, but sometimes I get this error.
sql error : - insert into article_read_count values(now() , 38,0)
The numbers are different but it's very annoying. Anyone knows how to fix that?
Posted: Sat 21. Apr 2007, 02:58
by phalancs
Hey, I had to remove this line
Code: Select all
// {ART_READ:<?php echo $GLOBALS["content"]["article_id"];?>:<?php echo $GLOBALS["content"]["cat_id"];?>}
to make it work. Although its commented out it contains the <?php tags and this causes trouble.
Posted: Sun 22. Apr 2007, 19:22
by phalancs
@johnn
hmm i dont know what this error causes but i had the same problem. I solved it simply by stopping the error cause output line creation stuff.
you need to outcomment one line in the include/inc_front/*article*.php file that echos the mysql error..
simple add "//"