<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/stats/cfg.php'; //Подключаемся к БД
?>
<html>
    <head>
        <title>Статистка кликов</title>
<meta name="robots" content="noindex,nofollow">
    </head>
    <body>

	
<!-- Начало вывода общей информации о кликах -->	
       <?php
	      
        //Выбираем и выводим записи
        $result = mysql_query("SELECT
                                        `id`, `click`, `text`
                                FROM
                                        main_rtr
                             ");
$dtnw 	= date ('Y-m-d');
						 
							 ?>
<table width="100%">
	<tr>
		<td width="50%" valign="top">
			<table border="1" cellpadding=5 cellspacing=0>
			<th colspan="3">
				За весь период
			</th>
				<tr>
					<td>
						ID
					</td>
					<td>
						ТЕКСТ
					</td>
					<td>
						Количество кликов
					</td>
				</tr>	 
			 <?php
				while ($row = mysql_fetch_array($result)) {
				
				
				$resulttemp = mysql_query("SELECT
                                        COUNT(`id`) AS `cnt`
                                FROM
                                        main_click
								WHERE `id`='".$row['id']."' AND `date` ='".$dtnw."'
                             ");
				$rowtemp = mysql_fetch_array($resulttemp)
				
				?>

				<tr>
				
					<td>
						<?=$row['id']?>
					</td>
					<td>
						<?=$row['text']?>
					</td>
					<td>
						<b><?=$row['click']?></b> 
							<?php if($rowtemp['cnt']!=0) 
								echo "<font style=\"color:green\">(+".$rowtemp['cnt'].")</font>"; ?>
					</td>
				</tr>
	
        <?php
			}
		
        //Выбираем и выводим записи
        $clicktoday = mysql_query("SELECT
                                       COUNT(`id_num`) AS `sum`
                                FROM
                                        main_click
								WHERE `date` ='".$dtnw."'
                             ");
		$rowclicktoday = mysql_fetch_array($clicktoday);
		?>
				<tr>
					<td>
						
					</td>
					<td>
					</td>
					<td>
						<b>&sum;</b> = <?php 
								echo "<font style=\"color:green\">+".$rowclicktoday['sum']."</font>"; 
						?>
					</td>
				</tr>	 
		
		
		
			</table>
			
<!-- Конец вывода общей информации о кликах -->	

	</td>
	<td width="50%" valign="top">

<!-- Начало вывода информации о кликах СЕГОДНЯ -->				
		       <?php

$datenow 	= date ('Y-m-d');
        //Выбираем и выводим записи
        $resulttoday = mysql_query("SELECT
                                       `id`, `text`, COUNT(`id_num`) AS `cnt`
                                FROM
                                        main_click, main_rtr
								WHERE `id`=`id` AND `date` ='".$datenow."'
								GROUP BY `id`
								ORDER BY `cnt` DESC
                             ");
							 ?>	
			<table border="1" width="100%" cellpadding=5 cellspacing=0>
			<th colspan="3">
				За сегодня
			</th>
				<tr>
					<td>
						ID
					</td>
					<td>
						ТЕКСТ
					</td>
					<td>
						Количество кликов
					</td>
				</tr>	 
			 <?php
				while ($rowtoday = mysql_fetch_array($resulttoday)) {
				?>

				<tr>
					<td>
						<?=$rowtoday['id']?>
					</td>
					<td>
						<?=$rowtoday['text']?>
					</td>
					<td>
						<b><?=$rowtoday['cnt']?></b>
					</td>
				</tr>
	
        <?php
			}
		?>
		

		
		
			</table>
<br/>

<!-- Конец вывода информации о кликах СЕГОДНЯ -->		



<!-- Начало вывода информации о кликах ВЧЕРА -->				
		       <?php

$dateyestoday 	= date("Y-m-d",time() - 1 * 24 * 60 * 60);
        //Выбираем и выводим записи
        $resultyestoday = mysql_query("SELECT
                                       `id`, `text`, COUNT(`id_num`) AS `cnt`
                                FROM
                                        main_click, main_rtr
								WHERE `id`=`id` AND `date` ='".$dateyestoday."'
								GROUP BY `id`
								ORDER BY `cnt` DESC
                             ");

    $clickyestoday = mysql_query("SELECT
                                       COUNT(`id_num`) AS `sum`
                                FROM
                                        main_click
								WHERE `date` ='".$dateyestoday."'
                             ");
		$rowclickyestoday = mysql_fetch_array($clickyestoday);	
							 
							 ?>	
			<table border="1" width="100%" cellpadding=5 cellspacing=0>
			<th colspan="3">
				За вчера
			</th>
				<tr>
					<td>
						ID
					</td>
					<td>
						ТЕКСТ
					</td>
					<td>
						Количество кликов
					</td>
				</tr>	 
			 <?php
				while ($rowyestoday = mysql_fetch_array($resultyestoday)) {
				

	
				?>

				<tr>
					<td>
						<?=$rowyestoday['id']?>
					</td>
					<td>
						<?=$rowyestoday['text']?>
					</td>
					<td>
						<b><?=$rowyestoday['cnt']?></b>
					</td>
				</tr>
	
        <?php
			}
		?>
		
				<tr>
					<td>
						
					</td>
					<td>
					</td>
					<td>
						<b>&sum;</b> = <?php 
								echo "<font style=\"color:green\">+".$rowclickyestoday['sum']."</font>"; 
						?>
					</td>
				</tr>	
		
		
			</table>
<br/>

<!-- Конец вывода информации о кликах ЗА ВЧЕРА -->		




<!-- Начало вывода информации о кликах ЗА 3 ДНЯ -->				
		       <?php
$date3back=date("Y-m-d",time() - 3 * 24 * 60 * 60);

        //Выбираем и выводим записи
        $result3day = mysql_query("SELECT
                                       `id`, `text`, COUNT(`id_num`) AS `cnt`
                                FROM
                                        main_click, main_rtr
								WHERE `id`=`id` AND `date` BETWEEN '".$date3back."' AND '".$datenow."'
								GROUP BY `id`
								ORDER BY `cnt` DESC
                             ");
							 

    $click3day = mysql_query("SELECT
                                       COUNT(`id_num`) AS `sum`
                                FROM
                                        main_click
								WHERE `date` BETWEEN '".$date3back."' AND '".$datenow."'
                             ");
	$rowclick3day = mysql_fetch_array($click3day);
							 
							 ?>	
<br/>
			<table border="1" width="100%" cellpadding=5 cellspacing=0>
			<th colspan="3">
				За 3 дня
			</th>
				<tr>
					<td>
						ID
					</td>
					<td>
						ТЕКСТ
					</td>
					<td>
						Количество кликов
					</td>
				</tr>	 
			 <?php
				while ($row3day = mysql_fetch_array($result3day)) {
				?>

				<tr>
					<td>
						<?=$row3day['id']?>
					</td>
					<td>
						<?=$row3day['text']?>
					</td>
					<td>
						<b><?=$row3day['cnt']?></b>
					</td>
				</tr>
	
        <?php
			}
		?>
		
				<tr>
					<td>
						
					</td>
					<td>
					</td>
					<td>
						<b>&sum;</b> = <?php 
								echo "<font style=\"color:green\">+".$rowclick3day['sum']."</font>"; 
						?>
					</td>
				</tr>	
		
			</table>
<br/>

<!-- Конец вывода информации о кликах ЗА 3 ДНЕЙ -->	


<br/>

<!-- Начало вывода информации о кликах ЗА 7 ДНЕЙ -->				
		       <?php
$date7back=date("Y-m-d",time() - 7 * 24 * 60 * 60);

        //Выбираем и выводим записи
        $result7day = mysql_query("SELECT
                                       `id`, `text`, COUNT(`id_num`) AS `cnt`
                                FROM
                                        main_click, main_rtr
								WHERE `id`=`id` AND `date` BETWEEN '".$date7back."' AND '".$datenow."'
								GROUP BY `id`
								ORDER BY `cnt` DESC
                             ");
							 

    $click7day = mysql_query("SELECT
                                       COUNT(`id_num`) AS `sum`
                                FROM
                                        main_click
								WHERE `date` BETWEEN '".$date7back."' AND '".$datenow."'
                             ");
	$rowclick7day = mysql_fetch_array($click7day);
							 
							 ?>	
<br/>
			<table border="1" width="100%" cellpadding=5 cellspacing=0>
			<th colspan="3">
				За 7 дней
			</th>
				<tr>
					<td>
						ID
					</td>
					<td>
						ТЕКСТ
					</td>
					<td>
						Количество кликов
					</td>
				</tr>	 
			 <?php
				while ($row7day = mysql_fetch_array($result7day)) {
				?>

				<tr>
					<td>
						<?=$row7day['id']?>
					</td>
					<td>
						<?=$row7day['text']?>
					</td>
					<td>
						<b><?=$row7day['cnt']?></b>
				</tr>
	
        <?php
			}
		?>
		
				<tr>
					<td>
						
					</td>
					<td>
					</td>
					<td>
						<b>&sum;</b> = <?php 
								echo "<font style=\"color:green\">+".$rowclick7day['sum']."</font>"; 
						?>
					</td>
				</tr>	
		
			</table>
<br/>

<!-- Конец вывода информации о кликах ЗА 7 ДНЕЙ -->	

<br/>

<!-- Начало вывода информации о кликах ЗА 30 ДНЕЙ -->				
		       <?php
$date30back=date("Y-m-d",time() - 30 * 24 * 60 * 60);

        //Выбираем и выводим записи
        $result30day = mysql_query("SELECT
                                       `id`, `text`, COUNT(`id_num`) AS `cnt`
                                FROM
                                        main_click, main_rtr
								WHERE `id`=`id` AND `date` BETWEEN '".$date30back."' AND '".$datenow."'
								GROUP BY `id`
								ORDER BY `cnt` DESC
                             ");
							 
    $click30day = mysql_query("SELECT
                                       COUNT(`id_num`) AS `sum`
                                FROM
                                        main_click
								WHERE `date` BETWEEN '".$date30back."' AND '".$datenow."'
                             ");
	$rowclick30day = mysql_fetch_array($click30day);
	
							 ?>	
<br/>
			<table border="1" width="100%" cellpadding=5 cellspacing=0>
			<th colspan="3">
				За 30 дней
			</th>
				<tr>
					<td>
						ID
					</td>
					<td>
						ТЕКСТ
					</td>
					<td>
						Количество кликов
					</td>
				</tr>	 
			 <?php
				while ($row30day = mysql_fetch_array($result30day)) {
				?>

				<tr>
					<td>
						<?=$row30day['id']?>
					</td>
					<td>
						<?=$row30day['text']?>
					</td>
					<td>
						<b><?=$row30day['cnt']?></b>
					</td>
				</tr>
	
        <?php
			}
		?>
				<tr>
					<td>
						
					</td>
					<td>
					</td>
					<td>
						<b>&sum;</b> = <?php 
								echo "<font style=\"color:green\">+".$rowclick30day['sum']."</font>"; 
						?>
					</td>
				</tr>	
			</table>
<br/>
<!-- Конец вывода информации о кликах ЗА 30 ДНЕЙ -->	

		</td>
	</tr>
</table>
    </body>
</html>