Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/04/2017, 21:40
stevens82
 
Fecha de Ingreso: octubre-2011
Mensajes: 132
Antigüedad: 12 años, 6 meses
Puntos: 4
Menú dinamico en mvc

Hola compañeros aca con una duda, estoy desarrollando un menú que traigo de la BD, por ahora lo tengo en un solo archivo y lo necesito separar en modelo-vista-controlador, el gran problema con el que me encontre es que tiene una funcion recursiva y por ahi no se como llamarlo en la vista aca les dejo lo que tengo por si alguien me de una idea.

esta es la BD:
Código SQL:
Ver original
  1. CREATE TABLE IF NOT EXISTS `menu` (
  2.   `id` INT(11) NOT NULL AUTO_INCREMENT,
  3.   `id_parent` INT(11) NOT NULL DEFAULT '0',
  4.   `link` VARCHAR(255) NOT NULL,
  5.   `order` INT(11) NOT NULL DEFAULT '0',
  6.   `title` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL DEFAULT '',
  7.   `level` tinyint(4) NOT NULL DEFAULT '0',
  8.   PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;
  10.  
  11.  
  12.  
  13. INSERT INTO `menu` (`id`, `id_parent`, `link`, `order`, `title`, `level`) VALUES
  14. (12, 0, '', 0, 'Office Furniture', 0),
  15. (13, 12, '', 0, 'Chairs', 0),
  16. (14, 12, '', 0, 'Work Tables', 0),
  17. (15, 12, '', 0, 'Workstations', 0),
  18. (16, 12, '', 0, 'Storage', 0),
  19. (17, 12, '', 0, 'Conference Tables', 0),
  20. (18, 13, '', 0, 'with spagh', 0),
  21. (19, 13, '', 0, 'without spagh', 0),
  22. (20, 18, '#', 0, 'Triangle', 0),
  23. (21, 18, '#', 0, 'Sqare', 0),
  24. (22, 19, '#', 0, 'Simple', 0),
  25. (23, 19, '#', 0, 'Complex', 0);

Y este el script php:
Código PHP:

$db 
= new PDO('mysql:dbname=db_menu''root''123456',
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

function 
createsubmenu($item)
{
    global 
$db;
    
$subsel $db->prepare('SELECT id, link, title, level  FROM menu WHERE id_parent = :parent ORDER BY `order`');

    
$subsel->execute(array('parent' => $item));
    
$text '';

        foreach (
$subsel as $i) {

        
$text .= '<li class="menucolor' . ($i['level'] - 1) . '">'
            
.'<a href="' htmlspecialchars($i['link']) . '">' htmlspecialchars($i['title']) . '</a>'
            
createsubmenu($i['id']) . '</li>';
        }

    if (
$text == '') {
        return 
'';
    }

    return 
'<ul id="red" class="treeview-red">' $text '</ul>';
}

echo 
createsubmenu(0); 
Gracias de antemano por su tiempo y ayuda.