- 论坛徽章:
- 0
|
创建数据库, 实现CRUD
- 说明
为了在最短的时间内创建个原型(prototype), 采用用 Vertical Slice (VS) 最佳实践。 VS是应用程序的纵切面。 比如对我们要实现的原型来说,就是创建 View,Controller 和 Module, 来实现一个 blog 的最小的功能, 比如创建 blog
- 创建数据库
- 用 phpMyAdmin 创建数据库 blueblog
- sql
Sql代码- 1.DROP TABLE IF EXISTS `blog`;
- 2.
- 3.CREATE TABLE IF NOT EXISTS `blog` (
- 4. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
- 5. `content` varchar(255) NOT NULL,
- 6. `createdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- 7. PRIMARY KEY (`id`)
- 8.) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
- 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
Php代码- 1.$db['default']['hostname'] = 'localhost';
- 2.$db['default']['username'] = 'root';
- 3.$db['default']['password'] = '换成自己的密码';
- 4.$db['default']['database'] = 'blueblog';
- $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代码- 1.<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- 2.
- 3. class Blog extends CI_Controller
- 4. {
- 5.
- 6. public function create()
- 7. {
- 8. $content = urldecode($this->uri->segment(3, "Default content")); // 这里使用了 CI 的 URI lib, 用来解析从 URL 传过来的参数。
- 9. $blog = array($content);
- 10.
- 11. $sql = "INSERT INTO blog (content) VALUES (?)"; // SQL ?ϣ
- 12.
- 13. $this->load->database(); // 加载 数据库
- 14. $this->db->query($sql, $blog); // 执行 SQL
- 15. }
- 16. }?>
- <?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
Html代码- 1.<!DOCTYPE html>
- 2. <html lang="en">
- 3. <head>
- 4. <meta charset="utf-8">
- 5. <title>Blog List</title>
- 6. </head>
- 7. <body>
- 8. <h1>Blog List</h1>
- 9. <code> <?php
- 10. if($list){
- 11. foreach ($list as $row)
- 12. {
- 13. echo $row->id. " | " . $row->content. " | " . $row->createdate . "<br/>";
- 14. }
- 15.
- 16. echo 'Total Results: ' . $num_rows;
- 17. }
- 18. ?> </code>
- 19. <p><br />
- 20. Page rendered in {elapsed_time} seconds</p>
- 21.
- 22. </body>
- 23. </html>
- <!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
Php代码- 1.public function read()
- 2.{
- 3. $this->load->database();
- 4. $query = $this->db->query('SELECT * FROM `blog`');
- 5.
- 6. $blog = array();
- 7. $blog['list'] = $query->result();
- 8. $blog['num_rows'] = $query->num_rows();
- 9.
- 10. $this->load->view('blog', $blog);
- 11.}
- 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
Java代码- 1.<style type="text/css">
- 2. body {
- 3. background-color: #fff;
- 4. margin: 40px;
- 5. font-family: Lucida Grande, Verdana, Sans-serif;
- 6. font-size: 14px;
- 7. color: #4F5155;
- 8. }
- 9.
- 10. a {
- 11. color: #003399;
- 12. background-color: transparent;
- 13. font-weight: normal;
- 14. }
- 15.
- 16. h1 {
- 17. color: #444;
- 18. background-color: transparent;
- 19. border-bottom: 1px solid #D0D0D0;
- 20. font-size: 16px;
- 21. font-weight: bold;
- 22. margin: 24px 0 2px 0;
- 23. padding: 5px 0 6px 0;
- 24. }
- 25.
- 26. code {
- 27. font-family: Monaco, Verdana, Sans-serif;
- 28. font-size: 12px;
- 29. background-color: #f9f9f9;
- 30. border: 1px solid #D0D0D0;
- 31. color: #002166;
- 32. display: block;
- 33. margin: 14px 0 14px 0;
- 34. padding: 12px 10px 12px 10px;
- 35. }
- 36. </style>
- <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 完了, 剩下的也不难了吧 |
|