Welcome Guest, Not a member yet? Register   Sign In
Calling controller function within controller?
#1

[eluser]delay[/eluser]
I am new to codeignitor and MVC but so far I really like it. I currently am not sure the best way to structure or handle part of my code.

In my "amazon" controller I have a function, like this which loads several functions from the model.

Code:
function get_keyword_search($searchterm)
    {
        parent::Controller();
        $this->load->model('amazon_model');
        $searchterm = urlencode($searchterm);
        $xmlarray = $this->amazon_model->get_keyword_search($searchterm);
        foreach ($xmlarray->Items->Item as $list) {
        $ASIN = $this->amazon_model->extract_itemsearch_book($list);
            if ($ASIN) {
            $this->amazon_model->extract_itemsearch_comments($list);
            $this->amazon_model->extract_itemsearch_similar_books($list, $ASIN);
            $this->amazon_model->extract_search_listmania_lists($list);
            $this->amazon_model->extract_tags($xmlarray, $ASIN);
            }
        }
    
    }

Now I want to call this and several other controller functions within another controller function.
Code:
function check_amazon ()
    {
           $this->db->select('SearchQuery');
        $query = $this->db->get('SearchQueue');
        foreach ($query->result() as $row)
        {
            $SearchQuery = $row->SearchQuery;
            get_keyword_search($SearchQuery); //??? What to do here to call the other function
        }
      }

I am very new to MVC and I understand I shouldn't really use DB queries outside of the model class. I could make the whole check_amazon function part of the model but my understanding is you also shouldn't call controllers from the model. So my question is where is the best place to call a bunch of controller functions? Can you call a controller function within a controller? How do you call the controller within the controller?

Thanks very much for any help.
#2

[eluser]Muhammad Faisal Shabbir[/eluser]
In very simple if u want to use same controller function in any other function of the same controller you just $this operator with the function name . Or if u want to use same model function in same model differnt function just use $this with function name .
If u want to use different controller functions or also different models in particular controller then for model you have to load all the models in that particular model or if u want to use different controller then inheritance will help u.
#3

[eluser]gunter[/eluser]
you could write a "base controller" and extend your controller from that... search for base controller
or write a library for that...
#4

[eluser]Grahack[/eluser]
Note that you can prefix some functions with an '_' underscore. This prevents the function to be triggered by http://site.com/index.php/controller/function (see the private functions section in the docs).
#5

[eluser]delay[/eluser]
Thanks for all the tips and fast response... just what I needed.




Theme © iAndrew 2016 - Forum software by © MyBB