一、MVC
CodeIgniter 采用MVC架構(gòu)即:控制層、模型層和視圖層。
對(duì)應(yīng)application下面的文件夾 (圖1):
所有新建文件以.php結(jié)尾
視圖層 view 文件夾放入HTML模板
模型層 model 存放對(duì)數(shù)據(jù)庫(kù)操作的代碼
控制層 controllers 存放進(jìn)行邏輯判斷的代碼,從模型層取得數(shù)據(jù)然后輸入到視圖層,發(fā)送給用戶。

圖1
功能:
1. 模板增加輸入表單
2. 控制器增加接收表單數(shù)據(jù)的代碼,并對(duì)用戶輸入進(jìn)行簡(jiǎn)單校驗(yàn)。
3. 在表單上方輸出標(biāo)題和正文,以及發(fā)布時(shí)間。
用到的知識(shí)點(diǎn):CI helper類(url) 和 輸入類(input),
以及CI ActiveRecord 和向模板傳值。
二、初始配置
1. 鏈接數(shù)據(jù)庫(kù)
修改數(shù)據(jù)庫(kù)配置:/application/config/database.php
'hostname' => 'localhost', 'username' => 'root', 'passWord' => '', 'database' => 'test', 'dbdriver' => 'MySQLi', 'db
2. 修改默認(rèn)路由
CI框架采用單文件入口的方式,默認(rèn)必須通過(guò)index.php來(lái)訪問(wèn)控制層。比如controllers文件夾下有個(gè)名為test的class類,test有個(gè)叫home的function,
則訪問(wèn)URL為:http://www.example.com/index.php/test/home
三、輸出頁(yè)面
1. 直接輸出HTML模板
新建兩個(gè)文件分別位于controllers文件夾和views文件夾
Test.php
<?phpdefined('BASEPATH') OR exit('No direct script accessallowed');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>這是我們的主頁(yè)</h1></body></html>
在瀏覽器打開類似如下地址:http://test.com/index.php/test/home

2. 插入數(shù)據(jù)庫(kù)條目
創(chuàng)建數(shù)據(jù)庫(kù)表ts_news

Test.php
<?phpdefined('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 '標(biāo)題或正文內(nèi)容過(guò)短';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>這是我們的主頁(yè)</h1><hr><form action="<?php echo site_url('test/add_news'); ?>"><label for="title">標(biāo)題</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-6ba69cdc4278493498ee-0f5c-4676-9cec-38e5a3f3e6fd 3. 查詢數(shù)據(jù)庫(kù)并輸出 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修改為:
<body><h1>這是我們的主頁(yè)</h1><?php foreach ($news as $key => $value) {echo '<div><h3>'.$value['title'].'</h3><span>發(fā)布時(shí)間:'.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">標(biāo)題</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>
刷新查看效果:

本文地址:http://m.survivalescaperooms.com/martin-tan/p/4708071.html
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注