Hi How can I add the function on story.php page: 'This story has been xxx times'. My site runs on Pligg 9.6 CMS. Thanks
<?php if ($hits = file_get_contents('hits.txt')) { $hits++; } else { $hits = 1; } echo "'This story has been {$hits} times"; file_put_contents('hits.txt', $hits); ?> PHP:
Thanks Kaizoku, I do appreciate you time & help, but I think it works as 'page hit xxx times' which counts on every page irrespective of story page. For example if there are 2 stories. If you click on story #1 & then story #2 it will increase the count to 2 for story #2 even if it has been viewed only once. Here is my commented code in story.php, which may be helpful to you to find out the solution: // log the pageview // $pageview = new Pageview; // $pageview->type='story'; // $pageview->page_id=$link->id; // $pageview->user_id=$current_user->user_id; require_once(mnminclude.'check_behind_proxy.php'); // $pageview->user_ip=check_ip_behind_proxy(); Code (markup): and code in story_center.tpl which is for email this, discuss, stumble etc. This is wher I want to show "Views xxx times" function: {if $pagename eq "story"} <span id="addto-{$link_shakebox_index}"> <img src="{$my_pligg_base}/templates/{$the_template}/images/add.gif" align="absmiddle" /> <a href="javascript:bookmarksite('{$title_url}', '{$enc_url}')">{#PLIGG_Visual_LS_Fav_Book#}</a> <a title="Submit '{$title_short}' to StumbleUpon" href="http://www.stumbleupon.com/submit?url={$enc_url}&title={$title_short}"><img src="{$my_base_url}{$my_pligg_base}/templates/{$the_template}/images/su.png" width="17px" height="16px" align="absmiddle">{#PLIGG_Visual_LS_Stumbleupon#}</a> <a href="http://delicious.com/post" onclick="window.open('http://delicious.com/post?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;"><img src="{$my_base_url}{$my_pligg_base}/templates/{$the_template}/images/delicious.png" width="16px" height="16px" align="absmiddle"> Save to del.icio.us</a> </span> {/if} Code (markup):
Add the following code to story.php: $hitsList = file(mnmpath.'hits.txt'); $storyHits = 0; for ($i=0;$i<sizeof($hitsList);$i++) { list($id, $hits) = split('\|',trim($hitsList[$i])); if ($id == $requestID) { $hitsList[$i] = $requestID.'|'.++$hits."\n"; $storyHits = $hits; } } if (!$storyHits) { $storyHits = 1; $hitsList[] = $requestID.'|1'."\n"; } file_put_contents(mnmpath.'hits.txt',implode("",$hitsList)); $main_smarty->assign('hits', $storyHits); PHP: ...it should go from about line 75 after the following line: require_once(mnminclude.'check_behind_proxy.php'); PHP: Next, update story_center.tpl with the following, where you want to display the number of views: This story has been viewed {$hits} times Code (markup): It would be better to store the number of views in the database but this should do it for you.
Question. Code suggested by stoli works but how to modify the code so that the views are unique (like per IP address it counts only 1 view). Thanks
Thanks Kaizoku. But if I was capable of making it working 'successfully', I would have not asked for the help here.
Do you have access to edit/store in DB? $query = mysql_query("SELECT * FROM database WHERE ip='".$_SERVER['REMOTE_ADDR']."'"); if(mysql_num_rows($query) == 0){ $query = mysql_query("SELECT * FROM database_storing_hits"); $row = mysql_fetch_array($query); $hits = $row['hits']; // Now run the queries: mysql_query("INSERT INTO database ip="'.$_SERVER['REMOTE_ADDR']."'"); mysql_query("UPDATE database_storing_hits hits='".$hits++."'"); } PHP: That should ultimately work. Store IPs and hits. Regards, Dennis M.
For anyone who was interested I did write up a Pligg mod for this to work properly. You can see it below: $---BEGIN PLIGG MOD---$ # NOTE: ALL "Standard Line:"'s correlate to # Pligg 9.6 Beta (unmodified) code. # # MOD NAME: Unique Hits Mod (UHMOD) # MOD AUTHOR: Dennis McWherter ----------------------- - - - Open MySQL DB - - - ----------------------- Run query: ALTER TABLE `pligg_links` ADD `link_ip_views` LONGTEXT NOT NULL , ADD `link_hits` BIGINT( 10 ) NOT NULL ; ---------------------- - - - Open libs/links.php- - - ---------------------- FIND: (Standard line: 180) $link_date = $this->date; $link_published_date = $this->published_date; if($this->id===0) { $db->query("INSERT INTO " . table_links . " (link_author, link_status, link_randkey, link_category, link_date, link_published_date, link_votes, link_karma) VALUES ($link_author, '$link_status', $link_randkey, $link_category, FROM_UNIXTIME($link_date), FROM_UNIXTIME($link_published_date), $link_votes, $link_karma)"); $this->id = $db->insert_id; } else { // update $sql = "UPDATE " . table_links . " set `link_reports`=$link_reports, `link_comments`=$link_comments, link_author=$link_author, link_status='$link_status', link_randkey=$link_randkey, link_category=$link_category, link_modified=NULL, link_date=FROM_UNIXTIME($link_date), link_published_date=FROM_UNIXTIME($link_published_date), link_votes=$link_votes, link_karma=$link_karma WHERE link_id=$this->id"; $db->query($sql); } } AFTER ADD: // Log our hits :) // Pligg "unique hits" mod by Dennis McWherter function log_hit(){ global $db; $sql = "SELECT `link_ip_views` FROM `" . table_links . "` WHERE `link_id` = $this->id"; $row = $db->get_col($sql); // Check if the IP exists in the database! If not, count as a page hit! :) if(preg_match("/" . $_SERVER['REMOTE_ADDR'] . "/s",implode($row))){ return false; // Nothing more to do... } else { // Not in there yet? Add it :) $sql = "UPDATE `" . table_links . "` SET `link_ip_views` = CONCAT(`link_ip_views`, '" . $_SERVER['REMOTE_ADDR'] . "\n'), `link_hits` = link_hits+1 WHERE `link_id` = $this->id"; $db->query($sql); return true; } } // Pull how many hits we have for later ;) // Pligg "unique hits" mod by Dennis McWherter function num_hits(){ global $db; $sql = "SELECT `link_hits` FROM " . table_links . " WHERE `link_id` = $this->id"; $hits = $db->get_col($sql); if(is_array($hits)){ $hits = implode($hits); } return $hits; } FIND: $smarty->assign('story_edit_url', getmyurl("editlink", $this->id)); BEFORE ADD: // Add the template var for UH Mod by Dennis McWherter $smarty->assign('UHMOD_hits', $this->num_hits()); ---------------------- - - - Open story.php - - - ---------------------- FIND: (Standard line: 52) $link->id=$requestID; $link->read(); if(isset($_POST['process'])){ if ($_POST['process']=='newcomment') { insert_comment(); } } AFTER ADD: // Log number of unique page hits (by IP) // Pligg "unique hits" mod by Dennis McWherter $link->log_hit(); ------------------------- - - - Open link_summary.tpl - - In templates directory- - - ------------------------- Find: (Standard line: 137) <a href="{$tags_url_array[thistag]}" style='text-decoration: none;'>{$tag_array[thistag]}</a> {/section} </span> {/if} {/if} AFTER ADD: | <span id="ls_hits-{$link_shakebox_index}"><b>Views</b>: {$UHMOD_hits}</span> ------------------------------------------------- - - - SAVE AND CLOSE ALL FILES: You're Done! :D - - - ------------------------------------------------- $---END PLIGG MOD---$ Code (markup): Hope this helps everyone! I'll do paid installations of the mod for anyone who needs it as well. PM if interested. Regards, Dennis M.