创建数据表,实现基本功能代码
- 添加数据库描述文件: app/notebook/dbschema/item.php
<?php
$db['item']=array (
'columns' =>
array (
'item_id' => array (
'type' => 'number',
'required' => true,
'extra' => 'auto_increment',
'pkey' => true
),
'item_subject' => array ( 'type' => 'varchar(100)' ),
'item_content' => array ( 'type' => 'text' ),
'item_posttime' => array ( 'type' => 'time' ),
'item_email' => array ( 'type' => 'email'),
),
);
- 数据库文件写好后,执行update命令即可自动创建数据库。
app/base/cmd update
Updating base_application_dbtable@notebook.
CREATE TABLE `sdb_notebook_item` (
`item_id` mediumint(8) unsigned not null auto_increment,
`item_subject` varchar(100),
`item_content` text,
`item_posttime` int(10) unsigned,
`item_email` varchar(255),
primary key (item_id)
)ENGINE = MyISAM DEFAULT CHARACTER SET utf8;
Updating base_application_cache_expires@notebook.
Installing Cache_Expires DB:SDB_NOTEBOOK_ITEM
UPDATE CACHE EXPIRES KV DATA
Applications database and services is up-to-date, ok.
- 控制器文件:app/notebook/controller/default.php
<?php
class notebook_ctl_default extends base_controller{
public function index(){
$this->pagedata['items'] = $this->app->model('item')->getList('*');
$this->display('default.html');
}
public function addnew(){
$this->begin(array("ctl" => "default", "act" => "index"));
$data = array(
'item_subject'=>$_POST['subject'],
'item_content'=>$_POST['content'],
'item_email'=>$_POST['email'],
'item_posttime'=>time(),
);
$result = $this->app->model('item')->insert($data);
$this->end($result);
}
}
- 编写视图文件:app/notebook/view/default.html 为了简化实现,此处用了webkit 浏览器的私有属性。
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Notebook</title>
<link rel="stylesheet" type="text/css"
href="<{$env.app.res_url}>/style.css" media="screen" />
</head>
<body>
<h1>Notebook</h1>
<dl>
<{foreach from=$items item=item}>
<dt>
<{$item.item_subject}>
<span class="date">日期: <{$item.item_posttime|cdate}></span>
</dt>
<dd><{$item.item_content}></dd>
<{/foreach}>
<div style="margin-bottom:20px;clear:both" /> </div>
</dl>
<form id="writer" method="post" action="<{link ctl='default' act='addnew' app='notebook'}>">
<input id="writer-subject" type="text" value=""
name="subject" placeholder="请输入留言的标题" />
<input id="writer-email" type="text" value=""
name="email" placeholder="请输入邮箱地址" />
<textarea name="content" placeholder="留言正文"></textarea>
<input type="submit" value="发布新留言" id="writer-submit" />
</form>
</body>
</html>
- 我们写个简单的样式:app/notebook/statics/style.css
html{background:#A4C5FF}
body{width:380px;margin:auto;background:#fff;padding:20px}
dd{padding:10px}
dt{border-bottom:1px solid #ccc;margin:10px 0;
font-weight:bold;color:#42597B;font-size:20px;}
#writer{background:#E8EEF7;padding:10px;border:1px solid #E1ECFE}
#writer-email,#writer-subject{display:block;margin-bottom:10px;width:300px}
#writer textarea{width:300px;margin-bottom:10px;display:block;height:80px}
#writer-submit{padding:2px 18px;font-weight:bold;}
h1{text-align:center;padding:20px;text-shadow: 2px 2px 2px #999;}