codeIgniter3 学习笔记二(基于 MVC 开发模式实现小案例)

基于 MVC 模式开发模式 + Mysql + codeigniter3 实现小案例

在这里插入图片描述

application/models 创建数据模型 Dbmodel

class Dbmodel extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        // 加载数据模型
        $this->load->database();
    }
    /**
     * 获取所有数据
     */
    public function get_blog_list()
    {
        $res = $this->db->get("blog");
        return $res->result();
    }
}

application/controllers 创建控制层类 Blog

class Blog extends CI_Controller
{
    public  function index()
    {
        // 加载模型
        $this->load->model('dbmodel');
        // 获取数据
        $res['blogList'] = $this->dbmodel->get_blog_list();

        // 显示数据
        $this->load->view("blogview", $res);
    }
}

application/views 创建视图层 blogview

<html>
<head>
    <title></title>
    <style>
        .header {
            width: 1400px;
            height: 30px;
            margin: 120px auto 0px auto;
            display: flex;
            justify-content: end;
        }
        .header .search {
            width: 260px;
            position: relative;
            height: 30px;
            border: 1px solid rgba(135, 206, 235, .7);
            border-radius: 6px;
            overflow: hidden;
        }
        .header .search .btn {
            position: absolute;
            right: 0;
            bottom: 0;
            width: 60px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            color: #ffffff;
            background-color: rgba(135, 206, 235, .7);
            border-left: 1px solid rgba(135, 206, 235, .7);
        }
        .header .search input {
            height: 30px;
            width: 200px;
            border: none;
            padding-left: 6px;
            margin: 0;
            outline: none;
        }
        .header .btn-group {
            display: flex;
            justify-content: end;
            margin-left: 40px;
        }
        .header .btn-group .btn {
            width: 60px;
            line-height: 32px;
            height: 32px;
            text-align: center;
            background-color: rgba(135, 206, 235, .7);
            color: #ffffff;
            border-radius: 6px;
            margin-right: 12px;
        }
        .header .btn-group .btn:last-child {
            margin-right: 0px;
        }

        .header .btn:hover {
            cursor: pointer;
            background-color: skyblue;
        }
        table {
            width: 1400px;
            margin:  12px auto;
        }
        table thead tr {
           background-color: skyblue;
           color: #fff;
           text-align: center;
        }
        table tbody tr td {
            padding: 6px 3px;
        }
        table tbody tr:hover {
            background-color: rgba(135, 206, 235, .7);
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="header">
   <div class="search">
       <input type="text">
       <div class="btn">搜索</div>
   </div>
   <div class="btn-group">
       <div class="btn">添加</div>
       <div class="btn">刷新</div>
   </div>
</div>
<table cellpadding="0" cellspacing="0" border="1" bordercolor="#ccc">
   <thead>
        <tr>
          <td>序号</td>
          <td>标题</td>
          <td>内容</td>
          <td>用户ID</td>
        </tr>
   </thead>
   <tbody>
       <?php foreach ($blogList as $item): ?>
           <tr>
               <td><?= $item->id ;?></td>
               <td><?= $item->title ;?></td>
               <td><?= $item->content ;?></td>
               <td><?= $item->userId ;?></td>
           </tr>
       <?php endforeach;?>
   </tbody>
</table>
</body>
</html>

测试访问地址:http://192.168.5.41/blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小美满

您的鼓励将是我前进的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值