0

I am in the process of updating my CodeIgniter app from version 2.x to 3.1. Previously, I used to call this function to count the number of non-zero numbers in the passed-in array:

function getCountOfNonZero($array, $d) {
    $NonZeroCount = 0;
    foreach($array as $key=>$value) {
        if(is_array($value)) {
            $d = $key;
            $NonZeroCount += getCountOfNonZero($value, $d); }
        else {
              if($value<>0 & $key<>'RES') {
                $NonZeroCount++;
            }
        }
    }
    return $NonZeroCount;
}

Now that I am trying to call this function in version 3.1 of CodeIgniter, I am getting an error. This is how I call the function:

echo "Count of non-zero is: ".$controller->getCountOfNonZero($myArray);

One thing that is different is that the function is in system\core\Controller.php but this is returning an error whenever I call the function and try to pass in the array.

Is there any reason why I can't pass an array to the core system controller in CodeIgniter?

DanielAttard
  • 3,467
  • 9
  • 55
  • 104
  • You say you are receiving an error? What is the error? Was this method previously not in `system\core\Controller.php` ? Where was it? Just looking at the function, it doesn't seem to depend on anything in your controllers, it might work better as a helper function. – Jeemusu Jun 06 '19 at 01:42
  • Thanks for the comment @Jeemusu. The error I am getting is something about mismatched tags in jQuery. That is probably happening because this function is failing. I think I may have to read-up on helper functions because I am not too familiar with those. First I will try your suggestion below. – DanielAttard Jun 06 '19 at 01:53
  • 1
    you modified the core controller? you should *never* do that. you can however make a core controller in `application/core` that extends the core controller. Google: `MY_Controller` codeigniter – Alex Jun 06 '19 at 01:56
  • @DanielAttard There is one other problem I can see with your code. The methods second paramater `$d` is required, but you are not passing it in in your example usage. If it is an optional parameter you need to specify this when defining the method : `function getCountOfNonZero($array, $d = null) {` – Jeemusu Jun 06 '19 at 02:02

1 Answers1

0

Now that you have moved the function into a class you you will need to use $this->getCountOfNonZero($value, $d); when calling the function from within itself.

function getCountOfNonZero($array, $d) {
    $NonZeroCount = 0;
    foreach($array as $key=>$value) {
        if(is_array($value)) {
            $d = $key;
            $NonZeroCount += $this->getCountOfNonZero($value, $d); }
        else {
              if($value<>0 & $key<>'RES') {
                $NonZeroCount++;
            }
        }
    }
    return $NonZeroCount;
}

Personally I would probably just put the method into a helper file and autoload it, rather than tethering it to your core controller.


Creating a helper method:

  1. As your method relates to arrays, we could just extend the already existing array helper by creating the following file.

application/helpers/MY_array_helper.php

  1. Add your original method to the file.

  2. Auto-load it during system initialization. This is done by opening the application/config/autoload.php file and adding the helper to the autoload array.

  3. Usage it anywhere in your application:

Jeemusu
  • 10,415
  • 3
  • 42
  • 64