MOST_READ : front end code to add

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
erwanpia
Posts: 39
Joined: Wed 21. Apr 2004, 10:28
Location: Breizh - Europa
Contact:

MOST_READ : front end code to add

Post by erwanpia »

I created this new tag : MOST_READ, works like NEWS

code to add in front.func.inc.php :

Code: Select all


function get_most_read_articles($template_default, $max_cnt_links=0, $dbcon) {
	// find all new articles

	$max_cnt_links = intval($max_cnt_links);
	$limit = ($max_cnt_links) ? " LIMIT ".$max_cnt_links : "";
	
	$sql ="SELECT article_id, count( article_id ) c
FROM ".DB_PREPEND."article_read_count
GROUP BY article_id
ORDER BY c DESC, date_read desc  
$limit ";
	$arr_id=array();
	$arr_count=array();
	if($result = mysql_query($sql, $dbcon)) {
		$count_results = mysql_num_rows($result); $count = 0;
		while ($row = mysql_fetch_row($result)) 
			{
			array_push($arr_id, $row[0]);
			$arr_count[$row[0]]= $row[1];
			}
reset($arr_id);
	$new_links = "";
	$count_results = count($arr_id); $count = 0;
while (list($k,$v)=each($arr_id))
{
	$sql =	"SELECT article_id, article_title, article_cid, UNIX_TIMESTAMP(article_tstamp) AS article_date ".
			"FROM ".DB_PREPEND."phpwcms_article WHERE ".
			"article_public=1 AND article_aktiv=1 AND article_deleted=0 AND ".
			"article_begin < NOW() AND article_end > NOW()  and article_id = ". $v ;

	// new articles list

	$target = ($template_default["link_target"]) ? ' target="'.$template_default["link_target"].'"' : "";
	if($result = mysql_query($sql, $dbcon)) {
	
		while ($row = mysql_fetch_row($result)) {
			$count++;
			if($template_default["link_length"] && strlen($row[1]) > $template_default["link_length"]) {
				$article_title = substr($row[1], 0, $template_default["link_length"]).$template_default["cut_title_add"];
			} else {
				$article_title = $row[1];
			}
			/*if(trim($template_default["date_format"])) {
				$article_title = international_date_format(
									$template_default["date_language"], 
									$template_default["date_format"], 
									$row[3]).$article_title;
			}*/
			$article_title = "&#149; " . $article_title ;
			$show_count = $arr_count[$row[0]] . " readers";
			$new_links .= $template_default["link_before"].$template_default["link_symbol"];
			$new_links .= '<a href="index.php?id='.$row[2].','.$row[0].',0,0,1,0"';
			$new_links .= $target." title='$show_count' alt='$show_count'>".html_specialchars($article_title)."</a>";
			
			//try to remove possible unwanted after - if not enclosed before.link.after
			if($new_links && !$template_default["link_before"] && $count < $count_results) {
				$new_links .= $template_default["link_after"];
			}
		}
		mysql_free_result($result);
	}
}
}
	//enclose whole
	if($new_links) $new_links = $template_default["before"].$new_links.$template_default["after"];
	
	return $new_links;
}
code to add in content.func.inc.php

Code: Select all

// most read article sorted by popularity
if( ! ( strpos($content["all"],'{MOST_READ:')===false ) ) {
	$content["all"] = preg_replace('/\{MOST_READ:(\d+)\}/e','get_most_read_articles($template_default["news"],$1,$db);',$content["all"]);
}
sample on http://ecolopop.free.Fr on the left menu at the bottom

the template for that one is :
(place in left field in template admin)

Code: Select all

<br>
<img src="img/leer.gif" width="1" height="5">
<table cellspacing=0 cellpadding=0 border=0><tr><td><table bgcolor=cccccc cellpadding=0 cellspacing=0 width=100%><tr><td bgcolor=cccccc><img src="img/leer.gif" width="1" height="1"></td></table></td><tr><Th bgcolor=#faffea height=20>Most popular</th><tr><td><img src="img/leer.gif" width="1" height="1"></td><tr><td><table bgcolor=cccccc cellpadding=0 cellspacing=0 width=100%><tr><td bgcolor=cccccc><img src="img/leer.gif" width="1" height="1"></td></table></td><tr><td><table><tr><td>{MOST_READ:5}</td></table></td><tr><td><img src="img/leer.gif" width="1" height="1"></td><tr><td><table bgcolor=cccccc cellpadding=0 cellspacing=0 width=100%><tr><td bgcolor=cccccc><img src="img/leer.gif" width="1" height="1"></td></table></td></table>
brans

