• 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>
  • 相关阅读:
    deleteCustomer
    python入门day07——可变不可变类型、数字类型、字符串类型.md
    python入门day06——流程控制之if判断、while循环、for循环
    python入门day05——基本运算符、逻辑运算符
    python入门day04——基本数据类型、输入输出、基本运算符
    python入门day03——变量、内存管理:垃圾回收机制(GC)、常量
    python入门day03——python介绍、运行程序步骤
    python入门day02——计算机硬件、操作系统、编程语言
    python入门day01——01 计算机硬件组成与工作原理
    python入门作业day04 基本数据类型应用
  • 原文地址:https://www.cnblogs.com/maoriaty/p/9052051.html
Copyright © 2020-2023  润新知