<?php
include("CheckSession.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="馬戲團" />
<title>馬戲團</title>
</head>
<?php
require_once("class/DBConfig.php");
require_once("class/DBOperation.php");
require_once("class/MacOperation.php");

$db = new DBOperation();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$db->query("SELECT count(*) FROM Customer");
$row = $db->fetch_row();  
$totalCount = $row[0];

$number = $totalCount/5000;
$answer = floor($number);
$j = $totalCount%5000;

if($j>0)
{
	$answer = $answer + 1;
}

//$i = 0;
//$i = 1;
//$i = 2;
//$i = 3;
//$i = 4;
//$i = 5;
//$i = 6;
//$i = 7;
//$i = 8;
//$i = 9;
//$i = 10;
//$i = 11;
$i = 12;

//for ( $i=0 ; $i<$answer ; $i++ ) {
	$start = $i*5000;

	$db->query("SELECT * FROM Customer LIMIT ".$start.", 5000");
	if($myrow = $db->fetch_array())     
	{
		do{
	   	$ConTimes = 0; 
	   	$db1 = new DBOperation();
			$db1->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
			
	   	$db1->query("SELECT Count(*) FROM CMissContent where CCId = '".$db->mysql_real_escape_string($myrow['CId'])."'");
	   	$row = $db1->fetch_row();  
	   	$ConTimes = $row[0];   
	   	
	   	$db1->close_db();
	   	
	   	$db2 = new DBOperation();
			$db2->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
		 	$updateSql = "UPDATE Customer set ConTimes = ".$ConTimes;
		 	$updateSql = $updateSql." where CId = ".$myrow['CId'];
		 	//echo $updateSql;
		 	$db2->query($updateSql);
		 	$db2->close_db();
		}while($myrow = $db->fetch_array());		
	}
	sleep(1);
//}
$db->close_db();

$url = "CountingTimes.php";
    
echo '<script type="text/javascript">';
echo 'alert("統計成功!!!!");';
echo 'location.href= "'.$url.'";';
echo '</script>';			

?>
</html>
