In the section that you just read, I demonstrated how to build a model class, which came in helpful for retrieving data from a “users” MySQL table. It’s time to build the corresponding controller, which will use the API of the model to directly access this user-related data. Please examine the signature of this brand new controller class, which looks like this: class Users extends Controller{ function Users (){ // load controller parent parent::Controller(); // load 'Users' model $this->load->model('Users'); } function index(){ // store data for being displayed on view file $data['users']=$this->Users->getUsers(); $data['numusers']=$this->Users->getNumUsers(); $data['title']='Displaying user data'; $data['header']='User List'; // load 'users_view' view $this->load->view('users_view',$data); } } As shown above, I created the previous user controller class by extending the default controller that comes bundled with Code Igniter. Again, don’t forget to save this file to the /system/application/controllers/ folder of Code Igniter for later use. Besides, there are some important details that you should notice with reference to the way that this class has been defined. First, note how the controller uses the corresponding loader class for loading the user model defined in the prior section. See how easy it is to instruct a controller to load a particular model? I guess you do! The required syntax for performing this task is the following: $this->load->model('model_name'); Simple and intuitive, right? Now that you have learned how to include a determined model from within a controller class, it’s time to pay attention to the implementation of the “index()” method, which actually is very interesting. As you can see, it uses the model’s API to fetch all the rows of the pertinent “users” database table, along with the number of records contained in the table in question. In addition, it’s very important to stress here the notation used for accessing the model’s API. Here’s an example of how to do this: $data['users']=$this->Users->getUsers(); In this case, since the model has been named “Users,” Code Igniter automatically creates an object that can be referenced as $this->Users. Naturally, if there’s a model called “Blog,” it should be called within the controller as $this->Blog. That’s not too difficult to understand, right? Now, returning to the implementation of the “index()” method, it finishes its execution by defining some additional parameters, which are first stored in the $data array, and then passed to the corresponding view file for display purposes. Also, an additional explanation is in order here: please, notice how the controller reuses the loader object for loading the view and populating it with user-related data. So far, so good. At this stage, I showed you how to build a controller class, which uses the model’s API, to fetch some rows from a sample “users” MySQL table, and to embed this data into a view file. Nonetheless, if you’re like me, then at this moment you’re wondering how this view file actually looks. Well, in response to that question, in the following section I’ll be creating this view, completing the development of this MySQL-driven application. Please, click on the link below and keep reading. We’re almost done!
blog comments powered by Disqus |
|
|
|
|
|
|
|