Now that we are done the CI installation. We are ready to start building our e-commerce application. What we need to do first is design our database structure. You can of course freely design your own structure, but for this tutorial I made it simple.
Now, Let say,
I want to categorize my products. So we must have a table for category.
Product category can have a child category.
Category can be deactivated and activated.
This is our table for categories looks like.
Then our products,
Can only belong to one category at a time
Our products has a name, image, thumbnail, short description, long description
And can be activated and deactivated.
This is what I came up with our products table.
Insure you have a database installation on your local webserver. Using any mySQL client available in your end just throw this SQL to create the tables.
Creating the Categories Model
In creating a model in CodeIgniter you can name it anything you want as long as it don’t have any conflicts of names in other model or to your controller. It is a good practice to put a prefix or suffixes to your model’s name. My style in naming a model is chosen name suffixed by ‘Model‘. So I have to name our model to ‘categoryModel‘.
More details about creating model can be read in CI userguide. Now let’s create our basic model called
categoryModel and our constructor.
Save the file as categorymodel.php in your system/application/models/ directory.
Then add a function that will retrieve a category from the database.
This function passes in an
$id variable that is used to select one row matching that ID from the categories table. It then returns the data as an array for future use.
Now we can select our category one by one with the function we just made. Next we have to create a function that returns all the categories in our categories table.
For more details about CI database class check the userguide.
Your categorymodel.php should look like this now.
We will be adding another functions in our category model later as we go on to the tutorial.
Creating the Products Model
Now create a new file name it productsmodel.php in your /system/application/models/ folder. And copy the code below.
As you can see our product model is very similar to our category model. The only difference between the two in the model name and the name of the database table.
As we go ahead later on this series maybe we need to add more functions to this models like for example a function that will retrieved all the products by a category id. But that’s it for now.
Now we need to auto load our models so that it will be available globally and saves us time in loading it locally in our controller. To do so, open the file /system/application/config/autload.php. Find $autoload option and add the following line.
Insert a dummy data to your table. You can use my dummy data if you want.
For the categories table.
For the products table.
To test our models if it’s working, open your test controller you just made in the first part of the tutorial. And populate the following code below.
Open your test view created in part 1 of this tutorial and populate the code below.
Now open your browser and go to the address of your application installation, in this tutorial our address is http://localhost/tutorial/. Since we have to view our test controller, type this url http://localhost/tutorial/test/
You must now see a list of categories and products we just have inserted lately to our database.
The third part of the tutorial will be posted soon. Thank you for reading.