Codeigniter 101
#Structure
important folder inside application folder
1)model folder
2)view folder
3)controller folder
4)config-->routes.php
#Calling Default Controller and its view
1)http://ihostidea.com/ci/
or
2)http://ihostidea.com/ci/index.php/welcome
//To create own Controller
Site.php #inside application/controller folder
class Site extends CI_Controller {
function index(){
echo "Hello Site index";
}
}
?>
//Calling of Site Controller
http://ihostidea.com/ci/index.php/site
//creating constructor method
class Site extends CI_Controller {
function index(){
echo "Hello Site index";
}
function myMethod(){
echo "Hello from myMethod";
}
}
?>
//Calling MyMethod
http://ihostidea.com/ci/index.php/site/mymethod
//To change default controller
config->route.php
$route['default_controller'] = "site";
//After changing Site can be called directly
http://ihostidea.com/ci/
Note: 1)name of controller class must start with upper case
2)controller file name must be same as of controller class name
//Creating view
//create myView.php file in application/view folder
My View
//loading view via controller constructor method
Syntax :
$this->load->view('directory-name/name');
$this->load->view('directory-name/name');
Site.php code
class Site extends CI_Controller {
function index(){
echo "Hello Site index";
}
function myMethod(){
$this->load->view('myView');
}
}
?>
//calling myView method
http://ihostidea.com/ci/index.php/site/mymethod
//Creating model
Math.php inside application/model folder
class Math extends CI_Model{
public function add(){
echo 2+3;
}
}
?>
// To call math model inside controller->myMethod
function myMethod(){
//$this->load->view('myView');
$this->load->model('math');
echo $this->math->add();
}
//$this->load->view('myView');
$this->load->model('math');
echo $this->math->add();
}
//To pass data from controller to view
function myMethod(){ //Controller method
$data['title']='My Site';
$this->load->view('myView,$data);
}
//Basic Calculator controller passes data to model model adds and subtracts and then views shows the result
out put
5+2=7
5-2=3
// Controller Site.php
function myMethod(){
$data['title']='My Site';
$data['var1']=5;
$data['var2']=2;
$this->load->model('math');
$data['add']=$this->math->add($data['var1'],$data['var2']);
$data['sub']=$this->math->sub($data['var1'],$data['var2']);
$this->load->view('myView,$data);
}
}
// Views : myView.php
echo "Output";
echo "$var1+$var2=$add";
echo "$var1-$var2=$sub";
?>
//Models : math.php
class Math extends CI_Model{
public function add($x,$y){
return $x+$y;
}
public function sub($x,$y){
return $x-$y;
}
}
//Assignment
function index()
{
$data['title'] = "My Web Page";
$this->load->view('header', $data);
$this->load->view('navigation');
$this->load->view('content');
$this->load->view('footer');
}
//Now create Home and Aboutus Controller and Views
Changes in view_home.php and about_us.php
To remove index.php from middle
Step 1. update system/.htaccess file
with here is my htaccess configuration and do update
RewriteEngine On
RewriteBase /c_intro/
….. rest all same
Step 2. do rewrite module on
Step 3. Goto config.php
$config['index_page'] = ' ';// Remove index.php
//Database
Goto Config folder-> autoload.php
Do changes
Step 1.$autoload['libraries'] = array('database');// Now database will be load automatically
Step 2. Goto config->database.php
provide username and password
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'my_db';
//Getting values from database
//output
a@gmail.com1234
b@gmail.com1234
Controller Name= view
Controller : get_info()
function get_info(){
$this->load->model('get_db');
$data['result']=$this->get_db->get();
$this->load->view('view_db',$data);
}
Model: get_db
class Get_db extends CI_Model{
public function get(){
$query=$this->db->query('select * from info');
return $query->result();
}
}
views: view_db
//print_r($result);
foreach($result as $row){
echo $row->emailid;
echo $row->password;
echo "
";
}
?>
//Insert into database
Controller:
function insert_info(){
$this->load->model('get_db');
$newRow=array('emailid'=>'c@gmail.com','password'=>'1234');
$this->get_db->insert1($newRow);
}
Model:
public function insert1($data){
$this->db->insert('info',$data);//info is table name returns true or false
}
Another way to add multiple record
Controller Changes:
$newRow=array(
array('emailid'=>'f@gmail.com','password'=>'1234'),
array('emailid'=>'g@gmail.com','password'=>'1234')
);
Model Changes: get_db
$this->db->insert_batch('info',$data)
Updating Values
To change password where email id like g@gmail.com
Controller:
function update_info(){
$this->load->model('get_db');
$newRow=array('password'=>'g@');//set password g@
$this->get_db->update1($newRow);
}
Model: get_db
public function update1($data){
if($this->db->update('info',$data,'emailid = "g@gmail.com"'))//info is table name
echo "Record Updated";
}
//Update multiple rows
use update_batch()
Deleting Values
Controller:
function delete_info(){
$this->load->model('get_db');
$newRow=array('emailid'=>'a@gmail.com');//Delete record a@gmail.com
$this->get_db->delete1($newRow);
}
Model: get_db
public function delete1($data){
if($this->db->delete('info',$data))
echo "Records Deleted";
}
0 comments:
Post a Comment