PHP+Mysql+jQuery+fullcalendar实现的微型公司内部订餐系统

需求相关: 公司20人左右, 每天中午, 下午两次订餐, 都是助理在群里说"开始订餐", 然后大家报菜名, 然后助理统计, 打电话.  今天, 助理说"开始订餐, 订什么密聊...", 突然就想做这么个东西....

 

耗时: 4.5小时

 

评估: 时间太短, 不能做到很好, 没有什么输入验证之类, 就自己公司内部一点人, 用用应该没啥问题.....bug估计很多...应该是没时间改bug的, 不过有不足之处请大家提出, 互相学习.

 

提醒: 由于我这边和服务器有个时差, 所以, 里面有代码在倒时差.....另外, 13点之前被认为是上午, 之后包括13点被认为是下午, 一天两次.

 

本文涉及到的插件: jQuery , jQuery.UI , fullcalendar

 

数据库创建:

CREATE TABLE USER

(

    ID INT PRIMARY KEY AUTO_INCREMENT,

    NAME VARCHAR(100)

);

CREATE TABLE ITEM

(

    ID INT PRIMARY KEY AUTO_INCREMENT,

    USER_ID INT,

    FOOD TINYBOLB,

    ORDER_TIME TIMESTAMP

);

ALTER TABLE ITEM ADD FOREIGN KEY(USER_ID) REFERENCES USER(ID) ON DELETE CASCADE;

 

一个实体的基类, 封装的不太好...呵呵

$method($value); } public function get() { $method = 'get'.ucwords($field); return $this->$method(); } } ?>

 

用户类(这个里面是没有权限, 登录的概念的, 就公司内部一个小东西)

id = $id; $this->name = $name; } public function getId() { return $this->id; } public function setId($id) { $this->id = $id; return $this; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; return $this; } } ?>

 

订餐订单项类

id = $id; $this->food = $food; $this->orderTime = $orderTime; } public function getId() { return $this->id; } public function setId($id) { $this->id = $id; return $this; } public function getUser() { return $this->user; } public function setUser($user) { $this->user = $user; return $this; } public function getFood() { return $this->food; } public function setFood($food) { $this->food = $food; return $this; } public function getOrderTime() { return $this->orderTime; } public function setOrderTime($orderTime) { $this->orderTime = $orderTime; return $this; } } ?>

 

数据库工具类

