Error no count ()

1

I need to put a website in the air (it was not me who made the site), and I need to adjust some things in it.

I'm facing a problem in a dynamic menu: when I'm on the home page it's ok, but if I browse the site the following error occurs:

  

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

Follow 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 where the same error happens 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 it's easier to read and if you solve one I think you can get the other one right.

    
asked by anonymous 18.09.2018 / 13:41

1 answer

1

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

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

This would remove the warning because it would only use count() when $redes was is_contable .

But, you'd have to see what's being added in $redes that does not count. For example, if $this->session->userdata->redes is a string it will not be countable, but it should investigate where a string is being added and not an array .

    
18.09.2018 / 14:36