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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我给您介绍一下如何按照MVC开发模式实现一个简易的用户查询功能。 首先,我们需要了解MVC开发模式的基本概念: - Model(模型):负责数据的存储和业务逻辑的处理。 - View(视图):负责展示数据和与用户的交互。 - Controller(控制器):负责接收用户请求,调用模型处理数据,并将结果反馈给视图展示。 接下来,我们按照这个流程来实现用户查询功能。 1. 首先,我们需要创建一个用户数据模型,用于存储用户信息和处理相关业务逻辑。 ```php class UserModel { protected $users = [ ['id'=>1, 'name'=>'Tom', 'age'=>20], ['id'=>2, 'name'=>'Jerry', 'age'=>22], ['id'=>3, 'name'=>'Mike', 'age'=>19], ['id'=>4, 'name'=>'Lucy', 'age'=>21], ]; public function getUserById($id) { foreach($this->users as $user) { if($user['id'] == $id) { return $user; } } return null; } public function getUserByName($name) { foreach($this->users as $user) { if($user['name'] == $name) { return $user; } } return null; } } ``` 2. 接下来,我们需要创建一个用户视图来展示用户信息和接收用户输入。 ```php class UserView { public function render($user) { if($user) { echo "ID: ".$user['id']."<br>"; echo "Name: ".$user['name']."<br>"; echo "Age: ".$user['age']."<br>"; } else { echo "User not found."; } } public function getInput() { return isset($_GET['name']) ? $_GET['name'] : null; } } ``` 3. 最后,我们需要创建一个用户控制器来处理用户请求,调用模型处理数据,并将结果反馈给视图展示。 ```php class UserController { protected $model; protected $view; public function __construct(UserModel $model, UserView $view) { $this->model = $model; $this->view = $view; } public function show($id = null) { $user = $this->model->getUserById($id); $this->view->render($user); } public function search() { $name = $this->view->getInput(); $user = $this->model->getUserByName($name); $this->view->render($user); } } ``` 现在,我们已经完成了用户查询功能的MVC开发模式实现。我们可以通过以下代码来测试: ```php $model = new UserModel(); $view = new UserView(); $controller = new UserController($model, $view); // 查询 ID 为 1 的用户信息 $controller->show(1); // 查询名字为 Tom 的用户信息 $controller->search(); ``` 以上就是按照MVC开发模式实现一个简易的用户查询功能的过程和代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值