1. 程式人生 > >CodeIgniter 入門教程第一篇:信息發布

CodeIgniter 入門教程第一篇:信息發布

per 所有 控制 知識點 image doc ref har 提交

一、MVC

CodeIgniter 采用MVC架構即:控制層、模型層和視圖層。

對應Application下面的文件夾 (圖1):

所有新建文件以.php結尾

視圖層 view 文件夾放入HTML模板

模型層 model 存放對數據庫操作的代碼

控制層 controllers 存放進行邏輯判斷的代碼,從模型層取得數據然後輸入到視圖層,發送給用戶。

技術分享

圖1

功能

1. 模板增加輸入表單

2. 控制器增加接收表單數據的代碼,並對用戶輸入進行簡單校驗。

3. 在表單上方輸出標題和正文,以及發布時間。

用到的知識點:CI helper類(url) 和 輸入類(input),

以及CI ActiveRecord 和向模板傳值。

二、初始配置

1. 鏈接數據庫

修改數據庫配置:/application/config/database.php

1 2 3 4 5 6 ‘hostname‘ => ‘localhost‘, ‘username‘ => ‘root‘, ‘password‘ => ‘‘, ‘database‘ => ‘test‘, ‘dbdriver‘ => ‘mysqli‘, ‘dbprefix‘ => ‘ts_‘,

  

2. 修改默認路由

CI框架采用單文件入口的方式,默認必須通過index.php來訪問控制層。比如controllers文件夾下有個名為test的class類,test有個叫home的function,

則訪問URL為:http://www.example.com/index.php/test/home

三、輸出頁面

1. 直接輸出HTML模板

新建兩個文件分別位於controllers文件夾和views文件夾

技術分享

Test.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <?php defined(‘BASEPATH‘) OR exit(‘No direct script access allowed‘
); class Test extends CI_Controller { public function home() { $this->load->view(‘home‘); } } home.php <?php defined(‘BASEPATH‘) OR exit(‘No direct script access allowed‘); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1>這是我們的主頁</h1> </body> </html>

  

在瀏覽器打開類似如下地址:http://test.com/index.php/test/home

技術分享

2. 插入數據庫條目

創建數據庫表ts_news

技術分享

Test.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 <?php defined(‘BASEPATH‘) OR exit(‘No direct script access allowed‘); class Test extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper(‘url‘); $this->load->model(‘news_model‘); } public function home() { $this->load->view(‘home‘); } public function add_news(){ $title = $this->input->get(‘title‘,TRUE); $content = $this->input->get(‘content‘); if ( (strlen($title) < 20 ) or (strlen($content) < 20 ) ){ echo ‘標題或正文內容過短‘; return false; } $arr = array( ‘id‘ => ‘‘, ‘title‘ => $title, ‘content‘ => $content, ‘update_time‘ => time(), ‘create_time‘ => time() ); $check = $this->news_model->insert($arr,‘news‘); if ($check) { redirect(‘test/home‘); } else { echo ‘提交失敗‘; } } } home.php <?php defined(‘BASEPATH‘) OR exit(‘No direct script access allowed‘); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1>這是我們的主頁</h1> <hr> <form action="<?php echo site_url(‘test/add_news‘); ?>"> <label for="title">標題</label> <input type="text" name="title" value=""> <br> <label for="content">正文</label> <textarea name="content" id="" cols="30" rows="10"></textarea> <br> <input type="submit" value="提交" > </form> </body> </html> News_model.php <?php if ( ! defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘); class News_model extends CI_Model { public function __construct() { parent::__construct(); $this->load->database(); } public function insert($arr,$table) { $this->db->insert($table, $arr); if ($this->db->affected_rows() > 0) { return $this->db->insert_id(); } else { return FALSE; } } } 412ded80-4884-4a2f-ae37-6ba69cdc4278 493498ee-0f5c-4676-9cec-38e5a3f3e6fd 3. 查詢數據庫並輸出 News_model.php增加 public function get_all($table) { $this->db->select(‘*‘); $query = $this->db->get($table); $query = $query->result_array(); return $query; } Test.php 的 home修改為: public function home() { $news = $this->news_model->get_all(‘news‘); $data[‘news‘] = $news; $this->load->view(‘home‘,$data); }

  Home模板的body修改為:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <body> <h1>這是我們的主頁</h1> <?php foreach ($news as $key => $value) { echo ‘<div> <h3>‘.$value[‘title‘].‘</h3> <span>發布時間:‘.date(‘Y-m-d H:i:s‘,$value[‘create_time‘]).‘</span> <p>‘.$value[‘content‘].‘</p> </div>‘; } ?> <hr> <form action="<?php echo site_url(‘test/add_news‘); ?>"> <label for="title">標題</label> <input type="text" name="title" value=""> <br> <label for="content">正文</label> <textarea name="content" id="" cols="30" rows="10"></textarea> <br> <input type="submit" value="提交" > </form> </body>

  

  

刷新查看效果:

技術分享

CodeIgniter 入門教程第一篇:信息發布