CI简单操作

路由:/application/config/routes.php

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

控制器:/application/controllers/News.php

<?php
/**
 * 新闻控制器
 */
class News extends CI_Controller 
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->helper('url_helper');
    }

    // 呈现所有数据-列表页
    public function index()
    {
        $data['news'] = $this->news_model->get_news();
        $data['title'] = 'News archive';

        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer', $data);
    }

    // 呈现某条新闻-详情页, 如果查出多条新闻只取第一条
    public function view($slug = NULL)
    {
        $data['news_item'] = $this->news_model->get_news($slug);
        
        if(empty($data['news_item']))
        {
            show_404();
        }

        $data['title'] = $data['news_item']['title'];

        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer', $data);
    }

    // 表单提交数据-创建
    public function create()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['title'] = 'Create a news item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');

        if($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('news/create');
            $this->load->view('templates/footer');
        }
        else
        {
            $this->news_model->set_news();
            $this->load->view('news/success');
        }
    }
}

模型:/application/models/News_model.php

<?php
class News_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    // 获取新闻列表
    public function get_news($slug = FALSE)
    {
        if($slug === FALSE) {
            $query = $this->db->get('news');
            return $query->result_array();
        }
        $query = $this->db->get_where('news', array('slug'=>$slug));
        return $query->row_array(); //返回单条数据
    }

    // 存储表单提交数据
    public function set_news()
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text')
        );

        return $this->db->insert('news', $data);
    }
}

创建表:

CREATE TABLE news (
    id int(11) NOT NULL AUTO_INCREMENT,
    title varchar(128) NOT NULL,
    slug varchar(128) NOT NULL,
    text text NOT NULL,
    PRIMARY KEY (id),
    KEY slug (slug)
);

视图:/application/views/

# news/create.php
<h2><?php echo $title ?></h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create'); ?>

    <label for="title">Title</label>
    <input type="text" name="title"><br />

    <label for="text">Text</label>
    <textarea name="text" ></textarea><br />

    <input type="submit" name="submit" value="Create news item">

</form>


# news/index.php
<h2><?php echo $title; ?></h2>

<?php foreach ($news as $news_item): ?>

    <h3><?php echo $news_item['title'] ?></h3>
    <div class="main">
        <?php echo $news_item['text'] ?>
    </div>
    <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>

<?php endforeach; ?>


# news/success.php
<?php
echo 'success';


# news/view.php
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];


# templates/header.php
<html>
<head>
<title>CodeIgniter Tutorial</title>
</head>
<body>
<h1><?php echo $title; ?></h1>
 
 
# templates/footer.php
<em>&copy;2018</em>
</body>
</html>
原文地址:https://www.cnblogs.com/maoriaty/p/9052051.html