Hit counter

I am trying to build a basic hit counter so users can see how often there page was viewed during a month.

I’m going to use the following code

[php]

“UPDATE something SET hits = hits + 1”

[/php]

as you can see its nothing extravagant but this will add to the overall hits, what I want is a monthly hit counter.

How would I go about inserting it into a monthly thing

add a year and month field and use:

“UPDATE something SET hits = hits + 1 WHERE month=”.date(‘m’)." AND year=".date(‘Y’)

of cause if it’s not existing u have to INSERT it.

Think i’ve sorted it

[php]
$upd_month = date(‘F’);
$upd_year = date(‘Y’);

mysql_select_db($database_connection, $connection);
$query_hits = “SELECT * FROM property_hits WHERE list_id = ‘$list_id’”;
$hits = mysql_query($query_hits, $connection) or die(mysql_error());
$row_hits = mysql_fetch_assoc($hits);
$totalRows_hits = mysql_num_rows($hits);

$m_toupdate = $row_hits[‘month’];
$y_toupdate = $row_hits[‘year’];
$inc_hits = $row_hits[‘hits’];
$inc_hits = $inc_hits + 1;
if(file_exists($m_toupdate == $upd_month) || ($y_toupdate == $upd_year)) {
mysql_select_db($database_connection, $connection);
$hit_count = “UPDATE property_hits SET hits = $inc_hits WHERE month = ‘$upd_month’ AND year = ‘$upd_year’ AND list_id = ‘$list_id’”;
$hit_result = mysql_query($hit_count, $connection) or die(mysql_error()); }
else {

mysql_select_db($database_connection, $connection);
$hit_count = (“INSERT INTO property_hits ( list_id, hits, month, year) VALUES (’$list_id’, ‘$inc_hits’, ‘$upd_month’, ‘$upd_year’)”);
$hit_result = mysql_query($hit_count, $connection) or die(mysql_error()); }
?>
[/php]

Just gotta wait til next month now and see if it works properly :wink:

i’m wondering about ‘file_exists($m_toupdate == $upd_month)’
this doesn’t make any sence to me.

its checking whether the month and year are present int he database. if they are it updates, if they arnt it inserts

i just think this is far to complicated

and i’m not sure what list_id is used for.

i would use something like:
[php]
$upd_month = date(‘F’);
$upd_year = date(‘Y’);

mysql_select_db($database_connection, $connection);

$query_hits = “SELECT hits FROM property_hits WHERE month = ‘$upd_month’ AND year = ‘$upd_year’ AND list_id = ‘$list_id’”;
$hits = mysql_query($query_hits, $connection) or die(mysql_error());

if(mysql_num_rows($hits))
{
$hit_count = “UPDATE property_hits SET hits = hits + 1 WHERE month = ‘$upd_month’ AND year = ‘$upd_year’ AND list_id = ‘$list_id’”;
$hit_result = mysql_query($hit_count, $connection) or die(mysql_error());
}
else
{
$hit_count = (“INSERT INTO property_hits ( list_id, hits, month, year) VALUES (’$list_id’, ‘1’, ‘$upd_month’, ‘$upd_year’)”);
$hit_result = mysql_query($hit_count, $connection) or die(mysql_error());
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service