Tech Programming Ideas is one of the best places on the programming for programmers. Learn coding with Tech Programming Ideas tutorials. We are covered android programming, php, yii2 framework, javascript, mysql, vb.net etc.
A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy.
Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework, the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have not read the Controllers page you should do so before continuing.
Creating a View
Using your text editor, create a file called blogview.php, and put this in it:
Then save the file in your application/views/ directory.
Loading a View
To load a particular view file you will use the following method:
$this->load->view('name');
Adding Dynamic Data to the View
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method. Here is an example using an array:
Models are PHP classes that are designed to work with information in your database.
You might have a model class that contains functions to insert, update, and retrieve your blog data.
Anatomy of a Model
Model classes are stored in your application/models/ directory.
The basic prototype for a model class is this:
classModel_nameextendsCI_Model{}
Where Model_name is the name of your class.
Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.
The file name must match the class name. For example, if this is your class:
classUser_modelextendsCI_Model{}
Loading a Model
Your models will typically be loaded and called from within your controller methods.
$this->load->model('model_name');
If your model is located in a sub-directory, include the relative path from your models directory. For example, if you have a model located at application/models/blog/Queries.php you’ll load it using:
$this->load->model('blog/queries');
Once loaded, you will access your model methods using an object with the same name as your class:
You can tell the model loading method to auto-connect by passing TRUE (boolean) via the third parameter, and connectivity settings, as defined in your database config file will be used:
$this->load->model('model_name','',TRUE);
You can manually pass database connectivity settings via the third parameter:
Example: Controller <?php class Blog extends CI_Controller { public function index() { //gather information here from models $this->load->model('blog_model'); echo $this->blog_model->test_blog(); } } ?> Model <?php class Blog_model extends CI_Model { function test_blog() { echo "This is model test funtion"; } //Database function write here } ?> Here is an example of what such a model class might look like:
classBlog_modelextendsCI_Model{public$title;public$content;public$date;publicfunctionget_last_ten_entries(){$query=$this->db->get('entries',10);return$query->result();}publicfunctioninsert_entry(){$this->title=$_POST['title'];// please read the below note$this->content=$_POST['content'];$this->date=time();$this->db->insert('entries',$this);}publicfunctionupdate_entry(){$this->title=$_POST['title'];$this->content=$_POST['content'];$this->date=time();$this->db->update('entries',$this,array('id'=>$_POST['id']));}}
Note
The methods in the above example use the Query Builder database methods.
Then save the file to your application/controllers/ directory.
Now visit the your site using a URL similar to this:
example.com/index.php/blog/
Methods
In the above example the method name is index(). The “index” method is always loaded by default if the second segment of the URI is empty. Another way to show your “Hello World” message would be this:
example.com/index.php/blog/index/
Add a new method to your controller:
<?phpclassBlogextendsCI_Controller{public function index()
{
echo "Hello World!";
}
public function test()
{
echo "Test method";
}}
Now load the following URL to see the comment method:
example.com/index.php/blog/test/
Call one method to anther
<?phpclassBlogextendsCI_Controller{public function index()
{
echo "Hello World!";
$this->test();
}
public function test()
{
echo "Test method";
}
}
Passing URI Segments to your methods
If your URI contains more than two segments they will be passed to your method as parameters.
For example, let’s say you have a URI like this:
example.com/index.php/blog/index/32/Shaneesh
Your method will be passed URI segments 3 and 4 (“sandals” and “123”):
<?php
class Blog extends CI_Controller {
public function index($id,$name)
{
echo $id ." - " ,$name;
}
}
?>
Defining a Default Controller
To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller']='blog';
by default loaded index method in blog controller.
Class Constructors
If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:
parent::__construct();
The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.
Example:
<?phpclassBlogextendsCI_Controller{publicfunction__construct(){parent::__construct();// Your own constructor code}}
Private methods
privatefunction_utility(){// some code}
Processing Output
CodeIgniter has an output class that takes care of sending your final rendered data to the web browser automatically.
publicfunction_output($output){echo$output;}
Remapping Method Calls
CodeIgniter permits you to override this behavior through the use of the _remap() method:
CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.
CodeIgniter is an Application Development Framework - a toolkit - for people who build web sites using PHP. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount of code needed for a given task.
Who is CodeIgniter For?
CodeIgniter is right for you if:
You want a framework with a small footprint.
You need exceptional performance.
You need broad compatibility with standard hosting accounts that run a variety of PHP versions and configurations.
You want a framework that requires nearly zero configuration.
You want a framework that does not require you to use the command line.
You want a framework that does not require you to adhere to restrictive coding rules.
You are not interested in large-scale monolithic libraries like PEAR.
You do not want to be forced to learn a templating language (although a template parser is optionally available if you desire one).