How to use Model in Codeigniter?

Hey your are using Codeigniter framework? stuck up with model? here you get the solution.
In this section we only discuss about how to use model in Codeigniter, if you are looking for more Codeigniter functionalities here you can find below.
1) How to retrieve data from MySql and display in Codeigniter view?
2) Steps to create Forgot password form using Codeigniter
3) How to use ajax post method in Codeigniter form insert?

How to use model in codeigniter?

When developing web application we use three files i.e. Model, view and controller(MVC), here view is used to display your responses from database and controller is used to write database operations, and finally CRUD operations is written in model, which can be easily reused in different controllers, they simply represent your data from database and other data stores.

When you unzip your Codeigniter project you can find model folder under application folder.you can see the folder structure in below image. Now create your model as User_service.php

How to use model in codeigniter

Here you create a class with name User_service and extends class to CI_Model which loads the database library, and enables database class available by simply using $this->db . when you clearly observe the above model code, you notice which is similar to controller as you created.
Now it time to query the database , Before that we need to create database schema and run this schema in mysql, finally you created the table. You can simply copy below code and run in your mysql database.

So finally we are travelling in right direction, now you know, we have set up model and database, to retrieve data from database what we need? Don’t think much, here we need method to get all data from our database.
Now let’s see the controller code below, how it is passing variables to model and getting query results from database.

In the above code we had loaded model in the constructor method, which helps to call model throughout the controller class, and also created method called disply_user_profile(), by using this method we can get the data of user from database, in this method we called model method get_user_details() to fetch database records.

Here we are passing user_id as a parameter to get active user record from database, How we are getting user_id here? Its simple we are storing user_id into session, Please go through how do session work?, Let’s check the syntax of the model.

How to use model in codeigniter

Now let’s check user_service.php model below

In the above code we simply passed user_id value into db query, here get_where() is a where condition, it’s simply check for user_id in the database and fetch the record and return the matched record values to controller method, here in my example user_id is dynamically getting when user logged in, if you want to work this directly enter static value in where condition as shown below.

Now in the controller method we stored query result and pass the query string to view.php,now we need to create view to display all values we get from controller.

Finally we made use of model, now go to browser and test the output using below syntax.

http://localhost/project_name/index.php/controller_name/method_name/

Before typing above url please start your Apache and mysql servers to work. You get user details.
Thanks for reading article.

Saritha Reddy Syamala

Does my profile interest you? You have any article writing requirements or if you want a blogger, do get in touch with me! I will be glad to write blogs, articles, reviews for you.Send me the details using contact form! Do drop me a line and your phone number and I will be there to help you.I am a freelance content writer since a long time.

More Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

eight + 2 =