Models in Code Igniter PART-2

0
2657

In the last section we went through the theory of Models in codeigniter and today we will try using them practically in this Models in CodeIgniter PART-2 tutorial.

We will create a database in mysql and connect it to our application and then perform all manipulations in our model class.

So follow the steps:

  1. Creating database in mysql:
  • To create a database in mysql, open your browser and write http://localhost/phpmyadmin in the address bar.
  • A login page will appear, so put root as the username and the password you have as the password.phpmyadmin_home_page

               fig 1

  • Now click on New option which is on the left side. You will get a page asking you the database name. So provide your database name and then click on Create
  • After clicking on Create button, you will get a message of successful creation of your database.
  • Now you will find your database name at the left side in the list below the New option pointed to in fig 1 (Consider our database name as employees_db).
  • Click on your database name, you will be asked for your table name and the number of columns it will contain.
  • Put the table name say emp_details and no. of columns say 5 and then click on Go
  • You will get the page to provide columns of the table. You will be asked to provide the name, type, and length etc. of the columns. So provide the information of the columns to be created and then click on Save Your table will be created.
  • OR you can write a create table query to create a table.
  • Now our employees_db database with emp_details table is ready.
  • Put some values in the table. I have entered 2 records in the table emp_details.

     2. Providing connection values in the database.php file:

  • We have studied in the last section that we need to provide the username, password and database name in the php file in the application/config folder inside your CodeIgniter folder.
  • So open the database.php file and provide the username, password and database name for the respective array key and then save and close the file.
  • Next thing to be done is to add the word database to the libraries array in the php file.
  • This is automatic connection used by us, similarly you can connect the database manually by providing the values of username, password and database name in the php file in application/config folder and then calling the function $this->load->database() in the constructor of model class or in any function where you want to perform manipulations with database.

       3. Creating a model file:

  • Now let us create a file for the model class in the application/models
  • You can even create another separate folder inside the application/models folder for your model classes.
  • So open a new notepad++ document and save it as emp_model.php in the application/models folder.
  • Write the following code inside the emp_model.php file:

     
  •  The model class name is Emp_model which inherits from CI_Model class.
  • We have two functions in Emp_model class: the first function count_emp() will count the number of employees in the emp_details table of our database employees_db. And the second function get_all_records() will return all the records in the emp_details
  • We have used Active Record functions count_all() and get() to manipulate with the database.

      4. Creating view files:

  • To display the contents of database on the browser we will require view files. So let us create a header and footer template and a content page to display the database table values.
  • First we will create templates: For this create a separate folder named templates in the application/views
  • Now open a new notepad++ document and save it as header.php in the newly created templates folder.
  • Write the following code in header.php file:
  • Save the file and close it.
  • Now open a new notepad++ document and save it as footer.php in the newly created templates folder.
  • Write the following code in footer.php file:

     
  • Now we want to create the page in which we want to display the database table contents. So to store the content page create a new folder in application/views folder named as body_pages.
  • Now open a new notepad++ document and save it asphp in the newly created body_pages folder.
  • Now write the following code in emp_info.php file:

     
  •  In this file we have displayed the title, number of employees whose records are entered in the emp_details table of employees_db
  • Also we have displayed the records from emp_details table in an html table format.

      5. Creating a Controller file:

  • Controller file is an important file which loads all the models and views.
  • So to create the controller for our application, open a new notepad++ file and save it as php in the application/controllers folder.
  • Write the following code in the emp_data.php file:

     
  • The controller class is named as Emp_data which inherits from CI_Controller class.
  • It has an index() function written in it which loads the model class and all the views.
  • The Loader class function $this->load->model() is used to load the model class in the controller. Name of the model is provided inside the function as shown below:

     
  •  We have assigned some values to the $data array as shown below:

 

  •  The title Employees Details is provided as the title in the array. Similarly the no. of employees and their details are obtained by calling the functions written in the emp_model.php file.
  • The functions written in the model class are called as $this->model_class_file_name->function_name.
  • Next whatever data accessed from the database should be displayed in the browser, so for that view files are loaded and the $data array is passed to them if needed as shown below:

    6. View the output:
  • To view the output, open your browser and write the following address in the address bar of your browser:

http://localhost/CodeIgniter/index.php/emp_data

  • emp_data is the controller file name.
  • The output is shown below:

final_output

 fig 2

  • In the above figure we can see the title passed in the $data array i.e. Employees Details.
  • We see number of employees i.e. 2 and the records in the emp_details table in the database are also displayed.
  • Similarly, we can perform any operation like insert, update, and delete using the active record functionality.

 

Thus we studied practically the connection and application of database with the model in codeigniter in this Models in CodeIgniter PART-2 tutorial.              

LEAVE A REPLY

Please enter your comment!
Please enter your name here