host = $host; $this->username = $username; $this->password = $password; $this->db_name = $db_name; $this->character_set = $character_set; } public function getConn() { $conn = mysql_connect($this->host, $this->username, $this->password) or die('数据库连接失败'); mysql_select_db($this->db_name, $conn); mysql_set_charset($this->character_set, $conn); return $conn; } /** * * @param $sql 查询用的sql * @param $result 查询得到的结果集, 如果UPDATE, INSERT, DELETE 不同提供这个参数 * @return SELECT 返回查询得到的条数, UPDATE, INSERT, DELETE返回bool型, 标识是否执行成功. */ public function execute($sql) { $conn = $this->getConn(); $resultset = mysql_query($sql, $conn) or die(mysql_error($conn)); if(!is_bool($resultset)) { while($line = mysql_fetch_assoc($resultset)) { $result[] = $line; } } else { $result = $resultset; } mysql_close($conn) or die(mysql_error($conn)); // echo "【".$sql."】 excute success!
"; return $result; } } ?>

 

业务逻辑(没时间了就写一个文件里了)

execute("SELECT * FROM USER"); foreach($users_data as $index => $data) { $user = new User(); foreach($data as $column_name => $value) { $user->set(User::getField($column_name), $value); } $users[] = $user; } return $users; } /** * 根据名字增加一个用户 * @param $name * @return unknown_type */ function addUser($name) { $util = new Mysql(); $name = iconv("UTF-8", "gbk" , $name); return $util->execute("INSERT INTO USER VALUES(NULL, '$name')"); } /** * 根据id删除一个用户 * @param $id * @return unknown_type */ function deleteUser($id) { $util = new Mysql(); return $util->execute("DELETE FROM USER WHERE ID = $id"); } /** * 根据id修改姓名 * @param $id * @param $name * @return unknown_type */ function updateUser($id, $name) { $util = new Mysql(); return $util->execute("UPDATE USER SET NAME = '$name' WHERE ID = $id"); } /** * 插入一条订餐记录 * @param $user_id * @param $food * @return unknown_type */ function addItem($user_id, $food, $ordertime) { $util = new Mysql(); $year = date('Y', $ordertime); $month = date('n', $ordertime); $day = date('j', $ordertime); $hour = (date('G', $ordertime) + 8) % 24; $flag = $hour < 13; $item = getItemByTimeAndUser($year, $month, $day, $flag, $user_id); $ordertime = "$year-$month-$day $hour:00:00"; $item && ($item = $item[0]) && ($flag = $flag ? ' < ' : ' >= '); return $item ? $util->execute("UPDATE ITEM SET FOOD = '$food' WHERE USER_ID = $user_id AND YEAR(ORDER_TIME) = $year AND MONTH(ORDER_TIME) = $month AND DAY(ORDER_TIME) = $day AND HOUR(ORDER_TIME) $flag 13") : $util->execute("INSERT INTO ITEM(USER_ID, FOOD, ORDER_TIME) VALUES($user_id, '$food', '$ordertime')"); } /** * 获取某天上午或下午的订餐 * @param $year * @param $month * @param $day * @param $flag 上午true下午false * @return unknown_type */ function getItemsByTime($year, $month, $day, $flag) { $util = new Mysql(); $flag = $flag ? ' < ' : ' >= '; $item_datas = $util->execute("SELECT ITEM.*, USER.NAME FROM ITEM JOIN USER ON ITEM.USER_ID = USER.ID WHERE YEAR(ORDER_TIME) = $year AND MONTH(ORDER_TIME) = $month AND DAY(ORDER_TIME) = $day AND HOUR(ORDER_TIME) $flag 13"); if(empty($item_datas)) return array(); foreach($item_datas as $index => $data) { $item = new Item(); $items[$data['USER_ID']] = $item->setId($data['ID']) ->setUser(new User($data['USER_ID'], $data['NAME'])) ->setFood($data['FOOD']) ->setOrderTime(date($data['ORDER_TIME'])); } return $items; } /** * 根据用户id, 时间, 获取其订饭记录 * @param $year * @param $month * @param $day * @param $flag * @param $user_id * @return unknown_type */ function getItemByTimeAndUser($year, $month, $day, $flag, $user_id) { $util = new Mysql(); $flag = $flag ? ' < ' : ' >= '; $item_datas = $util->execute("SELECT ITEM.*, USER.NAME FROM ITEM JOIN USER ON ITEM.USER_ID = USER.ID WHERE YEAR(ORDER_TIME) = $year AND MONTH(ORDER_TIME) = $month AND DAY(ORDER_TIME) = $day AND HOUR(ORDER_TIME) $flag 13 AND ITEM.USER_ID = $user_id"); echo "SELECT ITEM.*, USER.NAME FROM ITEM JOIN USER ON ITEM.USER_ID = USER.ID WHERE YEAR(ORDER_TIME) = $year AND MONTH(ORDER_TIME) = $month AND DAY(ORDER_TIME) = $day AND HOUR(ORDER_TIME) $flag 13 AND ITEM.USER_ID = $user_id"; if(empty($item_datas)) return array(); foreach($item_datas as $index => $data) { $item = new Item(); $items[] = $item->setId($data['ID']) ->setUser(new User($data['USER_ID'], $data['NAME'])) ->setFood($data['FOOD']) ->setOrderTime(date($data['ORDER_TIME'])); } return $items; } ?>

 

单元测试

execute('SHOW TABLES'); //print_r($res); //echo '
==========数据库连接成功===========
'; /** * 测试用例2: 获取所有用户 */ //var_dump(getUsers()); /** * 测试用例3: 增加用户 */ //var_dump(addUser("张三")); /** * 测试用例4: 删除用户 */ //var_dump(deleteUser(4)); /** * 测试用例5: 更新用户 */ //var_dump(updateUser(5, '李四')); /** * 测试用例6: 插入订餐项 */ //var_dump(addItem(1, '辣子鸡丁')); /** * 测试用例7: 获取某天上午或下午订餐 */ //var_dump(getItemsByTime(2010, 6, 4, false)); /** * 测试用例8: 获取某人某天上午或下午订餐 */ //var_dump(getItemByTimeAndUser(date('Y'), date('m'), date('d'), date('H') + 8 < 13, 1)); ?>

 

添加用户的Ajax接口

 

删除用户的ajax接口

 

提交订餐项的ajax接口

 

展现订餐项的视图:

$user) { if($index % 2 !== 0) continue; ?>
谁填谁的, 填完"吃啥"点下后面的"就吃这个"
名字 吃啥 操作 分水岭 名字 吃啥 操作
getName()); ?>      -|分|-      getName()); else echo ' '; ?>

 

展现所有用户的视图

