Tag Archives: codeigniter tutorial step by step

Codeigniter load model in library


Codeigniter load model in library : Sometimes we need to load model in library. It is very simple to load model in library using CI instance. Here in this article we are going to explain how you can load model in library.


Codeigniter load model in library Syntax

Here is an example to load model using CI instance in library-

$CI =& get_instance();
$CI->load->model('your_model_name');
$result = $CI->your_model_name->function_name();

Codeigniter load model in library Example

Suppose we are loading the “Users” model and calling the model function “get_my_profile”

$CI =& get_instance();
$CI->load->model('Users');
$result = $CI->Users->get_my_profile();

Codeigniter Load Model


Codeigniter load model : $this->load->model(‘Your_model_name’); is used to load models in Codeigniter.


Codeigniter Load Model Syntax

$this->load->model('Your_model_name');

Note if you are using subfolders in models. Example your model file is within the “application/models/users”
folder then in this case you need to load the models in this way :

$this->load->model('your_sub_foler_name/your_model_name');

For Example : load model users_model which is inside the users folder.

$this->load->model('users/users_model');
Codeigniter Load Model Example

Codeigniter Load Model Example

If you want to load model in library Read – http://tutorialsplane.com/codeigniter-load-model-in-library/

Codeigniter Load Model Example

1. Create Model :
place it in application/models folder

db->get('users',10);
     return $query->result_array();

  }

}
?>

2. Create Controller :
place it in application/controllers folder

Now Load model from and get data from users table.

load->model('My_first_model');
     // now call model method 
     $data['users'] = $this->My_first_model->getData();
     $this->load->view('MyUsers_view', $data);

  }

}
?>

3. Create view :
place it in application/views folder

Now We are going to print user data from users table.






0){
  foreach($users as $user){
  ?>

Now Access the url “http://localhost/MyUsers/users”

Demo»

Codeigniter Models Intro


Codeigniter Models Intro : Codeigniter Models are specifically used to interact with database. In Short Model is PHP Class which is used for database operations like insert, Update and Delete etc…


Codeigniter models are placed in “application/models” folder.

Codeigniter Models Example

IdNameEmailPhone
No User Found.
db->get('users',10);
     return $query->result();

  }

}

The Above example shoes a simple method to get 10 rows from the table “users”.

Codeigniter Return View as Data String


Codeigniter return view as data string : You can return view as string data in codeigniter


Codeigniter return view as data string Example


$string = $this->load->view('my_view',true);

if the second parameter is set true it will return the complete(including html) data as string.
if it set false it will output the result to the browser.

Codeigniter return view as data string Example

load->view('my_view1',true);
 $string2 = $this->load->view('my_view2',true);
 $string3 = $this->load->view('my_view3',true);
 $string4 = $this->load->view('my_view4',true);

  // process the above string as you wish.

}
}   
?>                    

Codeigniter Pass Data To View


Codeigniter pass data to view : Codeigniter provides functionality to pass data from the controller on views in form of array or object.


Codeigniter pass data to view Example

 'Blog Data Example',
 'heading' => 'Blog Data Example Heading......',
 'content' => 'Demo content starts here.......'

);
$this->load->view('Blogdataexample',$data);
}
}   
?>                    

Try it »

You can pass data as an object :

Example :

$data =  new MyClassName();
$this->load->view('Blogdataexample',$data);

Codeigniter Views Subfolders


Codeigniter views subfolders : You can create sub folders in “application/views” folder and can add your files inside it.


Subfolders Syntax

$this->load->view("YourFolder/filename");

Codeigniter views subfolders example

Example : Create a new folder “layouts” and add a new file “header.php”

$this->load->view("layouts/header");
Codeigniter Views Subfolders

Codeigniter Views Subfolders

Codeigniter Load Multiple Views


Codeigniter load multiple views : You can load multiple views inside the controller function.


Codeigniter loads multiple views and appends them as final output.

Syntax for Codeigniter load multiple views

load->view("header_view");
   $this->load->view("content_view");
   $this->load->view("footer_view");

}

}

So you can load multiple view as above as per your need.

Codeigniter Views Intro


Codeigniter Views : A view in codeigniter is a simple webpage which contains the html which is visible to the user.
Controller is responsible for loading the views or you can load view within a view.


Views are placed under the application/views folder.

Creating Codeigniter Views


Use editor and create the “my_view.php” in the views folder ie. application/views





Welcome to the home page.

More Information Goes Here.......

Loading Views

You Can Load Views As Below :

$this->load->view("my_view");

Note : You Need not to add “.php” while loading the view.

Now We Are going to load above view in

load->view('my_view');

}


}

?>

Demo

Try it »

Codeigniter class not found


Codeigniter class not found : This error occurs basically when your file name and class name does not match.


Suppose you have a controller file named as “User.php” and your class is something different then it will not work.

example :
File name : “User.php”




It will not work because your class name is “Users” instead of “User”

Correct Class name :




This Will Work perfectly.

Make Sure you Are “User” not “user” because of case sensetive.