Assuming you have set up CodeIgniter and PHP, this post will teach you how to design a simple app using the CodeIgniter framework
Gets the username and age and displays them to the user using CodeIgniter MVC model
NOTE: Please click open the screenshots to make it readable!
1. Create a html form Welcome1.html and put it under the root of Wamp directory (C:\Wamp\www).
This is a simple html form with 2 text fields and a submit button. Note the form action in line 3. This says, on Submit direct the user to /CodeIgniter_2.1.3/index.php/profile/user/
You should be able to access in your machine as http://localhost/welcome1.html
2. Create the model class for the project under Models folder of your codeigniter. I created the Model file profile_model.php under C:\wamp\CodeIgniter_2.1.3\application\models\
This is the Model class for our web application. Model class typically provides data. In our model class we create a function getProfile. It takes two parameters, assigns them to user-defined fields fullName and age and returns the data as an array. “FullName” => $p1 is in the format Key => Value, where Key is a string (note the double quotes) Full Name and Value is $p1
3. Create the Controller class for the project under Controllers folder of your codeigniter. I created the controller file profile.php under C:\wamp\CodeIgniter_2.1.3\application\controllers\
This is the controller class of our application. It extends codeIgniter’s Controller class and has two functions – __construct() and user()
__construct() – Constructor for the class. Will be called when the class is first called. Our application’s construct simply echoes the message “This is the construct”. Thats the one you see in your output page. Typically a construct initializes DB connection and other startup stuff
_user() – A user defined function that first extracts fullname and age from the post method of the welcome1.html form and assigns to variables $p1, $p2. Next, it loads the model profile_model in Line# 13. Next it calls the getProfile method in the model, passing p1, p2 (Extracted username, age from the welcome1.html form) in Line# 14 $profile = $this->profile_model->getProfile($p1, $p2); The model then returns to the controller, full name and age put together in an array.
Now create a variable $data as an array. Initialize the array using key=>value format. Here i do it as “profile” =>$profile. where the string “profile” is the name I give for the key, it can be anything. This key is going to be used in the view page.
$profile is the array returned by the model class. (Line#17)
load the view profile_view and pass $data array to it (Line# 18)
4. Create the View for the project under Views folder of your codeigniter. I created the View file profile_view.php under C:\wamp\CodeIgniter_2.1.3\application\views\
This retrieves and displays the Name and Age from the key “profile” which we created in the controller class.
Run the program
1. open http://localhost/welcome1.html
2. Enter the username and age. Hit Submit
3. Now you will be directed to http://localhost/CodeIgniter_2.1.3/index.php/profile/user/
4. This page displays the output – the full name entered and age. Also it displays a text called “This is the construct”.