Error in Count()

Asked

Viewed 145 times

1

I need to put a website on the air (I didn’t make the site), and I need to adjust some things on it.

I am facing a problem in a dynamic menu: when I am on the home page it gets ok, but if I browse the site the following error happens:

A PHP Error was encountered Severity: Warning Message: Count(): Parameter must be an array or an Object that Implements Countable Filename: views/layout.php Line Number: 170 Backtrace: File: D: Mampdois htdocs vandario application views layout.php Line: 170 Function: _error_handler File: D: Mampdois htdocs vandario application controllers Product.php Line: 589 Function: parse File: D: Mampdois htdocs vandario index.php Line: 292 Function: require_once

Follows my source:

$menus = $this->session->userdata('menus');
if (count($menus) > 0) {
    for ($i = 0; $i < count($menus); $i++) {
        if (($menus[$i]['id_pai'] == '' or $menus[$i]['id_pai'] == 0) and $menus[$i]['titulo'] != '') {
            echo '<li><a href="' . (strpos($menus[$i]['url'], '//') == true ? base_url() . 'frame?link=' . $c->encrypt_decrypt('encrypt', $menus[$i]['url']) : base_url() . 'index.php/' . $menus[$i]['url']) . '"><i class="fa fa-home"></i><span>' . $menus[$i]['titulo'] . '</span></a>';
            for ($a = 0; $a < count($menus); $a++) {
                if ($a == 0) {
                    echo '<ul>';
                }
                if ($menus[$a]['id_pai'] == $menus[$i]['id']) {
                    if ($menus[$a]['titulo'] == 'Simuladores de Cálculos') {
                        echo '<li><a target="_blank" href="' . $menus[$a]['url'] . '">' . $menus[$a]['titulo'] . '</a>';
                    } else {
                        echo '<li><a href="' . (strpos($menus[$a]['url'], '//') == true ? base_url() . 'frame?link=' . $c->encrypt_decrypt('encrypt', $menus[$a]['url']) : base_url() . 'index.php/' . $menus[$a]['url']) . '">' . $menus[$a]['titulo'] . '</a>';
                    }
                    for ($b = 0; $b < count($menus); $b++) {
                        if ($b == 0) {
                            echo '<ul>';
                        }
                        if ($menus[$b]['id_pai'] == $menus[$a]['id']) {
                            echo '<li>' . $menus[$b]['titulo'] . '</li>';
                            echo '<li><a href="' . (strpos($menus[$b]['url'], '//') == true ? base_url() . 'frame?link=' . $c->encrypt_decrypt('encrypt', $menus[$b]['url']) : base_url() . 'index.php/' . $menus[$b]['url']) . 'index.php/">' . $menus[$b]['titulo'] . '</a></li>';
                        }
                        if ($b == count($menus) - 1) {
                            echo '</ul>';
                        }
                    }


                    echo '</li>';
                }
                if ($a == count($menus) - 1) {
                    echo '</ul>';
                }
            }

            echo '</li>';
        }
    }
}

Another place that happens the same error is this:

<?php
$redes = $this->session->userdata('redes');
if(count($redes) > 0){
    for($i=0; $i<count($redes);$i++){
        echo '<li class="skew-25"><a href="'.$redes[$i]['url'].'" target="_blank" data-title="'.$redes[$i]['titulo'].'" data-tooltip="true"><span class="fa '.$redes[$i]['class'].' skew25"></span></a></li>';
    }
} 
?>

I think he’s easier to read and if you solve one I think you can fix the other.

1 answer

1

To resolve the error could scan is countable using is_contable (or is_array, if not in PHP 7.3)( before using count(), then it would be something like:

if (is_countable($redes) && count($redes) > 0) {
}

That would remove the Warning, because I would only use the count() when the $redes were is_contable.

But, you’d have to see what’s being added to $redes which makes it not countable. For example, if the $this->session->userdata->redes for a string it will not be countable, but should investigate where is being added a string and not a array.

  • I tried here and did not give, it seems to be something related to Séssion in the codeigniter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.