Here in indexAction I must call to subcat function to recursively get a list of categories and subcategories, otherwise it does not work.
Note subcat calls itself.
How can I write subcat function outside indexAction? and call it from inside IndexAction. Doing this, i can reuse subcat.
class ItemsController implements ControllerProviderInterface {
public function connect(Application $app) {
$controllers = $app['controllers_factory'];
$controllers
->get('/', array($this, 'indexAction'))
->bind('items_index');
return $controllers;
}
public function indexAction(Application $app) {
$categorias = array();
function subcat($idPadre, array & $categorias, $app) {
$sql = 'SELECT id, categoria FROM categorias' .
' WHERE id_padre=' . $idPadre .
' ORDER BY categoria';
$query = $app['db']->fetchAll($sql);
foreach ($query as $valor) {
$a = subcat($valor['id'], $categorias, $app);
$lista[$valor['id'].'-'.$valor['categoria']] = $a;
}
return $lista;
};
$estructura = subcat(0, $categorias, $app);
print_r($estructura);
return '';
}
}