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

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.