Jump to content

[SOLVED] Counting visit of "news"


web_master

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/71566-solved-counting-visit-of-news/
Share on other sites

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;";

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'

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']."'
");

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.