I have the router that defines my menu.
$router->define(
[
'inicio' => 'controllers/inicio.php',
'sobre-nosotros' => 'controllers/sobre-nosotros.php',
'blog' => 'controllers/blog.php',
'contactos' => 'controllers/contactos.php',
]
);
I want to get router data from the database instead of having to write it down, but I can't get the associative array into my router. I have tried a foreach
:
$menu = $pdo->query("
SELECT title FROM menu
ORDER BY priority DESC
;")->fetchAll(PDO::FETCH_OBJ);
$router->define(
[
foreach ($menu as $item) {
$item->title => 'Controllers/'.$item->title.'.php',
}
]
);
It throws me the following error:
unexpected 'foreach' (T_FOREACH), expecting ']'
I can't test it because I don't have more code but try this:
The objective is to define the array before the define and then pass it as a parameter, I hope it works for you, if you get another error, post it and see if I can adjust it.
PS: the error you got was "Hey, you can't put a forearch inside the definition of an array!"
You can't define an array like that, putting a foreach in the declaration, you have to build the array outside, you can try like this: