国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

CodeIgniter 入門教程第一篇:信息發(fā)布

2019-11-15 02:23:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
CodeIgniter 入門教程第一篇:信息發(fā)布

一、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ā)送給用戶。

e0ed3e758d47432abffae22d2083ea753

圖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文件夾

dd04a067-c93f-4a95-8621-9e0f8aa9d140

Test.php

<?phpdefined('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>這是我們的主頁(yè)</h1></body></html>

  

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

1098f9c8-b8fc-41e7-9dfd-b247246f0325

2. 插入數(shù)據(jù)庫(kù)條目

創(chuàng)建數(shù)據(jù)庫(kù)表ts_news

b1bf3a49186b459e8352cf242d9a63913

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>

  

  

刷新查看效果:

e08e54cc941c4ce99d21aafa80cba2233

本文地址:http://m.survivalescaperooms.com/martin-tan/p/4708071.html


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金溪县| 瑞昌市| 松滋市| 增城市| 太仆寺旗| 吴桥县| 彭山县| 连城县| 雷山县| 深泽县| 两当县| 于田县| 任丘市| 库车县| 交城县| 翁牛特旗| 萝北县| 左云县| 邵阳县| 福建省| 苏尼特右旗| 蓬安县| 西充县| 萨迦县| 奉贤区| 盈江县| 长岛县| 剑阁县| 外汇| 长汀县| 新民市| 宿迁市| 镇江市| 南通市| 临西县| 桃源县| 陆丰市| 高陵县| 贡山| 清苑县| 甘肃省|