Skip to content

Commit 8d5430f

Browse files
committed
分层细化 增加了cookie限制
1 parent f69eda5 commit 8d5430f

27 files changed

+1134
-53
lines changed

newmatch/MatchController.class.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class MatchController{
4+
5+
public function listAction(){
6+
require('./matchModel.class.php');
7+
$model = new matchModel();
8+
$rows = $model -> getList();
9+
require('./template/match_view.html');
10+
}
11+
12+
public function delAction(){
13+
require('./matchModel.class.php');
14+
$model = new matchModel();
15+
$res = $model -> del($_GET['id']);
16+
header('Location: /newmatch/index.php');
17+
}
18+
19+
}

newmatch/Model.class.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
class Model{
3+
protected $db;
4+
5+
public function __construct(){
6+
$this->inintLink();
7+
}
8+
9+
/**
10+
* 初始化数据库连接
11+
*/
12+
13+
public function inintLink(){
14+
15+
require './MySql.class.php';
16+
17+
$params = array(
18+
'host' => '127.0.0.1',
19+
'port' => '3306',
20+
'user' => 'root',
21+
'pass' => 'root',
22+
'charset' => 'utf8',
23+
'dbname' => 'match'
24+
);
25+
26+
$this->db = MySql::getInstance(array('dbname'=>'match'));
27+
}
28+
}
29+
30+
31+

newmatch/MySql.class.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
4+
5+
class MySql{
6+
private $host;
7+
private $port;
8+
private $user;
9+
private $pass;
10+
private $charset;
11+
private $dbname;
12+
13+
private $link;
14+
private $last_sql;
15+
16+
private static $instance;
17+
18+
private function __construct($params=array()){
19+
20+
$this->host = isset($params['host'])?$params['host']:'127.0.0.1';
21+
$this->port = isset($params['port'])?$params['port']:'3306';
22+
$this->user = isset($params['user'])?$params['userr']:'root';
23+
$this->pass = isset($params['pass'])?$params['pass']:'root';
24+
$this->charset = isset($params['charset'])?$params['charset']:'utf8';
25+
$this->dbname = isset($params['dbname'])?$params['dbname']:'test';
26+
27+
$this->conn();
28+
29+
$this->setCharset();
30+
31+
$this->selectDB();
32+
33+
}
34+
35+
private function conn(){
36+
if (!$link = mysql_connect("$this->host:$this->port",$this->user,$this->pass)) {
37+
echo '连接失败';
38+
echo '<br>';
39+
echo mysql_errno();
40+
echo '<br />';
41+
echo mysql_error();
42+
die();
43+
}else{
44+
$this->link = $link;
45+
}
46+
}
47+
48+
49+
private function setCharset(){
50+
$sql = "set names $this->charset";
51+
return $this->query($sql);
52+
}
53+
54+
55+
56+
private function selectDB(){
57+
if($this->dbname === ''){
58+
return;
59+
}
60+
61+
$sql = "use `$this->dbname`";
62+
return $this->query($sql);
63+
}
64+
65+
public function query($sql){
66+
$this->last_sql = $sql;
67+
if(!$result = mysql_query($this->last_sql,$this->link)){
68+
echo 'sql执行失败<br />';
69+
echo '失败的sql是:'.$sql.'<br>';
70+
echo '错误代码:'.mysql_errno();
71+
echo '错误信息:'.mysql_error();
72+
die();
73+
}else{
74+
return $result;
75+
}
76+
}
77+
78+
public function fetchAll($sql){
79+
echo 'in fetchAll...';
80+
if ($result = $this->query($sql)) {
81+
if (is_resource($result)) {
82+
echo 'not Res..';
83+
84+
}
85+
$rows = array();
86+
while ($row = mysql_fetch_assoc($result)) {
87+
$rows[] = $row;
88+
}
89+
90+
mysql_free_result($result);
91+
return $rows;
92+
}else{
93+
return false;
94+
}
95+
}
96+
97+
public function fetchRow($sql){
98+
if ($result =$this->query($sql)) {
99+
$row = mysql_fetch_assoc($result);
100+
return $row;
101+
}else{
102+
return false;
103+
}
104+
}
105+
106+
public static function getInstance($params){
107+
if (!(self::$instance instanceof self)) {
108+
self::$instance = new self($params);
109+
}
110+
111+
return self::$instance;
112+
}
113+
114+
// public function __destruct(){
115+
// echo 'a';
116+
// mysql_free_result($this->link);
117+
// echo 'b';
118+
// }
119+
}
120+
121+
//var_dump(Mysql::getInstance(array()));

