一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2022-06-24 19:12:47 编辑:袖梨 来源:一聚教程网

本文涉及到的插件: 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;


一个实体的基类, 封装的不太好: Entity.php教程

PHP code

 

/**
 * 实体基类
 * @author selfimpr
 * @blog http://blog.111com.net/lgg201
 * @email [email protected]
 *
 */
class Entity {
    protected static $_mapping = array();
    public static function getField($column_name) {
        return self::$_mapping[$column_name] ? self::$_mapping[$column_name] : $column_name;
    }
    public function set($field, $value) {
        $method = 'set'.ucwords($field);
        return $this->$method($value);
    }
    public function get() {
        $method = 'get'.ucwords($field);
        return $this->$method();
    }
}
?>

 

 


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

PHP code
require_once './Entity.php';

/**
 * 用户实体类
 * @author selfimpr
 * @blog http://blog.111com.net/lgg201
 * @email [email protected]
 *
 */
class User extends Entity {
    private $id;
    private $name;
   
    public function User($id = NULL, $name = NULL) {
        $this->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;
    }
}
?>

 


订餐订单项类: Item.php

PHP code
require_once './Entity.php';
/**
 * 订餐订单
 * @author selfimpr
 * @blog http://blog.111com.net/lgg201
 * @email [email protected]
 *
 */
class Item extends Entity {
    private $id;
    private $user;
    private $food;
    private $orderTime;
   
    public function Item($id = NULL, $food = NULL, $orderTime = NULL) {
        $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;
    }
}
?>

 

 

 


数据库工具类: mysql教程_util.php

PHP code
/**
 * Mysql工具类
 * @author selfimpr
 * @blog http://blog.111com.net/lgg201
 * @email [email protected]
 *
 */
class Mysql {
    private $host;
    private $username;
    private $password;
    private $db_name;
    private $character_set;
   
    public function __construct(
            $host = "192.168.2.10",
            $username = "root",
            $password = "jhx",
            $db_name = "eat",
            $character_set = "GBK") {
        $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;
    }

}
?>

 

 

 

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

 

PHP code
require_once './mysql_util.php';
require_once './User.php';
require_once './Item.php';

/**
 * 获取当前库中所有的用户
 * @return unknown_type
 */
function getUsers() {
    $util = new Mysql();
    $users_data = $util->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;
}
?>

 


单元测试: test.unit.php

PHP code

require_once './mysql_util.php';
require_once './bussiness.php';

/**
 * 本程序业务逻辑之下部分的单元测试
 * @author selfimpr
 * @blog http://blog.111com.net/lgg201
 * @email [email protected]
 */


/**
 * 测试用例1: 数据库连接
 */
//$util = new Mysql();
//$res = $util->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接口: addUser.php

PHP code
require_once './bussiness.php';

/**
 * 添加用户ajax请求
 */
return addUser($_POST['name']);
?>
删除用户的ajax接口: delUser.php

PHP code
require_once './bussiness.php';

/**
 * 删除用户ajax请求
 */
return deleteUser($_POST['id']);
?>

 

 

 


提交订餐项的ajax接口: submitItem.php

PHP code
require_once './bussiness.php';

$userId = $_POST['userId'];
$food = $_POST['food'];
$t = $_POST['t'];

/**
 * 提交一个订餐请求的ajax地址
 */
echo (string)addItem($userId, $food, $t);
?>

 


展现订餐项的视图: viewItems.php

PHP code
/**
 * 展现某个订餐时间的所有订餐项
 */
require_once './bussiness.php';
$t = $_GET['t'];
$year = date('Y', $t);
$month = date('n', $t);
$day = date('j', $t);
$hour = (date('G', $t) + 8) % 24;
$flag = $hour < 13;
$items = getItemsByTime($year, $month, $day, $flag);
$users = getUsers();
?>



   


   
   
   
   
   
   
   

foreach($users as $index => $user) {
    if($index % 2 !== 0) continue;
?>

   
   
   
   
   
   
   

}
?>
谁填谁的, 填完"吃啥"点下后面的"就吃这个"
名字吃啥操作分水岭名字吃啥操作
getName()); ?>     -|分|-     getName()); else echo ' '; ?>

 


展现所有用户的视图: viewUsers.php

PHP code
require_once './bussiness.php';

/**
 * 系统中用户的管理界面
 * @author selfimpr
 * @blog http://blog.111com.net/lgg201
 * @email [email protected]
 */

$users = getUsers();
?>

javascript教程: event.keyCode == 13 && $('#new_user').click();" />
if(empty($users)) return ;
?>


foreach($users as $index => $user) {
?>

   
   


}
?>

 


系统的入口: index.php

PHP code
/**
 * 程序入口
 */
?>
















 

 

 


系统涉及的javascript: eat.js

PHP code
$(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: ''},
        asp教程ectRatio: 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
PHP code
@CHARSET "UTF-8";
body{font-size: 75%;}
.wrapper{ margin: 0 auto;}

.food-input{ 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;}
?>