Before I proceed to build the three view files required to complete the PHP application created in the previous tutorial of this series, it would be useful to list the signatures of its model and controller classes, to help you recall more quickly how they were built. That said, below I included these two classes. Have a look at them, please: (‘user_model.php’ file – located at /application/models/ folder) <?php class User_model extends Model{ function User_model(){ // call the Model constructor parent::Model(); // load database class and connect to MySQL $this->load->database(); } // fetch all users function getAll(){ $query=$this->db->get('users'); // return result set as an object array return $query->result(); } // fetch some users based on a predefined condition function getWhere($field,$param){ $this->db->where($field,$param); $query=$this->db->get('users'); // return result set as an object array return $query->result(); } // get total number of users function getNumber(){ return $this->db->count_all('users'); } } ?> (‘webpage.php’ file – located at /application/controllers/ folder) <?php class WebPage extends Controller{ function WebPage(){ // load controller parent parent::Controller(); // load users model $this->load->model('User_model'); // load some helpers here } // generate web page using partial sections function index(){ // generate header section $data['header']=$this->load->view('header_view',array('header'=>'Header Section'),TRUE); // generate content section $data['content']=$this->load->view('content_view',array('users'=>$this->User_model->getAll()),TRUE); // generate footer section $data['footer']=$this->load->view('footer_view',array('footer'=>'Footer Section'),TRUE); // generate full web page $this->load->view('main_page',$data); } } ?> Undeniably, the tasks performed by both the model and controller classes are very easy to follow. First, the model has a few simple methods for counting and retrieving rows from a “user” MySQL table. On the other hand, the controller implements an “index()” method that uses the model’s “getAll()” method to generate the contents section of a web page. Of course, it’s valid to point out here that each part of the web page being created is stored on a $data array, which is finally passed to a “main_page.php” view. All of these tasks, accomplished by the “WebPage” controller, naturally imply that the views need to be created accordingly. Therefore, in the following section I’m going to build the first three views, which will be tasked with generating the header, content and footer sections of the web document. To learn how this will be achieved, you’ll have to click on the link that appears below and read the section to come.
blog comments powered by Disqus |
|
|
|
|
|
|
|