user1881482
user1881482

Reputation: 746

Trying to cut down on code in code igniter by making reusable function

In my main controller I have a bunch of functions which access the same set of code. I have been trying to cut down on the size of the controller by making one reusable function that grabs the data I need and then passing it to the function called by that other function. The _getSales() function would be used in about 30 functions within the same controller. Basically instead of adding those lines to each function. As well I will probably add about 5 other variables in that private function when I am done. Right now the sales variable in the view is showing null.

existing controller

function _getSales() {
    $sales = $this -> sale_model -> getSalesById();
    $data['sales'] = $sales;
}

function home() {
    $this -> _getSales();
    $data['pageName'] = 'home';
    $this -> load -> view($currentPage, $data);
}

Upvotes: 0

Views: 138

Answers (1)

Nouphal.M
Nouphal.M

Reputation: 6344

The data['sales'] variable in your _getSales() function is not accessible at your home() function. So try

function _getSales() {
    $sales = $this -> sale_model -> getSalesById();
    return $sales;
}

function home() {
    $data['sales'] = $this -> _getSales();
    $data['pageName'] = 'home';
    $this -> load -> view($currentPage, $data);
}

Also the model function looks like it fetches the sales by id and it doesn't seem that you are passing any arguments to it

Upvotes: 3

Related Questions