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
<?php class User_service extends CI_Model { public function __construct() { $this->load->database(); } }
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.
CREATE TABLE First_App( user_id int(11) NOT NULL AUTO_INCREMENT, Name varchar(128) NOT NULL, Email varchar(128) NOT NULL, Bio text NOT NULL, PRIMARY KEY (user_id), );
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.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?> <?php class User extends CI_Controller { public $data; public function __construct() { //Core controller constructor parent::__construct(); $this->load->model('user/user_service'); $this->load->library('form_validation'); $this->load->library("pagination"); } public function display_user_profile() { $user_id=$this->session->userdata('user_id'); $data['query']=$this->user_service->get_user_details( $user_id); if (empty($data['query'])) { show_404(); } $data['Name'] = $data['query']['Name']; $data['Email'] = $data['query']['Email']; $data['Bio'] = $data['query']['Bio']; $this->load->view('user/view',$data); } }
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.
Now let’s check user_service.php model below
class User_service extends CI_Model { public function __construct() { parent:: __construct(); } public function get_user_details($user_id) { return $this->db->get_where("First_App", array('user_id' => $user_id)); } }
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.
$user_id=1; // user_id should be available in database record to fetch record. return $this->db->get_where("First_App", array('user_id' => $user_id));
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.
<?php echo '<h2>'.$query['Name'].'</h2>'; echo $query['Email']; echo $query['Bio'];
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.
Leave A Comment