PHP 结合MYSQL简单的实现了Todo List 功能
具体功能如下:
1、增加、修改、删除及标记任务完成;
2、历时任务查看;
3、任务优先级 ;
效果图:
conn.php 数据库操作文件
1 <?php
2
3 function add($data) {
4 $sql = "insert into todo(content,level,status,op_date) values('".$data["content"]."','".$data["level"]."','0',current_date) ";
5 executeSql($sql);
6 }
7
8 function edit($data) {
9 $sql = "update todo set content='".$data["content"]."',level='".$data["level"]."' where id='".$data["id"]."'";
10 executeSql($sql);
11 }
12
13 function del($id) {
14 $sql = "delete from todo where id='$id' ";
15 executeSql($sql);
16 }
17
18 function complete($id) {
19 $sql = "update todo set status='1',complete_date=current_date where id='$id' ";
20 executeSql($sql);
21 }
22
23 function getById($id) {
24 $sql = "select * from todo where id='$id' ";
25 $result = executeSql($sql);
26 $num_results = $result->num_rows;
27
28 $data = array ();
29 for($i = 0; $i < $num_results; $i ++) {
30 $row = ( array ) $result->fetch_assoc ();
31 $data = $row;
32 break;
33 }
34
35 $result->free();
36 return $data;
37 }
38
39 function getList() {
40 $sql = "select * from todo where status='0' order by level asc";
41
42 $result = executeSql($sql);
43
44 $num_results = $result->num_rows;
45
46 $data = array ();
47 for($i = 0; $i < $num_results; $i ++) {
48 $row = ( array ) $result->fetch_assoc ();
49 array_push ( $data, $row );
50 }
51
52 $result->free();
53 return $data;
54 }
55
56 function getHasCompleteList() {
57 $sql = "select * from todo where status='1' order by complete_date asc";
58
59 $result = executeSql($sql);
60
61 $num_results = $result->num_rows;
62
63 $data = array ();
64 for($i = 0; $i < $num_results; $i ++) {
65 $row = ( array ) $result->fetch_assoc ();
66 array_push ( $data, $row );
67 }
68
69 $result->free();
70 return $data;
71 }
72
73 function executeSql($sql) {
74 $db = getMysqlConn ();
75 $result = $db->query ( $sql );
76 $db->close ();
77 return $result;
78 }
79
80 function getMysqlConn() {
81 $db = new mysqli ( "localhost", "root", "", "galaxia_platform" );
82 if (mysqli_connect_errno ()) {
83 echo "Error: connect mysql failed";
84 exit();
85 }
86 return $db;
87 }
88 ?>
2
3 function add($data) {
4 $sql = "insert into todo(content,level,status,op_date) values('".$data["content"]."','".$data["level"]."','0',current_date) ";
5 executeSql($sql);
6 }
7
8 function edit($data) {
9 $sql = "update todo set content='".$data["content"]."',level='".$data["level"]."' where id='".$data["id"]."'";
10 executeSql($sql);
11 }
12
13 function del($id) {
14 $sql = "delete from todo where id='$id' ";
15 executeSql($sql);
16 }
17
18 function complete($id) {
19 $sql = "update todo set status='1',complete_date=current_date where id='$id' ";
20 executeSql($sql);
21 }
22
23 function getById($id) {
24 $sql = "select * from todo where id='$id' ";
25 $result = executeSql($sql);
26 $num_results = $result->num_rows;
27
28 $data = array ();
29 for($i = 0; $i < $num_results; $i ++) {
30 $row = ( array ) $result->fetch_assoc ();
31 $data = $row;
32 break;
33 }
34
35 $result->free();
36 return $data;
37 }
38
39 function getList() {
40 $sql = "select * from todo where status='0' order by level asc";
41
42 $result = executeSql($sql);
43
44 $num_results = $result->num_rows;
45
46 $data = array ();
47 for($i = 0; $i < $num_results; $i ++) {
48 $row = ( array ) $result->fetch_assoc ();
49 array_push ( $data, $row );
50 }
51
52 $result->free();
53 return $data;
54 }
55
56 function getHasCompleteList() {
57 $sql = "select * from todo where status='1' order by complete_date asc";
58
59 $result = executeSql($sql);
60
61 $num_results = $result->num_rows;
62
63 $data = array ();
64 for($i = 0; $i < $num_results; $i ++) {
65 $row = ( array ) $result->fetch_assoc ();
66 array_push ( $data, $row );
67 }
68
69 $result->free();
70 return $data;
71 }
72
73 function executeSql($sql) {
74 $db = getMysqlConn ();
75 $result = $db->query ( $sql );
76 $db->close ();
77 return $result;
78 }
79
80 function getMysqlConn() {
81 $db = new mysqli ( "localhost", "root", "", "galaxia_platform" );
82 if (mysqli_connect_errno ()) {
83 echo "Error: connect mysql failed";
84 exit();
85 }
86 return $db;
87 }
88 ?>
index.php 主文件
2 <?php
3 include 'conn.php';
4
5 $op = "show";
6
7 if (isset($_REQUEST["op"])) {
8 $op = $_REQUEST["op"];
9 }
10
11 switch ($op) {
12 case "show" :
13 break;
14 case "add" :
15 showAddForm();
16 break;
17 case "edit" :
18 showEditForm();
19 break;
20 case "save" :
21 if (isset($_REQUEST["content"],$_REQUEST["level"])) {
22 $data["content"] = $_REQUEST["content"];
23 $data["level"] = $_REQUEST["level"];
24 add($data);
25 echo "add success ";
26 }
27 break;
28 case "update" :
29 if (isset($_REQUEST["id"],$_REQUEST["content"],$_REQUEST["level"])) {
30 $data["content"] = $_REQUEST["content"];
31 $data["level"] = $_REQUEST["level"];
32 $data["id"] = $_REQUEST["id"];
33 edit($data);
34 echo "edit success ";
35 }
36 break;
37 case "delete" :
38 if (isset($_REQUEST["id"])) {
39 $id = $_REQUEST["id"];
40 del($id);
41 echo "delete success ";
42 }
43 break;
44 case "complete" :
45 if (isset($_REQUEST["id"])) {
46 $id = $_REQUEST["id"];
47 complete($id);
48 echo "complete task success ";
49 }
50 break;
51 }
52 showTodoList();
53
54 if ($op == "showHasComplete") {
55 showHasCompleteTodoList();
56 }
57
58 ?>
59
60 <?php
61 function showTodoList() {
62 ?>
63 <caption><h3>Todo list</h3></caption>
64 <a href="index.php?op=add">add new Todo</a> / <a href="index.php?op=show">show Todo list</a> / <a href="index.php?op=showHasComplete">show all list</a> <br/>
65 <table border="1">
66 <thead>
67 <td> </td>
68 <td width="500">content</td>
69 <td> </td>
70 </thead>
71 <?php
72 $todos = getList();
73 foreach($todos as $todo) {
74 ?>
75 <tr>
76 <td><input type="checkbox" onclick="javascript:top.location='index.php?op=complete&id=<?php echo $todo['id']?>'"></td>
77 <td><?php echo $todo['content']?></td>
78 <td><a href="index.php?op=delete&id=<?php echo $todo['id']?>">delete</a>/<a
79 href="index.php?op=edit&id=<?php echo $todo['id']?>">edit</a></td>
80 </tr>
81 <?php
82 }
83 ?>
84 </table>
85 <?php
86 }
87 ?>
88
89 <?php
90 function showHasCompleteTodoList() {
91 ?>
92 <caption><h3>Has completed Todo list</h3></caption>
93 <table border="1">
94 <thead>
95 <td width="500">content</td>
96 <td>create date</td>
97 <td>complete date</td>
98 </thead>
99 <?php
100 $todos = getHasCompleteList();
101 foreach($todos as $todo) {
102 ?>
103 <tr>
104 <td><?php echo $todo['content']?></td>
105 <td><?php echo $todo['op_date']?></td>
106 <td><?php echo $todo['complete_date']?></td>
107 </tr>
108 <?php
109 }
110 ?>
111 </table>
112 <?php
113 }
114 ?>
115
116 <?php
117 function showAddForm() {
118 ?>
119 <form action="index.php?op=save" method="post">
120 content:<input type="text" name="content">
121 level:<select name="level">
122 <option vlaue="1">1</option>
123 <option vlaue="2">2</option>
124 <option vlaue="3">3</option>
125 </select>
126 <input type="submit" name="submit" value="add to list">
127 </form>
128 <?php
129 }
130 ?>
131
132 <?php
133 function showEditForm() {
134 ?>
135 <form action="index.php?op=update" method="post">
136 <?php
137 $todo = null;
138 if (isset($_REQUEST["id"])) {
139 $id = $_REQUEST["id"];
140 $todo = getById($id);
141 } else {
142 echo "id is empty !";
143 return;
144 }
145 ?>
146 <input type="hidden" name="id" value="<?php echo $todo['id']?>">
147 content:<input type="text" name="content" value="<?php echo $todo['content']?>">
148 level:<select name="level">
149 <option vlaue="1">1</option>
150 <option vlaue="2">2</option>
151 <option vlaue="3">3</option>
152 </select>
153 <input type="submit" name="submit" value="update">
154 <?php
155 ?>
156 </form>
157 <?php
158 }
159 ?>
3 include 'conn.php';
4
5 $op = "show";
6
7 if (isset($_REQUEST["op"])) {
8 $op = $_REQUEST["op"];
9 }
10
11 switch ($op) {
12 case "show" :
13 break;
14 case "add" :
15 showAddForm();
16 break;
17 case "edit" :
18 showEditForm();
19 break;
20 case "save" :
21 if (isset($_REQUEST["content"],$_REQUEST["level"])) {
22 $data["content"] = $_REQUEST["content"];
23 $data["level"] = $_REQUEST["level"];
24 add($data);
25 echo "add success ";
26 }
27 break;
28 case "update" :
29 if (isset($_REQUEST["id"],$_REQUEST["content"],$_REQUEST["level"])) {
30 $data["content"] = $_REQUEST["content"];
31 $data["level"] = $_REQUEST["level"];
32 $data["id"] = $_REQUEST["id"];
33 edit($data);
34 echo "edit success ";
35 }
36 break;
37 case "delete" :
38 if (isset($_REQUEST["id"])) {
39 $id = $_REQUEST["id"];
40 del($id);
41 echo "delete success ";
42 }
43 break;
44 case "complete" :
45 if (isset($_REQUEST["id"])) {
46 $id = $_REQUEST["id"];
47 complete($id);
48 echo "complete task success ";
49 }
50 break;
51 }
52 showTodoList();
53
54 if ($op == "showHasComplete") {
55 showHasCompleteTodoList();
56 }
57
58 ?>
59
60 <?php
61 function showTodoList() {
62 ?>
63 <caption><h3>Todo list</h3></caption>
64 <a href="index.php?op=add">add new Todo</a> / <a href="index.php?op=show">show Todo list</a> / <a href="index.php?op=showHasComplete">show all list</a> <br/>
65 <table border="1">
66 <thead>
67 <td> </td>
68 <td width="500">content</td>
69 <td> </td>
70 </thead>
71 <?php
72 $todos = getList();
73 foreach($todos as $todo) {
74 ?>
75 <tr>
76 <td><input type="checkbox" onclick="javascript:top.location='index.php?op=complete&id=<?php echo $todo['id']?>'"></td>
77 <td><?php echo $todo['content']?></td>
78 <td><a href="index.php?op=delete&id=<?php echo $todo['id']?>">delete</a>/<a
79 href="index.php?op=edit&id=<?php echo $todo['id']?>">edit</a></td>
80 </tr>
81 <?php
82 }
83 ?>
84 </table>
85 <?php
86 }
87 ?>
88
89 <?php
90 function showHasCompleteTodoList() {
91 ?>
92 <caption><h3>Has completed Todo list</h3></caption>
93 <table border="1">
94 <thead>
95 <td width="500">content</td>
96 <td>create date</td>
97 <td>complete date</td>
98 </thead>
99 <?php
100 $todos = getHasCompleteList();
101 foreach($todos as $todo) {
102 ?>
103 <tr>
104 <td><?php echo $todo['content']?></td>
105 <td><?php echo $todo['op_date']?></td>
106 <td><?php echo $todo['complete_date']?></td>
107 </tr>
108 <?php
109 }
110 ?>
111 </table>
112 <?php
113 }
114 ?>
115
116 <?php
117 function showAddForm() {
118 ?>
119 <form action="index.php?op=save" method="post">
120 content:<input type="text" name="content">
121 level:<select name="level">
122 <option vlaue="1">1</option>
123 <option vlaue="2">2</option>
124 <option vlaue="3">3</option>
125 </select>
126 <input type="submit" name="submit" value="add to list">
127 </form>
128 <?php
129 }
130 ?>
131
132 <?php
133 function showEditForm() {
134 ?>
135 <form action="index.php?op=update" method="post">
136 <?php
137 $todo = null;
138 if (isset($_REQUEST["id"])) {
139 $id = $_REQUEST["id"];
140 $todo = getById($id);
141 } else {
142 echo "id is empty !";
143 return;
144 }
145 ?>
146 <input type="hidden" name="id" value="<?php echo $todo['id']?>">
147 content:<input type="text" name="content" value="<?php echo $todo['content']?>">
148 level:<select name="level">
149 <option vlaue="1">1</option>
150 <option vlaue="2">2</option>
151 <option vlaue="3">3</option>
152 </select>
153 <input type="submit" name="submit" value="update">
154 <?php
155 ?>
156 </form>
157 <?php
158 }
159 ?>
SQL创建脚本:
2 CREATE TABLE IF NOT EXISTS `todo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
3 `content` varchar(500) NOT NULL,
4 `op_date` date NOT NULL,
5 `status` int(1) NOT NULL,
6 `complete_date` date NOT NULL,
7 `level` int(1) NOT NULL,
8 PRIMARY KEY (`id`)
9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;
`id` int(11) NOT NULL AUTO_INCREMENT,
3 `content` varchar(500) NOT NULL,
4 `op_date` date NOT NULL,
5 `status` int(1) NOT NULL,
6 `complete_date` date NOT NULL,
7 `level` int(1) NOT NULL,
8 PRIMARY KEY (`id`)
9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;
就花了一个多小时做的,只实现了基本功能,连注释也没来得及写,其实也就是CURD功能 :)