--
-- 数据库:ci-intro
创建数据库ci-intro
默认字符集 utf8 COLLATE utf8_general_ci; 使用ci-intro
;
--
-- 表的表结构posts
如果不存在则创建表posts
(
id
int(11) unsigned NOT NULL AUTO_INCREMENT,
title
varchar(100) DEFAULT NULL,
body
text,
created
datetime DEFAULT NULL,
modified
datetime DEFAULT NULL, PRIMARY KEY ( id
) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6;
--
-- 为表转储数据posts
INSERT INTO posts
( id
, title
, body
, created
, modified
) VALUES (1, '另一天还在寻找', '我的狮子跑了', '2013-04-02 08:20:20', '2013-04-03 12:43:49 '), (2, '美好的一天', '狮子回来了。', '2013-04-02 08:20:44', '2013-04-02 08:20:44'), (3, 'Thank GOD', '一切都属于我父亲', '2013-04-02 08:21:03', '2013-04-02 08:21:03'), (4, '在海边', '我们去了海边,然后开始下雨了!', '2013-04-02 08:21:35', '2013-04-03 19:28:48'), (5, '无聊posts', '事件日记不感兴趣', '2013-04-02 08:21:55', '2013-04-03 19:35:25');
应用程序\控制器\posts.php
function search()
{
$data['title'] = "Blogging";
$data['heading'] = "Bloging";
$this->load->view('view_search', $data);
}
应用程序\视图\view_search.php
<?php echo form_fieldset('<b>Search a Post!</b>');?>
<?php echo form_open('posts/execute_search'); ?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="5" style="border:dashed" bgcolor="#FFCC99">
<tr>
<td align="right">
<?php echo form_label('Search: enter keyword, title, content '); ?>
</td>
<td>
<?php echo form_input(array('name'=>'search')); ?>
</td>
</tr>
<tr>
<td align="right" valign="top"><?php echo nbs(1); ?></td>
<td valign="top" class="style1"><?php echo form_submit('search_submit','Submit'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>
<?php echo form_fieldset_close(); ?>
应用程序\控制器\posts.php
public function execute_search($search_term)
{
$data['title'] = "Blogging";
$data['heading'] = "Bloging";
$search_term = $_POST['search'];
$rs = $this->db->like('title', trim($search_term))
->or_like('body', trim($search_term))
->get('posts');
$total = $rs->num_rows();
$data['results'] = $rs->result();
$this->load->view("view_index", $data);
}