php小项目

<?php
// 引入Smarty核心文件
require_once "../smarty-3.1.40/libs/Smarty.class.php";

// 引入工具类
require_once "../common/DB.php";

//  初始化Smarty
$smarty = new Smarty();

// 自定义smarty左右边界符
$smarty->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."();");

<?php
// $dsn="mysql:host=127.0.0.1;port=3306;dbname=websystem;charset=utf8";
// $username="root";
// $password="root";

// try {
    
//     $pdo=new PDO($dsn,$username,$password);
// } catch (PDOException $e) {
//     //throw $th;
//     die("连接错误");
// }
class DB{
    //设置一个试例
   static private $_instance;
   private $_pdo;//初始化默认为null, boolen(false)
    //提供一个配置实例化pdo
    private $_config=[
        "dsn"=>"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;
    }
}


?>
<?php
require_once "../model/UserModel.php";
class UserController
{
    private $model;
    private $userurl='../../../webSystem/severmvc/controller/index.php?controller=User&method=index';
    public function __construct()
    {
        $this->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 "<pre>";
        // 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 "<script>alert('添加成功');window.location.href='$this->userurl'</script>";
        }else {
            # code...
            echo "<script>alert('添加失败');window.location.href='$this->userurl'</script>";
        }
    }
    public function editPage()
    {
        global $smarty;
        // 判断参数是否传入
        if (empty($_GET["id"])) {
            echo '<script>alert("缺少参数")</script>';
            return;
        }

        // 获取参数
        $id = $_GET['id'];
        // $usermodel = new UserModel();
        $user = $this->model->getUser($id);

        if (empty($user)) {
            echo "<script>alert('参数错误')</script>";
            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 "<script>alert('修改成功'); window.location.href='$this->userurl'</script>";
        } else {
            echo "<script>alert('修改失败'); window.location.href='$this->userurl'</script>";
        }
    }

    public function delete()
    {
        // 判断参数是否传入
        if (empty($_GET["id"])) {
            echo '<script>alert("缺少参数")</script>';
            return;
        }

        // 获取参数
        $id = $_GET['id'];

        $result = $this->model->deleteUser($id);

        // 处理结果
        if ($result > 0) {
            echo "<script>alert('删除成功'); window.location.href='$this->userurl'</script>";
        } else {
            echo "<script>alert('删除失败'); window.location.href='$this->userurl'</script>";
        }
    }
}
<?php
class UserModel
{
    public function getAllUser($itemBegin, $itemCount)
    {
        // limit i,n i为询结果的索引值(默认从0开始),n是查询结果返回的数量
        $sql2 = "select * from user limit $itemBegin,$itemCount";
        //定义数据库语句
        $sql = "select * from user";
        //执行数据建库语句
        // $stmt = $pdo->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;
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值