left_delimiter = "<{";
$smarty->right_delimiter = "}>";
// 初始化数据库
$pdo = DB::getInstance()->connect();
// 获取用户希望访问哪一个controller
$controller = isset($_GET['controller']) ? $_GET['controller'] : 'Login';
// var_dump($controller);
// 获取用户希望访问的方法
$method = isset($_GET['method']) ? $_GET['method'] : 'index';
// 引入对应的Controller类文件
require_once "./{$controller}Controller.php";
// 实例化该Controller对象
eval('$obj = new '.$controller."Controller();");
//调用用户传递方法名称
eval('$obj->'.$method."();");
"mysql:host=127.0.0.1;port=3306;dbname=websystem;charset=utf8",
"username"=>"root",
"password"=>"root",
"options" => [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC
]
];
//私有化构造函数 只能实例化一个对象
private function __construct()
{
}
//获取DB类的实例
static public function getInstance(){
//判断$_instance有没有值 如果没有值就实例化一个对象,并且返回该对象,为之后通过该对象调用其他方法提供一句
if (!self::$_instance instanceof self) {
# code...
self::$_instance=new self();
}
return self::$_instance;
}
function connect(){
//判断$_pdo是否有值
if (!$this->_pdo) {
# code...
$this->_pdo=new PDO($this->_config["dsn"],$this->_config["username"],$this->_config["password"],$this->_config["options"]);
}
return $this->_pdo;
}
}
?>
model=new UserModel();
}
public function index()
{
global $smarty;
//获取传入的页码及每页条数,如果没有则设置默认值
$curr = empty($_GET["curr"]) ? 1 : $_GET["curr"];
$itemCount = empty($_GET["itemCount"]) ? 5 : $_GET["itemCount"];
// 计算取值开始的条数
$itemBegin = ($curr - 1) * $itemCount;
// $usermodel = new UserModel();
$result = $this->model->getAllUser($itemBegin, $itemCount);
$count=$result['count'];
foreach ($result['count'] as &$data) {
# code...
switch ($data["type"]) {
case 1:
# code...
$data["typename"] = "学生";
break;
case 2:
# code...
$data["typename"] = "老师";
break;
case 3:
# code...
$data["typename"] = "校长";
break;
default:
# code...
break;
}
//处理create_time 数据
$data["ctime"] = date('Y-m-d:H:i:s', $data["create_time"]);
}
// echo "";
// var_dump($result['count']);
$smarty->assign("users", $result['users']);
$smarty->assign("curr", $curr);
$smarty->assign("count", $result['count']);
$smarty->display("../view/userlist.html");
}
public function addPage()
{
global $smarty;
$smarty->display("../view/adduser.html");
}
public function add()
{
//获取adduser传入的数据
$name = $_POST["name"];
$age = $_POST["age"];
$birthday = $_POST["birthday"];
$type = $_POST["type"];
$icon = $_POST["icon"];
$create_time = time();
// $usermodel=new UserModel();
$result=$this->model->adduser($name,$age,$birthday,$type,$icon,$create_time);
if ($result>0) {
# code...
echo "";
}else {
# code...
echo "";
}
}
public function editPage()
{
global $smarty;
// 判断参数是否传入
if (empty($_GET["id"])) {
echo '';
return;
}
// 获取参数
$id = $_GET['id'];
// $usermodel = new UserModel();
$user = $this->model->getUser($id);
if (empty($user)) {
echo "";
return;
}
$smarty->assign('count',$user);
$smarty->display('../view/reditlist.html');
}
public function edit()
{
// 获取参数
$id = $_POST["id"];
$name = $_POST["name"];
$age = $_POST["age"];
$birthday = $_POST["birthday"];
$type = $_POST["type"];
$icon = $_POST["icon"];
$result = $this->model->editUser($name,$age,$birthday,$type,$icon,$id);
// 处理结果
if ($result > 0) {
echo "";
} else {
echo "";
}
}
public function delete()
{
// 判断参数是否传入
if (empty($_GET["id"])) {
echo '';
return;
}
// 获取参数
$id = $_GET['id'];
$result = $this->model->deleteUser($id);
// 处理结果
if ($result > 0) {
echo "";
} else {
echo "";
}
}
}
query($sql);
// $stmt2 = $pdo->query($sql2);
$stmt = DB::getInstance()->connect()->query($sql);
$stmt2 = DB::getInstance()->connect()->query($sql2);
//处理结果及
// 统计结果集个数,给列表分页模块使用
$count = $stmt->rowCount();
// $user = $stmt->fetchAll();
$result = $stmt2->fetchAll();
return ["users" => $count, "count" => $result];
}
public function adduser($name, $age, $birthday, $type, $icon, $create_time)
{
global $pdo;
//定义sql语句
$sql = "insert into user (name,type,icon,age,birthday,create_time) values ('$name','$type','$icon','$age','$birthday',$create_time)";
//执行sql
$result=$pdo->exec($sql);
return $result;
}
public function deleteUser($id)
{
global $pdo;
// 定义sql语句
$sql = "delete from user where id = $id";
// 执行sql
$result = $pdo->exec($sql);
return $result;
}
public function getUser($id)
{
# code...
global $pdo;
// 定义sql语句
$sql = "select * from user where id = $id";
// 执行sql
$result = $pdo->query($sql);
return $result->fetch();
}
public function editUser($name, $age, $birthday, $type, $icon, $id)
{
global $pdo;
// 定义sql语句
$sql = "update user set name = '$name', age = $age, birthday = '$birthday', type = $type, icon = '$icon' where id = $id";
// 执行sql语句获取结果集
$result = $pdo->exec($sql);
return $result;
}
//编辑新闻
public function editNews($name, $age, $birthday, $type, $icon, $id)
{
global $pdo;
// 定义sql语句
$sql = "update user set name = '$name', age = $age, birthday = '$birthday', type = $type, icon = '$icon' where id = $id";
// 执行sql语句获取结果集
$result = $pdo->exec($sql);
return $result;
}
}