0

I currently have some code which connects to a database which will grab the information to allow me to make a dynamic menu. But for some reason I constantly get an error.

The code is:

$config['database']['db_host'] = "localhost";
$config['database']['db_port'] = "db";
$config['database']['db_user'] = "db";
$config['database']['db_pass'] = "db";
$config['database']['db_name'] = "db";

function connect_db(){
    global $config;
    $db = mysql_connect($config['database']['db_host'].":".$config['database']['db_port'],$config['database']['db_user'],$config['database']['db_pass']) or die("Unable to connect mysql server!");
    $conn = mysql_select_db($config['database']['db_name']) or die("Database not found in mysql server!");
    return $conn;
}

function menu_from_query($query){
    global $index;
    connect_db();
    $result = mysql_query($query);

    $menu = array(
        'items' => array(),
        'parents' => array()
    );

    while ($items = mysql_fetch_assoc($result)){
        $menu['items'][$items['id']] = $items;
        $menu['parents'][$items['parent']][] = $items['id'];
        $parent = $items['parent'];
        while($parent != 0){
            if($menu['items'][$parent]['parent'] == 0)
                break;
            $parent = $menu['items'][$parent]['parent'];
        }
        $menu['items'][$items['id']]['top_parent'] = $parent;
        if($index == $items['link'] && $parent != 0)
            $menu['items'][$parent]['class'] = "open";
    }
    return menu_builder($menu);
}

function menu_builder($menu, $parent = 0){
    global $index;
    $output = "";
    if (isset($menu['parents'][$parent])){
        $output .= "";
            foreach ($menu['parents'][$parent] as $itemId){
                $active = ($index.".html" == $menu['items'][$itemId]['link'])?" class='active'":null;
                if(!isset($menu['parents'][$itemId])){
                    $output .= "".(!empty($menu['items'][$itemId]['icon'])?"":null)."".$menu['items'][$itemId]['title']."";
                }
                if(isset($menu['parents'][$itemId])){
                    $dropdown = ' ';
                    $output .= "".(!empty($menu['items'][$itemId]['icon'])?"":null)."".$menu['items'][$itemId]['title']."$dropdown";
                    $output .= menu_builder($menu, $itemId);
                    $output .= "";
                }
            }
            $output .= "";
    }

    return $output;
}

When I call it in my page using:

$menu = menu_from_query("SELECT id, title, link, icon, parent FROM menu ORDER BY parent, sort, title");
menu_builder($menu, 0);

I get the error:

Warning: Illegal string offset 'parents' in /var/www/html/assets/php/functions/menu.functions.php on line 47

Warning: Invalid argument supplied for foreach() in /var/www/html/assets/php/functions/menu.functions.php on line 47

What is causing the error and how can I fix it?

Tim Vos
  • 1
  • 2
  • foreach ($menu['parents'][$parent] as $itemId){ – Tim Vos Jul 11 '16 at 16:56
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 11 '16 at 17:41

0 Answers0