CodeIgniter(1)简单入门---使用mvc来完成对新闻的一组操作----增删改查

来源:互联网 发布:北国知春物业 编辑:程序博客网 时间:2024/04/30 04:43
CodeIgniter(1)简单入门---使用mvc来完成对新闻的一组操作----增删改查

1.下载CodeIgniter框架
    进入官网http://codeigniter.org.cn/,下载相应的CodeIgniter代码,如图1.

图1
  将源码包解压到你的网站目录下,我的本地集成环境使用phpstudy,解压到www文件夹下,改名为ci,然后在浏览器中访问localhost/citest,显示如下页面,则表示安装成功,如图2.

图2
   文件夹介绍


2.创建数据表
   在数据库test中创建数据表news
  1. create database test;
  2. use test;
  3. create table news(
  4.     id int unsigned not null primary key auto_increment,
  5.     title varchar(50) not null default '',
  6.     author varchar(30) not null default '',
  7.     content text,
  8.     add_time int unsigned not null default 0
  9. )engine=myisam charset=utf8;

3.数据库配置,如图3.

   

图3


4.编写代码--模型

   文件位置application/models/News_model.php

    defined('BASEPATH') OR exit('No direct script access allowed');
    class News_model extends CI_Model{
        const TBL='news';

        //构造函数
        public function __construct(){
            //调用父类构造函数,必不可少
            parent::__construct();
            //手动载入数据库操作类
            $this->load->database();
        }
        /*
            @access public
            @param $data array
            @return bool 成功返回true,失败返回false
         */
        public function add_news($data){
            //使用AR类完成插入操作
            return $this->db->insert(self::TBL,$data);
        }

        /*
            @access public
            @return array 查询结果

         */
        public function list_news(){
            $query=$this->db->get(self::TBL);
            return $query->result_array();
        }

        /*
            @access public
            @return int 成功返回受影响的条数

         */
        public function delete_news($id){
            return $this->db->delete(self::TBL,array('id'=>$id));
        }

        /*
            @access public
            @return array修改查询的结果

         */
        public function get_news($id){
            $query=$this->db->get_where("news",array('id'=>$id));
            return $query->result_array();
        }

        /*
            @access public
            @return bool 修改成功返回true

         */
        public function update_news($id,$data){
            return $this->db->update('news',$data,array('id'=>$id));
        }
    }

5.编写代码--控制器

   文件位置application/controllers/news.php

    defined('BASEPATH') OR exit("No direct script access allowed");
    class News extends CI_Controller{
        public function __construct(){
            parent::__construct();
            #载入news——Model,载入之后可以使用$this->user_model来操作
            $this->load->model("news_model");

        }

        //显示添加新闻表单
        public function add(){
            $this->load->view("news/add.html");
        }

        //完成新闻的添加
        public function insert(){
            #获取表单提交的数据
            $data['title']=$_POST['title'];
            $data['author']=$_POST['author'];
            $data['content']=$_POST['content'];
            $data['add_time']=time();
            #调用news_model的方法
            if($this->news_model->add_news($data)){
                echo "添加成功<a href='".site_url('news')."'>返回首页</a>";
            }else{
                echo "添加失败<a href='".site_url('news')."'>返回首页</a>";
            }
        }

        // 显示新闻列表
        public function index(){
            #调用list_news方法得到数据
            $data['news']=$this->news_model->list_news();
            #分配到视图
            $this->load->view("news/list.html",$data);
        }

        //删除新闻
        public function delete($id){
            if($this->news_model->delete_news($id)){
                echo "删除成功<a href='".site_url('news')."'>返回首页</a>";
            }else{
                echo "删除失败<a href='".site_url('news')."'>返回首页</a>";
            }
        }
        //修改新闻,显示新闻详情页
        public function edit($id){
            $data['new']=$this->news_model->get_news($id);
            $data['new']=$data['new'][0];
            $this->load->view("news/edit.html",$data);
        }
        //完成新闻的修改
        public function update(){
            #获取表单提交的数据
            $id=$_POST['id'];
            $data['title']=$_POST['title'];
            $data['author']=$_POST['author'];
            $data['content']=$_POST['content'];
            #调用news_model的方法
            if($this->news_model->update_news($id,$data)){
                echo "修改成功<a href='".site_url('news')."'>返回首页</a>";
            }else{
                echo "修改失败<a href='".site_url('news')."'>返回首页</a>";
            }
        }

    }


6.编写代码---视图

 文件位置application/view/news

  (1)add.html

<!DOCTYPE html>
<html>
<head>
    <title>添加新闻</title>
</head>
<body>
    <div>
        <form action="<?php echo site_url('news/insert');?>" method="post">
            <fieldset>
                <legend>添加新闻</legend>
                <ul>
                    <li for=""><label>标题</label><input type="text" name="title"></li>
                    <li for=""><label>作者</label><input type="text" name="author"></li>
                    <li for=""><label>正文</label><textarea name="content" id="" cols="100"></textarea></li>
                    <li for=""><label>&nbsp;&nbsp;&nbsp;</label><input type="submit" name="btn" value="添加"></li>
                    <input type="hidden" name="act" value="add">
                </ul>
            </fieldset>

        </form>
    </div>
</body>
</html>


(2)list.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h3>新闻列表</h3><a href="<?php echo site_url('news/add')?>">添加新闻</a>
    <table width="700" border="1">
        <tr>
            <th>编号</th>
            <th>标题</th>
            <th>作者</th>
            <th>添加时间</th>
            <th>操作</th>
        </tr>
        <?php foreach($news as $row): ?>
            <tr>
                <td><?php echo $row['id'] ?></td>
                <td><?php echo $row['title'] ?></td>
                <td><?php echo $row['author'] ?></td>
                <td><?php echo $row['add_time'] ?></td>
                <td><a href="<?php $url='news/edit/'.$row['id'];echo site_url($url); ?>">编辑</a>
                    &nbsp;&nbsp;
                    <a href="<?php $url='news/delete/'.$row['id'];echo site_url($url); ?>">删除</a></td>
            </tr>
        <?php endforeach; ?>
    </table>
</body>
</html>


(3)edit.html

<!DOCTYPE html>
<html>
<head>
    <title>修改新闻</title>
</head>
<body>
    <div>
        <form action="<?php echo site_url('news/update');?>" method="post">
            <fieldset>
                <legend>修改新闻</legend>
                <ul>
                    <li for=""><label>标题</label><input type="text" name="title" value="<?php echo $new['title']?>"></li>
                    <li for=""><label>作者</label><input type="text" name="author" value="<?php echo $new['author']?>"></li>
                    <li for=""><label>正文</label><textarea name="content" id="" cols="100"><?php echo $new['content']?></textarea></li>
                    <li for=""><label>&nbsp;&nbsp;&nbsp;</label><input type="submit" name="btn" value="修改"></li>
                    <input type="hidden" name="id" value="<?php echo $new['id']?>">
                </ul>
            </fieldset>

        </form>
    </div>
</body>
</html>


完成!








0 0
原创粉丝点击