1. 程式人生 > >php實現一個簡單的新增查詢統計功能

php實現一個簡單的新增查詢統計功能

<?php 
require('config.inc.php');
date_default_timezone_set("Asia/Shanghai");
header("Content-type: text/html; charset=utf-8"); 

$str_from = $_GET["from"].' 00:00:00';
$str_to = $_GET["to"].' 00:00:00';
$from_time = strtotime($str_from);
$to_time = strtotime($str_to);
$display_all = array_key_exists("dispall"
, $_POST); if ($from_time >= $to_time){ echo '起始時間必須小於截止時間'; return; } $conn = mysqli_connect($_CFG['mysql_host'], $_CFG['mysql_user'], $_CFG['mysql_pwd'], $_CFG['mysql_db']); if (mysqli_connect_errno($conn)) { echo "mysql連線失敗!".mysqli_connect_error(); return; } $result = mysqli_query($conn
, "SELECT COUNT(1) FROM user_statis WHERE first_time >= $from_time AND first_time < $to_time"); $row=mysqli_fetch_row($result); $total = $row[0]; if ($display_all) { $strsql = "SELECT first_appid as AppId, (SELECT COUNT(1) FROM user_statis WHERE first_time >= $from_time AND first_time < $to_time
AND first_appid = AppId) AS Number FROM user_statis GROUP BY first_appid ORDER BY Number DESC"
; } else { $strsql = "SELECT first_appid as AppId, (SELECT COUNT(1) FROM user_statis WHERE first_time >= $from_time AND first_time < $to_time AND first_appid = AppId) AS Number FROM user_statis WHERE first_time >= $from_time AND first_time < $to_time GROUP BY first_appid ORDER BY Number DESC"; } //var_dump($conn); $result = mysqli_query($conn, $strsql); //var_dump($result); if (mysqli_num_rows($result) > 0) { echo '<table border="1" cellpadding="0" cellspacing="0">'; echo '<tr>'; echo '<th>Order</th>'; echo '<th>AppId</th>'; echo '<th>Number</th>'; echo '<th>Percent</th>'; echo '</tr>'; $index = 1; while ($row=mysqli_fetch_row($result)) { echo '<tr>'; echo '<td align="center">'; echo $index++; echo '</td>'; echo '<td bgcolor="#FFFFFF" align="center">'; if ($row[0] == '') { echo '無'; } else { echo $row[0]; } echo '</td>'; echo '<td bgcolor="#FFFFFF" align="center">'; echo $row[1]; echo '</td>'; echo '<td bgcolor="#FFFFFF" align="center">'; $percentage = $total == 0 ? 0 : $row[1]/$total; echo sprintf("%.2f%%",$percentage * 100); echo '</td>'; echo '</tr>'; } echo '</table>'; echo '<p>總計 '.$total.'</p>'; echo '查詢完成, 操作時間 '.date("Y-m-d H:i:s",time()); } else { echo '記錄為空!'; } mysqli_free_result($result); mysqli_close($conn);
<!DOCTYPE html>
<html lang="zh-CN">
	<head>		
    	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    	<meta name="viewport" content="width=device-width, initial-scale=1">
    	<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
    	<!--<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
		<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>-->
		<title>新增查詢</title>		
	</head>
	<body>
	 	<div class="container">
			  <div class="jumbotron">
			    <h2>新增查詢</h2>
			    <p>時間跨度越大耗時越久,提交之後請耐心等待...</p>
			    <form name="query_newbie" action="query.php" method="get" target="iframe_query">
			    	<div class="col">
			    		<p>
				    		<label>開始</label>
							<input name="from" type="date" id="fromDate"/>
							<script>
				    		document.getElementById('fromDate').valueAsDate=new Date();
							</script>				
			    		</p>
			    	</div>
					<div class="col">
						<p>
							<label>截止</label>
							<input name="to" type="date" id="toDate"/>
							<script>
				    		document.getElementById('toDate').valueAsDate=new Date();    		
							</script>				
						</p>
					</div>
					<div class="col">
						<p>
							<input type="checkbox" name="dispall" />
							<label>是否顯示所有AppId</label>
							<input type="submit" value="查詢"/>
						</p>
					</div>		
					<div class="col">
						<p>
					    	<iframe id="iframe_query" name="iframe_query" width="100%" scrolling="auto" onload="this.height=iframe_query.document.body.scrollHeight" frameborder="0" >
					    	</iframe>
				    	</p>
			    	</div>
			  </div>			  
		</div>	
		
	</body>
</html>