Ver Mensaje Individual
  #7 (permalink)  
Antiguo 09/02/2016, 08:04
Skorge
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Hoja de Estilo con PHP

Cita:
<?php

// connect to mysql
$link = mysql_connect( 'localhost', 'root', '' );

// select the database
mysql_select_db('pagina', $link);


// create an array to hold the references
$refs = array();

// create and array to hold the list
$list = array();

// the query to fetch the menu data
$sql = "SELECT Codigo, Menu_padre, Descripcion FROM Menu ORDER BY Descripcion";

// get the results of the query
$result = mysql_query($sql);

// loop over the results
while($data = @mysql_fetch_assoc($result))
{
// Assign by reference
$thisref = &$refs[ $data['Codigo'] ];

// add the the menu parent
$thisref['Menu_padre'] = $data['Menu_padre'];
$thisref['Descripcion'] = $data['Descripcion'];

// if there is no parent Codigo
if ($data['Menu_padre'] == 0)
{
$list[ $data['Codigo'] ] = &$thisref;
}
else
{
$refs[ $data['Menu_padre'] ]['children'][ $data['Codigo'] ] = &$thisref;
}
}

/**
*
* Create a HTML list from an array
*
* @param array $arr
* @param string $list_type
* @return string
*
*/
function create_list( $arr )
{
$html = "\n<ul>\n";
foreach ($arr as $key=>$v)
{
$html .= '<li>'.$v['Descripcion']."</li>\n";
if (array_key_exists('children', $v))
{
$html .= "<li>";
$html .= create_list($v['children']);
$html .= "</li>\n";
}
else{}
}
$html .= "</ul>\n";
return $html;
}

echo create_list( $list );

?>