CodeIgniter 入门教程第一篇:信息发布,ci
一、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
'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
<?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 id="这是我们的主页">这是我们的主页</h1> </body> </html>
在浏览器打开类似如下地址:http://test.com/index.php/test/home
2. 插入数据库条目
创建数据库表ts_news
Test.php
<?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 id="这是我们的主页">这是我们的主页</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修改为:
<body> <h1 id="这是我们的主页">这是我们的主页</h1> <?php foreach ($news as $key => $value) { echo '<div> <h3 id="value-title">'.$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>
刷新查看效果:

一个好的网站,不能只看外表,网站后台同样很重要。本篇文章给大家分享10款好看又实用的Bootstrap后台管理系统模板,可以帮助大家快速建立强大有美观的网站后台,欢迎下载使用!如果想要获取更多后端模板,请关注php中文网后端模板栏目!

bootstrap与jquery的关系是:bootstrap是基于jquery结合了其他技术的前端框架。bootstrap用于快速开发Web应用程序和网站,jquery是一个兼容多浏览器的javascript库,bootstrap是基于HTML、CSS、JAVASCRIPT的。

好看又实用的Bootstrap电商源码模板可以提高建站效率,下面本文给大家分享7款实用响应式Bootstrap电商源码,均可免费下载,欢迎大家使用!更多电商源码模板,请关注php中文网电商源码栏目!

好看又实用的企业公司网站模板可以提高您的建站效率,下面PHP中文网为大家分享8款Bootstrap企业公司网站模板,均可免费下载,欢迎大家使用!更多企业站源码模板,请关注php中文网企业站源码栏目!

在bootstrap中,sm是“小”的意思,是small的缩写;sm常用于表示栅格类“.col-sm-*”,是小屏幕设备类的意思,表示显示大小大于等于768px并且小于992px的屏幕设备,类似平板设备。

bootstrap默认字体大小是“14px”;Bootstrap是一个基于HTML、CSS、JavaScript的开源框架,用于快速构建基于PC端和移动端设备的响应式web页面,并且默认的行高为“20px”,p元素行高为“10px”。

bootstrap是免费的;bootstrap是美国Twitter公司的设计师“Mark Otto”和“Jacob Thornton”合作基于HTML、CSS、JavaScript 开发的简洁、直观、强悍的前端开发框架,开发完成后在2011年8月就在GitHub上发布了,并且开源免费。

bootstrap modal关闭的方法:1、连接好bootstrap的插件;2、给按钮绑定模态框事件;3、通过“ $('#myModal').modal('hide');”方法手动关闭模态框即可。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),