CodeIgniter(1)簡單入門---使用mvc來完成對新聞的一組操作----增刪改查

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>


完成!








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章