Counting total clicks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Counting total clicks

Post by Mr Tech »

I have this code which updates the amount of times a tutorial is viewed each month and in total...

Code: Select all

<?php
// Current month & year
$month = date('M', time());
$year = date('Y', time());

// Check if month & year exist
$check = mysql_num_rows(mysql_query("select * from clicks where month='$month' and year='$year' and id='$id'"));
if ($check > 0) {
mysql_query("update clicks set tclicks = tclicks + 1 where month='$month' and year='$year' and id='$id'");
} else {
mysql_query("insert into clicks values($month,$year,$id,'1')");
}

// Show total clicks
$totalclicks = mysql_fetch_array(mysql_query("select tclicks from clicks where id='$id'"));
$monthclicks = mysql_fetch_array(mysql_query("select tclicks from clicks where month='$month' and year='$year' and id='$id'"));

echo "This tutorial has been viewed $monthclicks[tclicks] times this month and $totalclicks[tclicks] in total.";
?>
Basically I have to withdraw the total clicks from each month and add them all together. How do I do this?

Let me know if that is not clear.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

SELECT sum(tclicks) FROM clicks WHERE year = $year
Post Reply