<?php

	include "library.php";

	$cxn = start_code( true );

	echo "<html><head><meta name='viewport' content='width=device-width, initial-scale=1.0'></head>";

	echo "<style> html { font-size: 9pt; font-family: verdana; } ";
	echo ".glink { font-size: 9pt; font-family: verdana; } ";
    echo "</style>";
	
	
	echo "<font color='#999999'>".date( "d M Y  g:ia" )."</font><br><br><br>";


	$today_date = date_create( date("Y-m-d") );

	$today_date_str = $today_date->format( "Y-m-d" );		


    $day_to_process = date_create( date("Y-m-d") );

	date_sub( $day_to_process, date_interval_create_from_date_string( '10 days' ));

	
    $day_to_process_str = $day_to_process->format( "Y-m-d" );		


	echo "<br>Hits this day<br><br>";
	
	echo "<table>";

	echo "<tr><td>Date</td><td align='right'>Australia</td><td align='right'>Other Countries</td></tr>";

    for (; $day_to_process_str <= $today_date_str;)
	{
    	$query = "SELECT DISTINCT ip_address FROM hits WHERE hit_date = '".$day_to_process_str."' AND ip_country = 'Australia'";

     	$result = run_query( $query, $cxn );	    

        $au_num_visitors = query_num_rows( $result );


    	$query = "SELECT DISTINCT ip_address FROM hits WHERE hit_date = '".$day_to_process_str."' AND ip_country <> 'Australia'";

     	$result = run_query( $query, $cxn );	    

        $other_num_visitors = query_num_rows( $result );


		echo "<tr><td>".substr( $day_to_process_str, 8, 2 )."/".substr( $day_to_process_str, 5, 2 )."/".substr( $day_to_process_str, 0, 4 )."</td><td align='right'>".$au_num_visitors."</td><td align='right'>".$other_num_visitors."</td>";


		date_add( $day_to_process, date_interval_create_from_date_string( '1 day' ));
    	$day_to_process_str = $day_to_process->format( "Y-m-d" );		
	}


    echo "</table><br><br><br>";
    

		/* latest site visitors */
		
	echo "Latest site visitors (Australia)<br><br>";

	echo "<table>";

	echo "<tr><td>IP Address</td><td>Source</td><td align='center'>Latest Hit</td><td>Pages</td></tr>";

	
	$query = "SELECT * FROM hits WHERE ip_country = 'Australia' ORDER BY latest_hit DESC LIMIT 15";

 	$result = run_query( $query, $cxn );

	for ($i=0; $i < query_num_rows( $result ); $i++)
	{
		$this_row = get_row( $result );
		
		$data[$i] = $this_row;
	}


	for ($i = query_num_rows( $result )-1; $i >= 0; $i--)
	{
		$time_txt = substr( $data[$i]['latest_hit'], 11, 8 );

		$time_txt2 = substr( $data[$i]['latest_hit'], 0, 4 )."&#8209;".substr( $data[$i]['latest_hit'], 5, 2 )."&#8209;".substr( $data[$i]['latest_hit'], 8, 2 );
		
		$time_display = date_format( date_create_from_format( "H:i:s", $time_txt ),  "g:i" )."&nbsp;".date_format( date_create_from_format( "H:i:s", $time_txt ),  "a" );
		
		echo "<tr><td>&nbsp;".$data[$i]['ip_address']."&nbsp;</td><td>&nbsp;".$data[$i]['source']."&nbsp;</td><td>&nbsp;".$time_txt2."</td><td align='right'>".$time_display."</td><td align='right'>".$data[$i]['hits']."</td></tr>";
		
//		echo "<tr><td>&nbsp;".$data[$i]['ip_address']."&nbsp;</td><td>&nbsp;".$data[$i]['ip_country']."&nbsp;</td><td>&nbsp;".$data[$i]['source']."&nbsp;</td><td>&nbsp;".substr( $data[$i]['latest_hit'], 0, 10 )." ".$time_display."</td><td align='right'>".$data[$i]['hits']."</td></tr>";
	}
	

	echo "</table><br><br><br>";

?>