Professional CodeIgniter

(singke) #1

Chapter 4: Creating the Main Web Site


84


Notice that the temporary array is keyed by category_id in the foreach loop:

if ($Q- > num_rows() > 0){
foreach ($Q- > result_array() as $row){
$temp[$row[‘category_id’]] = array(
“id” = > $row[‘id’],
“name” = > $row[‘name’],
“thumbnail” = > $row[‘thumbnail’]
);
}
}

This simple step ensures that the final step in the process, in which the function shifts however many
items were specified by the $limit argument, gives you what you want: no more than one product per
category in the returned data.

At the moment, there are no products or categories stored in the database tables, so now would be a
good time to put in some sample data. For now, upload a few dummy images into the /images folder,
and create a dozen dummy entries in the products table. The data don ’ t have to be exactly like Claudia ’ s
inventory, nor do the pictures have to be anything more specific than a shot that has a watermark on it.
It ’ s all just for placement at the moment.

Please note that there is a downloadable zip file available at http://www.wrox.com that contains some dummy
data. The dummy data consist of sample categories, products, and uploadable images that you can use
while you work. It ’ s often easier to develop an application like this if you have something to work with.
The dummy data aren ’ t a work of literature. They merely contain product names like Dress 1 or Shoes 3
and descriptions like “ This is a nice pair of shoes. ” The idea is to give you just enough differentiation
that you can test your site. Your customers will blow it all away when they start entering real data.

phpMyAdmin
If you know about phpMyAdmin, you know that it ’ s a powerful tool that allows you to administer your
entire database. It allows you to add, drop, and modify databases; add, drop, and modify tables; add and
remove fields to any table; add, modify, and remove data from any field; and much, much more. It even
allows you to import and export data. It ’ s an invaluable tool.
If you have phpMyAdmin installed on your server (or if it ’ s available as part of your hosting package),
you ’ ll be using it many times a day during development. If you don ’ t have access to it, the only
way you ’ ll be able to manipulate the database is to work with the mysql command line. That may work
OK for a short time, but eventually, you ’ ll want to take the time to install phpMyAdmin.

Using the New Model Functions on the Home Page


As soon as you have some data and images loaded, open your controller file in a text editor and start
making some quick changes. The first thing you want to do is call your new functions:

function index(){
$data[‘title’] = “Welcome to Claudia’s Kids”;
$data[‘navlist’] = $this- > MCats- > getCategoriesNav();
$data[‘mainf’] = $this- > MProducts- > getMainFeature();
$skip = $data[‘mainf’][‘id’];
Free download pdf