newmatch/classModel.class.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
require_once('./Model.class.php');
4+
5+
class ClassModel extends Model{
6+
7+
public function getList(){
8+
$sql = 'select count(select_student.stu_name) as count,select_class.class_name as classname from select_student left join select_class on select_student.class_id = select_class.id group by select_class.id';
9+
10+
//return $this->db->query($sql);
11+
return $this->db->fetchAll($sql);
12+
}
13+
14+
public function delClass($id){
15+
$sql = "delete from select_class where class_id=$id";
16+
return $this->db->query($sql);
17+
}
18+
}

newmatch/class_module.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
$action = isset($_GET['a']) ? $_GET['a'] : 'list';
3+
4+
if('list' === $action){
5+
6+
require('./classModel.class.php');
7+
$model = new ClassModel();
8+
$rows = $model -> getList();
9+
// var_dump(mysql_fetch_assoc($rows));
10+
require('./template/class_view.html');
11+
12+
}elseif('delmatch' === $action){
13+
14+
require('./classModel.class.php');
15+
$model = new ClassModel();
16+
$rows = $model->delMact();
17+
header(');
18+
19+
}elseif('viewmatch' === $action){
20+
21+
}

newmatch/index.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$c = isset($_GET['c'])?$_GET['c']:'Match';
4+
$controllerName = $c.'Controller';
5+
6+
require('./'.$controllerName.'.class.php');
7+
8+
$controller = new $controllerName;
9+
10+
$a = isset($_GET['a'])?$_GET['a']:'list';
11+
$action_name = $a.'Action';
12+
$controller->$action_name();
13+
14+

newmatch/matchModel.class.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
require_once('./Model.class.php');
4+
5+
class MatchModel extends Model{
6+
7+
public function getList(){
8+
$sql = 'select m.match_id as id,s1.stu_name as p1,s2.stu_name as p2,m.match_time as time,m.match_result as result from select_match m left join select_student s1 on s1.id=m.player_1 left join select_student s2 on s2.id = m.player_2';
9+
10+
return $this->db->fetchAll($sql);
11+
}
12+
13+
public function del($id){
14+
$sql = "delete from select_match where match_id=$id";
15+
return $this->db->query($sql);
16+
}
17+
}

newmatch/match_module.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
$action = isset($_GET['a'])?$_GET['a']:'list';
3+
4+
if('list' == $action){
5+
6+
require('./matchModel.class.php');
7+
$model = new matchModel();
8+
$rows = $model -> getList();
9+
require('./template/match_view.html');
10+
11+
}elseif('del' === $action){
12+
13+
require('./matchModel.class.php');
14+
$model = new matchModel();
15+
$res = $model -> del($_GET['id']);
16+
header('Location: /newmatch/match_module.php');
17+
}

newmatch/template/class_view.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2+
<!-- 显示部分,将php代码获得数据展示出来 -->
3+
<!-- 视图文件,模板充当-->
4+
<HTML>
5+
<HEAD>
6+
<TITLE> 班级列表 </TITLE>
7+
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" />
8+
</HEAD>
9+
<BODY>
10+
11+
<h1>比赛概况表</h1>
12+
<table>
13+
<tr>
14+
<th>班级</th>
15+
<th>参赛人数</th>
16+
</tr>
17+
<?php foreach($rows as $row) : ?>
18+
<tr>
19+
<td><?php echo $row['count'];?></td>
20+
<td><?php echo $row['classname'];?></td>
21+
</tr>
22+
<?php endForeach;?>
23+
</table>
24+
25+
</BODY>
26+
</HTML>

newmatch/template/match_view.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2+
<!-- 显示部分,将php代码获得数据展示出来 -->
3+
<!-- 视图文件,模板充当-->
4+
<HTML>
5+
<HEAD>
6+
<TITLE> 班级列表 </TITLE>
7+
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8" />
8+
</HEAD>
9+
<BODY>
10+
11+
<h1>比赛概况表</h1>
12+
<table>
13+
<tr>
14+
<th>选手1</th>
15+
<th>选手2</th>
16+
<th>比赛时间</th>
17+
<th>结果</th>
18+
</tr>
19+
<?php foreach($rows as $row) : ?>
20+
<tr>
21+
<td><?php echo $row['p1'];?></td>
22+
<td><?php echo $row['p2'];?></td>
23+
<td><?php echo $row['time'];?></td>
24+
<td><?php echo $row['result'];?></td>
25+
<td><a href="/newmatch/index.php?a=del&id=<?php echo $row['id'];?>">1删除</a></td>
26+
</tr>
27+
<?php endForeach;?>
28+
</table>
29+
30+
</BODY>
31+
</HTML>

0 commit comments

Comments
 (0)