Professional CodeIgniter, Thomas Myer

Chapter 4: Creating the Main Web Site
102
$this-
>
db-
>
where(`status','active');
$this-
>
db-
>
limit(50);
$Q = $this-
>
db-
>
get(`products');
if ($Q-
>
num_rows()
>
0){
foreach ($Q-
>
result_array() as $row){
$data[] = $row;
}
}
$Q-
>
free_result();
return $data;
}

Notice the use of
$this
-
>
db
-
>
like()

and
$this
-
>
db
-
>
orlike()

? These methods create wildcard
matching on certain fields. The above code creates the following SQL statement:
select id, name, shortdesc, thumnail from products
where (name like `%$term%'
or shordesc like `%$term%'
or longdesc like `%$term%')
and status='active'
order by name asc
limit 50

Now that the search function is built, you can create the controller function called
search()
. Remember
that this URL is loaded only when someone fills in a search term and clicks Search, so you ' ll need to
check for form input.

In CodeIgniter, the way to check for form input is to use the
$this
-
>
input
-
>
post()

method. If there
isn ' t a value for " term " in the form post, redirect the user to the home page. There are other security
considerations to make, but most of those are covered in Chapter 9 .
function search(){
if ($this-
>
input-
>
post(`term')){
$search[`results'] = $this-
>
MProducts-
>
search($this-
>
input-
>
post(`term'));
}else{
redirect(`welcome/index','refresh');
}
$data[`main'] = `search';
$data[`title'] = "Claudia's Kids | Search Results";
$data[`navlist'] = $this-
>
MCats-
>
getCategoriesNav();
$this-
>
load-
>
vars($data);
$this-
>
load-
>
view(`template',$data);
}

Once again, you ' re going to use
$data[
'
main
'
]

to pass along the name of the view you want loaded in
the main area of the page. This time, you ' re also passing in an array called
$search[
'
results
'
]
and
looping over the array called
$results

in the view.

The search view looks like the following code listing. Notice that you ' re only using the
pleft
div,
instead of both
pleft

and
pright

. The reason you ' re not going to use
pright

is that you don ' t need
it -- search pages don ' t have a right column that displays related products.
c04.indd 102
c04.indd 102
6/10/08 5:34:29 PM
6/10/08 5:34:29 PM


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.