{ART_READ} Display number of times an article has been read

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
JensZ
Posts: 136
Joined: Wed 16. Feb 2005, 12:18
Location: Stockholm, Sweden
Contact:

{ART_READ} Display number of times an article has been read

Post 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
trip
Posts: 657
Joined: Tue 17. Feb 2004, 09:56
Location: Cape Town, South Africa
Contact:

Post by trip »

you have been busy indeed

8) 8)
frold
Posts: 2151
Joined: Tue 25. Nov 2003, 22:42

Post 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
http://www.studmed.dk Portal for doctors and medical students in Denmark
JensZ
Posts: 136
Joined: Wed 16. Feb 2005, 12:18
Location: Stockholm, Sweden
Contact:

Post 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
snezko
Posts: 21
Joined: Fri 11. Mar 2005, 18:33
Location: Wien
Contact:

version

Post 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 ?
many thanks Snezko
frold
Posts: 2151
Joined: Tue 25. Nov 2003, 22:42

Post by frold »

It will not work can you give a complete guide?
http://www.studmed.dk Portal for doctors and medical students in Denmark
JensZ
Posts: 136
Joined: Wed 16. Feb 2005, 12:18
Location: Stockholm, Sweden
Contact:

Post by JensZ »

Strange that you cannot do a simple insert. What does the mysql_error() return?

Jens
frold
Posts: 2151
Joined: Tue 25. Nov 2003, 22:42

Post 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...
http://www.studmed.dk Portal for doctors and medical students in Denmark
JensZ
Posts: 136
Joined: Wed 16. Feb 2005, 12:18
Location: Stockholm, Sweden
Contact:

Post 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
frold
Posts: 2151
Joined: Tue 25. Nov 2003, 22:42

Post 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?
http://www.studmed.dk Portal for doctors and medical students in Denmark
JensZ
Posts: 136
Joined: Wed 16. Feb 2005, 12:18
Location: Stockholm, Sweden
Contact:

Post 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
User avatar
pSouper
Posts: 1552
Joined: Tue 11. Nov 2003, 15:45
Location: London
Contact:

Post by pSouper »

deleted for being silly (found answer)
johnn
Posts: 50
Joined: Mon 10. Apr 2006, 14:07

Post 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?
phalancs
Posts: 793
Joined: Thu 19. Feb 2004, 05:09
Location: Germany

Post 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.
2008
phalancs
Posts: 793
Joined: Thu 19. Feb 2004, 05:09
Location: Germany

Post 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 "//"
2008
Post Reply