$user) { ?>

 

系统的入口index.php

 

系统涉及的javascript: eat.js

$(document).ready(function() { /** * 计算一月有多少天 */ function daysInMonth(month,year) { var dd = new Date(year, month, 0); return dd.getDate(); } /** * 预置日历初始化使用日期, 当天 */ var date = new Date(); var d = date.getDate(); var m = date.getMonth(); var y = date.getFullYear(); /** * 初始化日历中每天里面的上下午日程 */ var day_nums = daysInMonth(m, y); var events = new Array(); for(var i = 1; i < day_nums; i ++) { events.push({ title: '上午吃啥', start: new Date(y, m, i, 1), end: new Date(y, m, i, 11) }); events.push({ title: '下午吃啥', start: new Date(y, m, i, 14), end: new Date(y, m, i, 23) }); } /** * 初始化日历 */ $('#calendar').fullCalendar({ year: y, month: m, day: d, defaultView: 'month', events: events, header: {left: '', center: 'title', right: ''}, aspectRatio: 1.6, weekMode: 'variable', eventClick: function(calEvent, jsEvent, view) { /** * 日程被点击后, 加载该时段的订餐视图 */ $('

').load('./viewItems.php?t=' + (calEvent.start / 1000), function() { var self= this; /** * 订餐的提交 */ $('.ctrl-submit', this).bind('click', function(event) { var record = $(this).parents('tr.ctrl-record').get(0); var userId = $(record).attr('userId'); var food = $('.ctrl-food-input', record).val(); $(self).dialog('close'); $.ajax({ type: 'POST', url: './submitItem.php', data:'userId=' + userId + '&food=' + food + '&t=' + (calEvent.start / 1000), async: false, success: function(res) { $('
').text('嘿嘿嘿, 吃好喝好啊.').dialog({ resizable: false, modal: true, title: '订了, 该干啥干啥吧.', width: 600, close: function() { $(this).dialog('destroy').remove(); }, buttons: { '就是关的时候方便点1': function() { $(this).dialog('close'); }, '就是关的时候方便点2': function() { $(this).dialog('close'); }, '就是关的时候方便点3': function() { $(this).dialog('close'); } } }); } }); }); /** * 把加载到的订餐视图预置为jqueryUI.dialog */ var t = new Date(calEvent.start); $(this).css('margin', '0 auto').dialog({ resizable: false, modal: true, title: '[' + t.getFullYear() + '年' + (t.getMonth() + 1) + '月' + t.getDay() + '日' + (t.getHours() > 13 ? '下午' : '上午') + ']----你想吃啥啊?', width: 1000, close: function() { $(this).dialog('destroy').remove(); }, buttons: { '就是关的时候方便点1': function() { $(this).dialog('close'); }, '就是关的时候方便点2': function() { $(this).dialog('close'); }, '就是关的时候方便点3': function() { $(this).dialog('close'); } } }); }); } }); /** * 修改当天的DOM, 加明显标记 */ $('.fc-today').append('
这里是今天
'); /** * 加人按钮, 用来加载用户管理界面 */ $('#maintain').bind('click', function(event) { $('
').load('./viewUsers.php', function() { $(this).dialog({ resizable: false, modal: true, title: '嘿嘿, 吃着喝着啊', width: 1128, height: 450, close: function() { $(this).dialog('destroy').remove(); }, buttons: { '就是关的时候方便点1': function() { $(this).dialog('close'); }, '就是关的时候方便点2': function() { $(this).dialog('close'); }, '就是关的时候方便点3': function() { $(this).dialog('close'); }, '就是关的时候方便点4': function() { $(this).dialog('close'); } } }); bindMaitainArea(this); }); }); /** * 用户管理界面加载完毕之后, 对其上的按钮绑定事件 */ var bindMaitainArea = (function(context) { /** * 新增用户的事件 */ $('#new_user', context).bind('click', function(event) { var self = this; $.ajax({ type: 'POST', url: './addUser.php', data:'name=' + $('#new_user_input').val(), timeout:8000, async: false, success: function(res) { $(self).parents('.ui-dialog-content').load('./viewUsers.php', function() { bindMaitainArea(); }); } }); }); /** * 删除用户的事件 */ $('.ctrl-del-user', context).bind('click', function(event) { var self = this; $.ajax({ type: 'POST', url: './delUser.php', data:'id=' + $(self).parents('.ctrl-record').attr('userId'), timeout:8000, async: false, success: function(res) { $(self).parents('.ui-dialog-content').load('./viewUsers.php', function() { bindMaitainArea(); }); } }); }); }); });

 

系统涉及的自定义样式eat.css

@CHARSET "UTF-8"; body{font-size: 75%;} .wrapper{width: 1024px; margin: 0 auto;} .food-input{width: 300px; border: 1px solid #E0E0E0;} table{border-left: 1px solid #D0D0D0; border-top: 1px solid #D0D0D0;} th, td{border-right: 1px solid #D0D0D0; border-bottom: 1px solid #D0D0D0;}

 

 

 

 

你可能感兴趣的:(PHP,javascript,html,CSS,Mysql,mysql,jquery,php,function,user,date)