web_master Posted October 2, 2007 Share Posted October 2, 2007 Hi, Ive made (redesign and reprogramming) one internet site. So, I want to count how many people visit (click) every "news". You can see the site www.radio90.co.yu and when You click on one "news" headline or lead, You can read all text. So I want to count how many people read each news. I hope it was understudable. Thanks in advanced. Quote Link to comment Share on other sites More sharing options...
MmmVomit Posted October 2, 2007 Share Posted October 2, 2007 I'm going to assume each news item is stored as a record in a table. You need to have a column (let's say num_views) that will store the number of times people have read that article. Every time someone access an article, you need to run a query like this $sql = "UPDATE news_table SET num_views = num_views + 1 WHERE id = $id_of_news_story;"; Quote Link to comment Share on other sites More sharing options...
micah1701 Posted October 2, 2007 Share Posted October 2, 2007 i think i understudable'd you. when the news article is loaded, is the information coming out of a database? if so you could just add a column to the articles table and add +1 to it each time someone views the article. UPDATE table SET pageview++ WHERE primarykey = 'selected_article' Quote Link to comment Share on other sites More sharing options...
MmmVomit Posted October 2, 2007 Share Posted October 2, 2007 You would need two queries, one to retrieve the article (a SELECT query) and one to update the pageviews column (an UPDATE query). But you can't use the increment operator (++). MySQL doesn't understand it. You'll have to use an old fashioned x = x + 1. Quote Link to comment Share on other sites More sharing options...
web_master Posted October 2, 2007 Author Share Posted October 2, 2007 Its workin, thanx people lots!!! Quote Link to comment Share on other sites More sharing options...
web_master Posted October 2, 2007 Author Share Posted October 2, 2007 This is the way, how I do it. // Counting visit $query_return = mysql_query("SELECT * FROM `news` WHERE `news_id` = '".$_GET['news_id']."'"); if(!$query_return){ print mysql_error(); exit; } $request_count = mysql_fetch_array($query_return); $count = $request_count['news_count'] + 1; //print $count; $query_return = mysql_query("UPDATE `news` SET `news_count` = '".$count."' WHERE `news_id` = '".$_GET['news_id']."' "); Quote Link to comment Share on other sites More sharing options...
MmmVomit Posted October 2, 2007 Share Posted October 2, 2007 I don't know if you'll see this, but you can just do the operation inside of the actual query. <?php // Counting visit $query_return = mysql_query("SELECT * FROM `news` WHERE `news_id` = '".$_GET['news_id']."'"); if(!$query_return){ print mysql_error(); exit; } $request_count = mysql_fetch_array($query_return); // NOT NEEDED $count = $request_count['news_count'] + 1; //print $count; $query_return = mysql_query("UPDATE `news` SET `news_count` = `news_count` + 1 WHERE `news_id` = '".$_GET['news_id']."' "); ?> Also, make sure you filter $_GET['news_id']. That code is open to sql injection attacks. Quote Link to comment Share on other sites More sharing options...
web_master Posted October 2, 2007 Author Share Posted October 2, 2007 You right! Quote Link to comment Share on other sites More sharing options...
web_master Posted October 2, 2007 Author Share Posted October 2, 2007 This is enough <?php // Counting visit $query_return = mysql_query("UPDATE `news` SET `news_count` = `news_count` + 1 WHERE `news_id` = '".$_GET['news_id']."' "); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.