codeigniter l3 model & active record & template
TRANSCRIPT
Model component
• What is Model.
• Create Model class.
• Load and call Model functions.
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
What is Model?
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Models are PHP classes that are designed
to work with information in your database.
• contains functions to insert, update, and
retrieve .
• Class names must have the first letter
capitalized with the rest of the name
lowercase.
Create Model class
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Create model class.
class Model_name extends CI_Model {
function __construct() {
parent::__construct();
}
function your_fanction(){
//your code
}}
Load and call Model functions
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• load model class.
$this->load->model(‘model_name');
• Call model functions.
$this->model_name->function();
Active record
• What is active record.
• Selecting data.
• Inserting data.
• Updating data.
• Deleting data.
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
What is Active record
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• This pattern allows information to be
retrieved, inserted, and updated in your
database with minimal scripting.
• allows you to create database
independent applications.
Selecting data
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Selecting data:
$query = $this->db->get(‘table_name');
foreach ($query->result() as $row){
echo $row->field_name;}
• Other Methods:
$this->db->select();
$this->db->where();
$this->db->join();
Inserting data
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Inserting data:
$this->db->insert(‘table‘,$data);
Updating data
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Updating data:
$this->db->where(‘id’, 4);
$this->db->update(‘my_table’,$data);
Or
$this->db->update(‘tabel’,$data,”id= $id”);
Deleting data
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Deleting data:
$this->db->where($id, 4);
$this->db->Delete();
Template
• Divide your template to 3 parts: Header,
Content and Footer.
• Put each one in a single view file
• Make main view page.
• Load the parts in the main view page.
• Enjoy :D
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
Template
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Hello_world.php
<html>
<head> <title>Hello World!</title> </head>
<body>
<p>Hello world!</p>
</body>
</html>
Template
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Display hello world page
class Hello_world extends CI_Controller {
public function index() {
$this->load->view('hello_world');
}
}
Template
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Header.php
<html>
<head><title>Hello World!</title></head>
• Hellow_world.php
<p>Hello world!</p>
• Footer.php
</body></html>
Template
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Display hello world page
class Hello_world extends CI_Controller {
public function index() {
$this->load->view('header');
$this->load->view('hello_world');
$this->load->view('footer');
}
}
Template
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• template.php
<?php $this->load->view(‘header’);?>
<?php $this->load->view($main_content);?>
<?php $this->load->view(‘footer’);?>
Template
Mhd Opada Al-Bosh & Mhd Tahsin Al-Shalabi
• Display hello world page
class Hello_world extends CI_Controller {
public function index() {
$data[‘main_content’]='hello_world’;
$this->load->view(‘template’, $data);
}
}