BaseController.class.php代码如下:
class BaseController{
function __construct(){
header("content-type:text/html;charset=utf-8");
}
function GotoUrl($msg,$url,$time){
//显示一定提示文字,然后,自动跳转
echo "$msg";
echo "返回";
echo "
页面将在{$time}秒之后自动跳转";
header("refresh:$time;url=$url");//自动跳转
}
}
?>
用户控制器类:
UserController.class.php代码如下
//header("content-type:text/html; charset=utf8");//设置输出的字符编码为utf8
require './UserModel.class.php';
require './ModelFactory.class.php';
require'./BaseController.class.php';
class UserController extends BaseController{
function detailAction(){
//获取一个用户的信息
$id=$_GET[id];
$obj=ModelFactory::M('UserModel');
$data=$obj->GetUserInfoById($id);
//显示到一个用户的视图中
include './userInfo.html';
}
function ShowFormAction(){
include './form_view.html';
}
function addUserAction(){
$username = $_POST['username'];
$password = $_POST['password'];
$age = $_POST['age'];
$edu = $_POST['edu'];
$aihao = $_POST['aihao'];//这是一个数组结果,类似这样:array('1','2','4');
$xingqu = array_sum($aihao); //求得数组的所有项的和;
//$xingqu =implode(",",$aihao);
$from = $_POST['from'];
$obj= ModelFactory::M('UserModel');
$result=$obj->InsertUser($username,$password,$age,$edu,$aihao,$xingqu,$from);
$this->GotoUrl("添加用户成功!","?",3);//实现3s自动跳转
//echo "添加用户成功!";
//echo "返回";
}
//删除用户的功能
function delAction(){
$id = $_GET['id'];
$obj = ModelFactory::M('UserModel');
$result = $obj->delUserById($id);
$this->GotoUrl("删除成功!","?",6);//实现6s自动跳转
//echo "删除成功!";
//echo "返回";
}
function IndexAction(){
//$obj_user = new UserModel(); //这一行使用下一行代替
$obj_user = ModelFactory::M('UserModel');
$data1 = $obj_user->GetAllUser(); //是一个二维数组
$data2 = $obj_user->GetUserCount(); //是一个数字
include './showAllUser_view.html';
}
//修改用户
function editAction(){
$id=$_GET['id'];
$obj_user=ModelFactory::M('UserModel');
$user=$obj_user-> GetUserInfoById($id);
$aihao=explode(",",$user['xingqu']);
include "./user_form_view.html";
}
function UpdateUserAction(){
$id=$_POST['id'];
$user_name = $_POST['username'];
$user_pass = $_POST['password'];
$age = $_POST['age'];
$edu = $_POST['edu'];
$aihao = $_POST['aihao'];//这是一个数组结果,类似这样:array('1','2','4');
//$xingqu = array_sum($aihao); //求得数组的所有项的和;
$xingqu =implode(",",$aihao);
$from = $_POST['from'];
$obj= ModelFactory::M('UserModel');
$result=$obj->UpdateUser($id,$user_name,$user_pass,$age,$edu,$aihao,$xingqu,$from);
$this->GotoUrl("修改用户成功!","?",6);//实现自动跳转
//echo "修改用户成功!";
//echo "返回";
}
function ShowAllProductAction(){
}
}
$ctrl=new UserController();
$act=!empty($_GET['act'])?$_GET['act']:"Index";
$action=$act."Action";
$ctrl->$action();
//实例化模型类,并从中获取2份数据:
/*if(!empty($_GET['act'])&&$_GET['act']=='detail'){
//获取一个用户的信息
$id=$_GET[id];
$obj=ModelFactory::M('UserModel');
$data=$obj->GetUserInfoById($id);
//显示到一个用户的视图中
include './userInfo.html';
}
else if(!empty($_GET['act']) && $_GET['act'] == 'ShowForm'){
include './form_view.html';
}
else if(!empty($_GET['act']) && $_GET['act'] == 'addUser'){
$username = $_POST['username'];
$password = $_POST['password'];
$age = $_POST['age'];
$edu = $_POST['edu'];
$aihao = $_POST['aihao'];//这是一个数组结果,类似这样:array('1','2','4');
$xingqu = array_sum($aihao); //求得数组的所有项的和;
//$xingqu =implode(",",$aihao);
$from = $_POST['from'];
$obj= ModelFactory::M('UserModel');
$result=$obj->InsertUser($username,$password,$age,$edu,$aihao,$xingqu,$from);
echo "添加用户成功!";
echo "返回";
}
else if(!empty($_GET['act']) && $_GET['act'] == 'del'){
$id = $_GET['id'];
$obj = ModelFactory::M('UserModel');
$result = $obj->delUserById($id);
echo "删除成功!";
echo "返回";
}else{
//$obj_user = new UserModel(); //这一行使用下一行代替
$obj_user = ModelFactory::M('UserModel');
$data1 = $obj_user->GetAllUser(); //是一个二维数组
$data2 = $obj_user->GetUserCount(); //是一个数字
//载入视图文件,以显示该2份数据:
include './showAllUser_view.html';}*/
?>