True to form, building a controller class that fetches all the movies included in the previous “movies” MySQL table, and also allows users to enter comments about each of them, simply involves defining a different method for performing each of these tasks. However, you will gain a better understanding of the way that this controller class is going to work if you examine its signature, which has been included below. Take a look at it, please: class Movies extends Controller{ function Movie(){ // load controller parent parent::Controller(); // load 'MovieModel' model $this->load->model('MovieModel'); // load helpers $this->load->helper('url'); $this->load->helper('form'); } // display all movies function index(){ $data['title']='Movies List'; $data['movies']=$this->MovieModel->fetchAllRows('movies'); // load 'movie_view' view $this->load->view('movie_view',$data); } // display all comments function comments(){ $data['title']='Comment List'; $data['comments']=$this->MovieModel->fetchRow($this->uri->segment(3),'movie_id','movie_comments'); // load 'moviecomment_view' view $this->load->view('moviecomment_view',$data); } // insert comment function insert_comment(){ $this->MovieModel->insertRow('movie_comments',$_POST); redirect('movie/comments/'.$_POST['movie_id']); } } Although the definition of the above “Movie” controller looks pretty intimidating at first, it's actually pretty simple to understand. First, its constructor loads all of the source classes and functions that will be used within the management system; this includes the corresponding model, as well as the “url” and “form” helpers that you learned in previous articles of this series. Now, take a look at the “index()” method. As you’ll recall, it will be called automatically by Code Igniter when implemented, and it is tasked with displaying on screen all of the movies included in the “movies” MySQL table defined before. As you can see, this method first fetches all of the movies from the aforementioned table by means of the model, and lastly it embeds this data into a view file called “movie_view.php” for displaying purposes. For right now, don't worry about how this view looks, since this topic will be discussed in depth in the following section. At this level, you hopefully grasped the logic implemented by the pertinent “index()” method, right? So focus your attention now on the one called “comments().” As its name suggests, this method is responsible for fetching all of the comments that belong to a specific movie. This task is performed by means of a conditional WHERE SQL statement, which is logically hidden behind the model’s API. In addition, you should notice that this method retrieves the comments made on a particular movie by using a parameter passed in within the URL, whose value is returned by the following expression: $this->uri->segment(3) Actually, Code Igniter will automatically load a class called “uri” with each controller defined, so in this case its “segment()” method is utilized to retrieve the ID of the movie that has received a comment. And finally, the “insert_comment()” method again uses the model’s API, this time for adding a new entry to the “comments” MySQL table created in the previous section. Of course, the text and author that correspond to each comment are collected via a post HTML form, therefore the following expressions: $this->MovieModel->insertRow('movie_comments',$_POST); redirect('movie/comments/'.$_POST['movie_id']); first add the comment in question to the corresponding “comments” MySQL table, and then redirect users back to the comment’s web page. Now, before I forget, please save the controller class to the Code Igniter /system/application/controllers/ as “movies.php” folder for further use. So far, so good, right? At this point, you should have a clear idea of how the previous “Movies” controller class does its thing. Also, it’s possible that you still have some doubts regarding the way that movies and comments are displayed on the browser, or even how new comments are submitted by a user. However, you shouldn’t be too concerned about this. In the next section, I’ll be listing all of the view files that are required to perform all the aforementioned tasks. Want to see how this will be done? Click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|