首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

CI Series - 来个CRUD试试

2012-07-03 
CI Series -- 来个CRUD试试创建数据库, 实现CRUD??? ??? - 说明??? ??? 为了在最短的时间内创建个原型(pro

CI Series -- 来个CRUD试试
创建数据库, 实现CRUD

???
??? - 说明
??? ??? 为了在最短的时间内创建个原型(prototype), 采用用 Vertical Slice (VS) 最佳实践。 VS是应用程序的纵切面。 比如对我们要实现的原型来说,就是创建 View,Controller 和 Module, 来实现一个 blog 的最小的功能, 比如创建 blog
??? ???
???
??? - 创建数据库
??? ??? - 用 phpMyAdmin 创建数据库 blueblog
???
???
??? - sql

DROP TABLE IF EXISTS `blog`;       CREATE TABLE IF NOT EXISTS `blog` (          `id` int(10) unsigned NOT NULL AUTO_INCREMENT,          `content` varchar(255) NOT NULL,          `createdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,          PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
?

??? ???
??? - 配置 CI 的 DB
??? ??? C:\ws\php\phplab\BlueBlog\application\config\database.php

$db['default']['hostname'] = 'localhost';$db['default']['username'] = 'root';$db['default']['password'] = '换成自己的密码';$db['default']['database'] = 'blueblog';
?

??? ???
??? ???
??? - 创建 controller
??? ??? - C:\ws\php\phplab\BlueBlog\application\controllers\blog.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');            class Blog extends CI_Controller            {                               public function create()                {                    $content = urldecode($this->uri->segment(3, "Default content")); // 这里使用了 CI 的 URI lib, 用来解析从 URL 传过来的参数。                    $blog = array($content);                                       $sql = "INSERT INTO blog (content) VALUES (?)"; // SQL ??                                       $this->load->database(); // 加载 数据库                    $this->db->query($sql, $blog); // 执行 SQL                }            }?>
?

??? ???
??? - 测试 VS
??? ??? - http://127.0.0.1/blueblog/index.php/blog/create/Blue blog test content
??? ??? ??? 这里注意下 CI 的 URL mapping: blog/create/Blue blog test content => controller class/function/param
??? ??? ??? ???
??? ??? ??? ??? 输入上面的URL, 回车, 查数据表, yes! 数据创建成功
??? ??? ??? ??? id ??? content ??? createdate
??? ??? ??? ??? 1 ??? Blue blog test content ??? 2011-05-30 14:34:48
??? ??? ??? ???
??? ??? ??? ???
??? - 为 blog 添加 view 和其他操作
??? ??? - CRUD
??? ??? ???
??? ??? - 创建 view
??? ??? ??? - C:\ws\php\phplab\BlueBlog\application\views\blog.php
??? ??? ??? ??? ---------------------------------------------- CS

<!DOCTYPE html>                    <html lang="en">                    <head>                    <meta charset="utf-8">                    <title>Blog List</title>                    </head>                    <body>                    <h1>Blog List</h1>                    <code> <?php                    if($list){                        foreach ($list as $row)                        {                            echo $row->id. " | " . $row->content. " | " . $row->createdate . "<br/>";                        }                        echo 'Total Results: ' . $num_rows;                    }                    ?> </code>                    <p><br />                    Page rendered in {elapsed_time} seconds</p>                    </body>                    </html>
?

??? ??? ??? ??? ---------------------------------------------- CE
??? ??? ??? ???
??? ??? - Controller 实现读操作
??? ??? ??? - C:\ws\php\phplab\BlueBlog\application\controllers\blog.php
??? ??? ??? ??? 在 controller 里添加读操作
??? ??? ??? ??? ---------------------------------------------- CS

                    public function read()                    {                        $this->load->database();                        $query = $this->db->query('SELECT * FROM `blog`');                                               $blog = array();                        $blog['list'] = $query->result();                        $blog['num_rows'] = $query->num_rows();                        $this->load->view('blog', $blog);                    }
?

??? ??? ??? ??? ---------------------------------------------- CE
??? ??? ??? ???
??? ??? - http://127.0.0.1/blueblog/index.php/blog/read
??? ??? ??? 测试下
??? ??? ???
??? ??? ???
??? ??? - 嫌页面丑的加点儿 CSS
??? ??? ???
??? ??? ??? ---------------------------------------------- CS

              <style type="text/css">                body {                    background-color: #fff;                    margin: 40px;                    font-family: Lucida Grande, Verdana, Sans-serif;                    font-size: 14px;                    color: #4F5155;                }                a {                    color: #003399;                    background-color: transparent;                    font-weight: normal;                }                h1 {                    color: #444;                    background-color: transparent;                    border-bottom: 1px solid #D0D0D0;                    font-size: 16px;                    font-weight: bold;                    margin: 24px 0 2px 0;                    padding: 5px 0 6px 0;                }                code {                    font-family: Monaco, Verdana, Sans-serif;                    font-size: 12px;                    background-color: #f9f9f9;                    border: 1px solid #D0D0D0;                    color: #002166;                    display: block;                    margin: 14px 0 14px 0;                    padding: 12px 10px 12px 10px;                }                </style>
?

??? ??? ??? ---------------------------------------------- CE

??? - CR 完了, 剩下的也不难了吧

热点排行