<?php
include ('mysql.php');

$exists = array();

// Get traffic
$traffic $db->sql_query ("SELECT trafficid, datetimestamp, type, ipaddress, ad FROM traffic");
if (
count($traffic) == 0) {    die('No traffic logged'); }

$delete '(0';
foreach (
$traffic as $t) {
    
// Get values
    
$day date('d'$t['datetimestamp']);
    
$week date('W'$t['datetimestamp']);
    
$month date('n'$t['datetimestamp']);
    
$year date('Y'$t['datetimestamp']);
    
$ad $t['ad'];
    
    
// Which type?
    
if ($t['type'] == 'view') {
        
$views 'views + 1';
        
$clicks 'clicks';
    } else {
        
$views 'views';
        
$clicks 'clicks + 1';
    }

    
records_exist($day$week$month$year$ad);

    
// Update day record
    
$db->query ("UPDATE stats SET views = $views, clicks = $clicks, ctr = ((clicks/views)*100) WHERE type = 'day' AND day = $day AND month = $month AND year = $year AND ad = $ad");

    
// Update week record
    
$db->query ("UPDATE stats SET views = $views, clicks = $clicks, ctr = ((clicks/views)*100) WHERE type = 'week' AND week = $week AND year = $year AND ad = $ad");

    
// Update month record
    
$db->query ("UPDATE stats SET views = $views, clicks = $clicks, ctr = ((clicks/views)*100) WHERE type = 'month' AND month = $month AND year = $year AND ad = $ad");

    
// Update total record
    
$db->query ("UPDATE stats SET views = $views, clicks = $clicks, ctr = ((clicks/views)*100) WHERE type = 'total' AND ad = $ad");

    
$delete .= ',' $t['trafficid'];
}
$delete .= ')';

$db->query ("DELETE FROM traffic WHERE trafficid IN $delete");

function 
records_exist($day$week$month$year$ad) {
    global 
$exists$db;

    
// Array keys:
    
$daykey 'day:' $day '-' $month '-' $year '-ad:' $ad;
    
$weekkey 'week:' $week '-' $year '-ad:' $ad;;
    
$monthkey 'month:' $month '-' $year '-ad:' $ad;;
    
$totalkey 'total-ad:' $ad;

    
// Check if day ad records exists
    
if (!isset($exists[$daykey])) {
        
// In db maybe?
        
$test $db->query_first ("SELECT statsid FROM stats WHERE type = 'day' AND day = $day AND month = $month AND year = $year AND ad = $ad");

        if (
$test == false) {
            
// Insert it
            
$db->query ("INSERT INTO stats (day, month, year, week, ad, type) VALUES ($day, $month, $year, $week, $ad, 'day')");
        }

        
$exists[$daykey] = true;
    }

    
// Check if week ad records exists
    
if (!isset($exists[$weekkey])) {
        
// In db maybe?
        
$test $db->query_first ("SELECT statsid FROM stats WHERE type = 'week' AND week = $week AND year = $year AND ad = $ad");

        if (
$test == false) {
            
// Insert it
            
$db->query ("INSERT INTO stats (month, year, week, ad, type) VALUES ($month, $year, $week, $ad, 'week')");
        }

        
$exists[$weekkey] = true;
    }

    
// Check if month ad records exists
    
if (!isset($exists[$monthkey])) {
        
// In db maybe?
        
$test $db->query_first ("SELECT statsid FROM stats WHERE type = 'month' AND month = $month AND year = $year AND ad = $ad");

        if (
$test == false) {
            
// Insert it
            
$db->query ("INSERT INTO stats (month, year, ad, type) VALUES ($month, $year, $ad, 'month')");
        }

        
$exists[$monthkey] = true;
    }

    
// Check if total ad records exists
    
if (!isset($exists[$totalkey])) {
        
// In db maybe?
        
$test $db->query_first ("SELECT statsid FROM stats WHERE type = 'total' AND ad = $ad");

        if (
$test == false) {
            
// Insert it
            
$db->query ("INSERT INTO stats (ad, type) VALUES ($ad, 'total')");
        }

        
$exists[$totalkey] = true;
    }

}


?>