Notice: Trying to get Property 'Descricao' of non-object in C: xampp htdocs testemenu menu.php on line 83

Asked

Viewed 694 times

0

I need to set up a dynamic menu, follow code:

require_once('testemenu.php');

$sql = "SELECT * FROM tbmenu";
$query = $pdo->query($sql);
$menu = $query->fetchAll(PDO::FETCH_ASSOC);

function montaMenuFilho($menu) {
    echo '<ul class="dropdown-menu pull-left">';
    foreach ($menu as $item) {
        if(is_null($item)) {
            echo '<li class="dropdown-submenu">
                        <a href="#">
                        <i class="icon-puzzle"></i>'.$item->Descricao.'</a>';
            echo montaMenuFilho($item);
            echo '</li>';
        } else {
            if ($item->IdMenu == '27') {

                echo '<li class=""><a href="'.$item->Url.'" title="Necessário logar na ferramenta Power BI.">'.$item->Descricao.'</a></li>';

            } elseif ($item->IdMenu == '47') {

                echo '<li class=""><a href="'.$item->Url.'" title="Apenas para acompanhamento da entrada de recursos.">'.$item->Descricao.'</a></li>';

            } elseif ($item->IdMenu == '45') {

                echo '<li class=""><a href="'.$item->Url.'" title="Painel em validação">'.$item->Descricao.'</a></li>';

            } elseif ($item->IdMenu == '11' || $item->IdMenu == '23' || $item->IdMenu == '16' || $item->IdMenu == '17' || $item->IdMenu == '19' || $item->IdMenu == '12' || $item->IdMenu == '26') {

                echo '<li class=""><a target="_blank" href="'.$item->Url.'">'.$item->Descricao.'</a></li>';

            } else {

                echo '<li class=""><a href="'.$item->Url.'">'.$item->Descricao.'</a></li>';

            }
        }        
    }
    echo '</ul>';
}
?>
<ul class="nav navbar-nav">
    <li class="">
        <a href="http://10.31.40.74/novahome/">Home</a>
    </li>                 
    <?php                        
        foreach ($menu as $item) {
            if(!empty($item)) {                                
                echo '<li class="menu-dropdown classic-menu-dropdown">
                            <a data-hover="megamenu-dropdown" data-toggle="dropdown">
                                '.$item->Descricao.'<i class="fa fa-angle-down"></i>
                            </a>';
                echo montaMenuFilho($item);
                echo '</li>';
            } else {
                echo '<li class=""><a href="'.$item->Url.'">'.$item->Descricao.'</a></li>';
            }
        }  
    ?>             
</ul>

Also follows the select database:

  IdMenu | Descricao                   | Url                                                                  | IdMenuPai | Ordem | Status | DataCriacao |
+--------+-----------------------------+----------------------------------------------------------------------+-----------+-------+--------+-------------+
|      1 | Painéis                     | NULL                                                                 |      NULL |     1 |      1 | NULL        |
|      5 | Soluções                    | NULL                                                                 |      NULL |     3 |      1 | NULL        |
|      7 | SCC                         | NULL                                                                 |         5 |     3 |      1 | NULL        |
|      8 | Consulta de Clientes        | http://google.com                                                 |         7 |     1 |      1 | NULL        |
|      9 | Utilização do SCC           | http://google.com                                                |         7 |     2 |      1 | NULL        |
|     14 | Canais Eletrônicos          |  http://google.com                                                 |         1 |    13 |      0 | NULL        |
|     15 | Calculadora de Parcelamento | http://google.com                                                 |         5 |     1 |      1 | NULL        |
|     20 | Fluxos do GPS               |  http://google.com                                                 |        66 |     2 |      1 | NULL        |
|     21 | Contestação                 | NULL                                                                 |         1 |     7 |      1 | NULL        |
|     22 | Contestação Móvel           |  http://google.com                                                 |        21 |     1 |      1 | NULL        |
|     35 | Blended                     |  http://google.com                                                 |        43 |     2 |      1 | NULL

1 answer

0


Mate, to fix this, you can exchange your FETCH response method to:

$menu = $query->fetchAll(PDO::FETCH_OBJ);

But if you want to keep as FECTH_ASSOC, the form of field recovery becomes:

$item["Descricao"]

and so on for all fields accessed...

  • Colleague, thank you, you helped me a lot

  • now gave this error... Warning: Invalid argument supplied for foreach() in C: xampp htdocs testemenu menu.php on line 43

  • Did you remember to change the way to get the data? If it worked, you could mark as a response?

  • Yes, I did everything as instructed, he returned the menu, but came without hierarchy :S

Browser other questions tagged

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