#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,664
    Rep Power
    171

    How to call multiple methods of a controller


    Hello;

    In MVC, the url below means: Load method hotel_details located in controller hotels and send 1234 as the method argument
    Code:
    www.website.com/hotels/hotel_details/1234
    Now what happens if you want to load more than one function in controller?

    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    this wouldn't make any sense. The path specifies a certain "location", so obviously you can't have more than one. How would that look like? "Show me the hotel details and the main page at the same time"?

    But of course the controller can call any number of methods within that "location method".

    And you could add further parameters to the URL.
    Last edited by Jacques1; November 21st, 2012 at 06:26 PM.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,664
    Rep Power
    171
    Originally Posted by Jacques1
    Hi,

    this wouldn't make any sense. The path specifies a certain "location", so obviously you can't have more than one. How would that look like? "Show me the hotel details and the main page at the same time"?

    But of course the controller can call any number of methods within that "location method".
    Hello Jacques1; Beofre I write: I have no doubts there is a big aha moment coming my way

    Ok, I am sure you have already realized my questions are related. It is most likely a simple lack of understanding mvc. Anyways, lets work:

    Well of course, I don't want to show 2 pages at the same time. But regardless of controller I load, I need to build different sections of the webpage using other methods. For example dynamic drop down in the form, dynamic footer,...and there are different methods that build each of those. Now when I visit www.website.com/hotels/hotel_details/123 ,,,, it is simple to just load hotels controller, call hotel_details and send 123 as argument. Something like:
    PHP Code:
    controller Hotels
        
    {
            public 
    method hotel_details($hotel_id)
                {
                    
    $this->load->model('hotel_details_model');
                    
    $hotel_info $this->hotel_details_model->hotel_details($hotel_id);
                    if(
    $hotel_info)
                        {
                            
    $list_places[] = array('title'=>$val['title']), 'AID'=>$val['AID']);
                            
    $this->smarty->assign("places"$list_places);
                        }
                    
    $this->smarty->view('places_view');    
                }
        } 
    But now how would you call the rest of the methods?
    PHP Code:
    public function load_drop_down()
         {
        
    $this->load->model('cities_model');
                    
    $cities $this->cities_model->lost_details();
                    if(
    $cities)
                        {
                            foreach(
    $citiesas $val)
                                {
                                    
    $cities_list[] =  array('title'........
                                }
                        }
        } 
    I assume you call these functions from the hotel_details method. Or maybe you place these methods in another controller and call the other controllers?

    Thank you
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    Here's a CI controller from a project I did, you can see how I build the data up and then pass it into various views:

    PHP Code:
    function index($game_id) {
            
    $this->load->helper(array('form''file'));

            
    $data['cards'] = $this->card_model->get_cards_by_game_id($game_id);

            
    $data['images'] = $this->card_model->get_images($_SESSION['slug']);
            
    $data['game_id'] = $game_id;

            
    $statements asPairs($this->statement_model->get_statements_by_game_id($game_id), 'statement');
            
    $data['statements'] = array('0' => 'Choose Statement');
            
    $data['statements'] = $data['statements'] + $statements;

            
    $categories asPairs($this->category_model->get_categories_by_game_id($game_id), 'category_name');
            
    $data['categories'] = array('0' => 'Choose Category');
            
    $data['categories'] = $data['categories'] + $categories;

            
    $header['css'][] = '/css/cards.css';

            
    $header['messages'] = $this->flash->display();
            
    $header['javascript']['vars']['game_id'] = $game_id;
            
    $header['javascript']['vars']['current_card_id'] = '0';
            
    $header['javascript']['vars']['card_default_image'] = CARD_IMAGE_DEFAULT;

            
    $footer['javascript'][] = '/js/jquery.jeditable.mini.js';
            
    $footer['javascript'][] = '/js/cards.js';

            
    $this->load->view('admin/header'$header);
            
    $this->load->view('admin/cards/cards'$data);
            
    $this->load->view('admin/footer'$footer);
        } 
    If you wanted to create methods in your controller to construct your header or navbar or whatever, I believe that CI will load views in the order you call them.

    e.g.

    PHP Code:
    function index()
    {
           
    $this->displayHeader();
           
    $this->load->view'bodytemplate' );
    }

    function 
    display_header()
    {
          
    $model_data $this->my_model-get_data();
          
          
    // do some data manipulation
         
    $this->load->view'header'$model_data );

    I haven't used smarty with codeigniter but if you just have a single template and need to pass in data into a single template then you could dos omething like:


    PHP Code:
    function index()
    {
         
    $data = array();
         
    $data['header_data'] = $this->city_model->get_cities();
         
    $data['body_data'] = $this->more_model->get_more_data();

         
    $this->smarty->view'myview'$data );

  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,664
    Rep Power
    171
    Originally Posted by msteudel
    ...
    Hey thank you for posting this. I just wasn't sure if variables assigned using $this->smarty->assign("places", $list_places) ; stay alive within the class and they all get passed to the view using $this->smarty->view('places_view') ; from any other methods within the class. That was the trick: All assigned variables using $this->smarty->assign get stored in an array and can be passed to view. I am not sure how it happens but it works!!

    For example in the code below I assign 3 variables from different methods and at the end I send them to the view from the last view called view_page:
    PHP Code:
    class Share_accomodation extends CI_Controller
        
    {
            public 
    $suburb="";
            
             public function 
    index()
               {
                    
    $this->cities_dropdown();
                    
    $this->show_ads();
                    
    $this->view_page();
                }
            public function 
    cities_dropdown()
                {
                    
    $this->load->model('cities_model');
                    
    $cities $this->cities_model->load_cities();
                    foreach(
    $cities as $val)
                        {
                            
    $australia[] = array('name'=>$val['name'], 'id'=>$val['id']);
                        }
                    
    $this->smarty->assign("australia"$australia);
                }
            public function 
    show_ads()
                {
                    
    $this->load->model('places_model');
                    
    $places $this->places_model->load_places();
                    if(
    $places)
                        {
                            foreach(
    $places as $val)
                                {
                                    if(
    $val['suburb']!=$val['city'])
                                        {
                                            
    $this->suburb ", ".$val['suburb'];
                                        }
                                    else
                                        {
                                            
    $this->suburb="";
                                        }
                                    
    $list_places[] = array('title'=>html_escape(ucwords(strtolower($val['title']))), 'city_suburb'=>html_escape(ucwords(strtolower($val['city'].$this->suburb))), 'date_added'=>$val['date_added'], 'AID'=>$val['AID']);
                                }
                        }
                    
    $this->smarty->assign("title""Share accomodation in Australia");
                    
    $this->smarty->assign("places"$list_places);
                }
            
             public function 
    view_page()
                {
                    
    $this->smarty->view('places_view');
                } 
        } 
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    A general remark:

    Where and how exactly you call those auxiliary methods for the drop down etc. doesn't really matter. I'm sure there are many different solutions, and we could probably argue all day long which one is the best.

    What's important is that those methods are purely internal. The user does not care which methods of your PHP controller class you have to call to render the page. All he wants is get the details of a certain hotel. So the URL is supposed to contain something like "hotel", "details" and the ID and nothing else. All the internal stuff doesn't belong in the URL.

    Think of a plain HTML page you generate with a simple PHP script. It would never occur to you to pass internal stuff like the logo filename or formatting rules as URL parameters:
    Code:
    www.myhotels.com/hotel.php?id=42&logo_filename=prettylogo.jpg&footer_width=200px
    Because that's simply none of the user's business. So don't do it with MVC either.
  12. #7
  13. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,664
    Rep Power
    171
    Originally Posted by Jacques1
    Think of a plain HTML page you generate with a simple PHP script. It would never occur to you to pass internal stuff like the logo filename or formatting rules as URL parameters:
    Code:
    www.myhotels.com/hotel.php?id=42&logo_filename=prettylogo.jpg&footer_width=200px
    Because that's simply none of the user's business. So don't do it with MVC either.
    That is understandable and makes sense. But how about dynamic drop downs that are also a part of the design? For example the drop down (list of cities) in this page's header? So don't do it with MVC either ...How else can I do it?

    Thanks
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    Originally Posted by zxcvbnm
    That is understandable and makes sense. But how about dynamic drop downs that are also a part of the design? For example the drop down (list of cities) in this page's header? So don't do it with MVC either ...How else can I do it?

    Thanks
    I would call my model to retrieve the list of cities, and then pass it into my view.

    I think if you have a lot of massaging to do to e results to get them ready for a drop down, then using additional methods to format the data is appropriate.

    I might do something like:
    PHP Code:

    $header
    ['cities'] = $this->format_citiesthis->model->get_cities() );
    $header['current_city'] = $current_city// maybe you want to make the drop down select the current city

    this->load->view'header'$header );

    $data[stuff] = this->model->get_data();

    this->load->view'body'$data); 
    If you had bits of data that affected the flow, say a hierarchical drop downs, I think passing them thru the URL is a great way to do it.

    E.g. Domain.com/controller/search/country/state/city

    Would be excellent way to pass those pieces of information. It would allow a user to email that link on to their friends and they could get to the same state.

    My search method would have three argumentse in the above example

    PHP Code:
    Function search$country$state null$city null){}; 
    PS sorry for any typos I'm typing on an iPad.

IMN logo majestic logo threadwatch logo seochat tools logo