[eluser]brian88[/eluser]
This is my first attempt at testing a search feature for a website. Its very simple and Im looking for feedback to make the search more usable without a ton of advance code. The code is simple now, Im just looking for some simple feedback to make the search a little better.
Table: blog
----------------------------------------------------
| id | title | body |
----------------------------------------------------
| 1 | How to lose weight fast | a bunch of lorum ipsom text |
----------------------------------------------------
| 2 | How to hack stuff | a bunch of lorum ipsom text again |
-----------------------------------------------------
| 3 | Top 10 best foods | lots of body copy about foods |
-----------------------------------------------------
model
Code:
// get search results
function search($keyword = '') {
$q = $this->db->query("
select *
from blog
where title
like '%{$keyword}%'
or body
like '%{$keyword}%'
");
if($q->num_rows() > 0){
return $q->result();
}
} // end function
controller
Code:
function search(){
// get results from database for the users keyword
$data['results'] = $this->main_mod->search( $this->input->post('search') );
// views
$this->load->view('homepage', $data);
} // end search
view
Code:
<div id='content'>
<form action="<?php echo base_url('main/search'); ?>" method="post" accept-charset="utf-8">
<input id='search' type="text" name="search" value="search">
<input type="submit" name="submit" value="search">
</form>
<ul>
<?php if(!empty($results)): ?> // if there are results
<?php foreach($results as $r): ?>
<li>
<a href="#"><?php echo $r->title; ?></a><br/> // blog title
<?php echo $r->body; ?> // blog body text
</li>
<?php endforeach; ?>
<?php else: ?>
<p><br />no results</p>
<?php endif; ?>
</ul>
} ?>
</div><!-- #Content -->