I put this simple tutorial together because when I first started off with Codeigniter I couldn't find any tutorials on how the MVC works together, all I could find were examples using the View and the Controller.
For today's example, let's make a blog! Original, I know.
MVC in CI (CodeIgniter) breaks down like this. Model (M) is where all the SQL queries happen. View (V) is where all the templates and superficial HTML/CSS coding is done. Controller (C) is where the M and V come together, so basically it 'controls' the interchanges between the backend and the frontend. I'm sure my explanation doesn't even remotely do MVC any justice, so for a more coherent explanation go here, Codeigniter Model-View-Controller. Otherwise, let's get started!
You'll need to make 3 files, in different folders.
First thing, we're assuming the layout of our MySQL table looks like this:
This will be the controller file for the 'blog' app, yes it's called an application.
This can really be named anything you want, but I'm just trying to KISS.
Same with the above, it doesn't need to be called 'view_blog', just wanted to keep it simple.
In the 'blog.php' controller, enter this code, follow the comments to see what's going on:
In the 'blog_model.php' model, enter this code, follow the comments to see what's going on:
In the 'view_blog.php' view, enter this code, follow the comments to see what's going on:
So then, after seeing how it's all put together, here's the final product below.
So, there you have it. That's how the Model and the View come together with the Controller, in CodeIgniter.