Post by brans »

ouhm maybe you should better use the frontend init and render Folder for creating new replacement tags instead of changing core files ?!?

see the changelog.txt for hints on render and init folder or search this forum for init folder and author brans ... there you will find some hints on how to use it!
Pappnase

Post by Pappnase »

hello

if you would get this replacement tag as a php script like brans discribe.
and other also create new replacement tags send it to me an i will open an replacement tag download area at the docu site!!
frold
Posts: 2151
Joined: Tue 25. Nov 2003, 22:42

Post by frold »

Pappnase wrote:hello

if you would get this replacement tag as a php script like brans discribe.
and other also create new replacement tags send it to me an i will open an replacement tag download area at the docu site!!
Good idea, maybe you also could make a mod section when v1.2 is out - a v1.2 valid mod section - so we only have to visit one site to find mods and info

what about a template database... :?:
http://www.studmed.dk Portal for doctors and medical students in Denmark
Pappnase

Post by Pappnase »

hello

a template database is an good idea! i also write in this case a few month ago an thread. but it cames no response.

but i will not make an mod database as long as there so many source code editing is needed. :!:
frold
Posts: 2151
Joined: Tue 25. Nov 2003, 22:42

Post by frold »

Pappnase wrote: but i will not make an mod database as long as there so many source code editing is needed. :!:
Ofcouse you need to edit the sourcecode - there is no plugin function yet
http://www.studmed.dk Portal for doctors and medical students in Denmark
Pappnase

Post by Pappnase »

hello

yes thats true. but i hope that this function will come!
brans

Post by brans »

öhm mates I guess that most mods could be realized through using the init and render folder! Maybe you would have to increase the size of your scripts a little bit but if this is the condition for not editing the core code anymore, why not ?
Pappnase

Post by Pappnase »

brans wrote:öhm mates I guess that most mods could be realized through using the init and render folder! Maybe you would have to increase the size of your scripts a little bit but if this is the condition for not editing the core code anymore, why not ?
hello brans

i have thought that too. but son't write cos i'm a php dummy. :oops: :wink:
brans

Post by brans »

so am I ;-)

so maybe the mod coding guidelines should be updated... I think they have their roots in a time when the init and render folder hadn't been created...
creep
Posts: 10
Joined: Tue 28. Dec 2004, 15:53

Post by creep »

Sorry, where is the table article_read_count ? :shock:

Code: Select all

   $sql ="SELECT article_id, count( article_id ) c 
FROM ".DB_PREPEND."article_read_count 
GROUP BY article_id 
ORDER BY c DESC, date_read desc  
$limit "; 
erwanpia
Posts: 39
Joined: Wed 21. Apr 2004, 10:28
Location: Breizh - Europa
Contact:

Post by erwanpia »

table article_read_count is in a previous post :

http://phpwcms.de/forum/viewtopic.php?t=4816
User avatar
lady_witchcraft
Posts: 94
Joined: Sun 14. Nov 2004, 19:19
Location: Austria
Contact:

little prob with article count - HELP NEEDED

Post by lady_witchcraft »

hy guys - got a little problem with your hack - this is the failure the page reports - what does it mean? i followed every step in your old and this post to implement the code - please help me!? thx in advance :oops:

failure: "sql error : count_sql - insert into article_read_count values(now() , 17,3) "
... it's not bad luck, it's a bug ...
User avatar
marcus@localhorst
Posts: 815
Joined: Fri 28. May 2004, 11:31
Location: localhorst
Contact:

Post by marcus@localhorst »

hi,

i've tried to put that script in the frontend_render but I see there are many variables not available.
is there a solution?

marcus
brans

Post by brans »

hi marcus, what variables are not available ?
Post Reply