<email ⁄>
<windows live messenger ⁄>
<myCurriculum type="pdf" ⁄>
*{padding: 0; margin: 0;} body{ font-family: Tahoma, Verdana, Arial, sans-serif;font-size: 62.5%; color: #000;background: #fff;text-align: justify;margin-top: .5em; } div#container{ margin: 0 auto; width: 30em; padding: .8em; border: solid 0.6em #3F4C6B; text-align: center; } label, strong{font-size: 1.2em;color: #D01F3C;} strong{color: #356AA0 !important; font-weight: 900;} select{border: solid .3em #F3AE48;font-size: 1.3em} select option.node{color: #B71313; font-weight:800;background-color: #EFEBCE;}
-- Table "lista_produtos" DDL CREATE TABLE `lista_produtos` ( `id` tinyint(4) NOT NULL AUTO_INCREMENT, `pai` tinyint(4) NOT NULL DEFAULT '0', `nome` varchar(50) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- Inserir valores INSERT INTO lista_produtos (id, pai, nome) VALUES (1, 0, "Opção 1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (2, 0, "Opção 2"); INSERT INTO lista_produtos (id, pai, nome) VALUES (3, 0, "Opção 3"); INSERT INTO lista_produtos (id, pai, nome) VALUES (4, 0, "Opção 4"); INSERT INTO lista_produtos (id, pai, nome) VALUES (5, 1, "Opção 1.1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (6, 2, "Opção 2.1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (7, 2, "Opção 2.3"); INSERT INTO lista_produtos (id, pai, nome) VALUES (8, 2, "Opção 2.4"); INSERT INTO lista_produtos (id, pai, nome) VALUES (10, 0, "Opção 5"); INSERT INTO lista_produtos (id, pai, nome) VALUES (9, 10, "Opção 5.1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (11, 5, "Opção 1.1.1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (12, 3, "Opção 3.1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (13, 4, "Opção 4.1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (14, 8, "Opção 2.4.1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (15, 0, "Opção 6"); INSERT INTO lista_produtos (id, pai, nome) VALUES (16, 14, "Opção 2.4.1.1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (17, 14, "Opção 2.4.1.2"); INSERT INTO lista_produtos (id, pai, nome) VALUES (18, 8, "Opção 2.4.2"); INSERT INTO lista_produtos (id, pai, nome) VALUES (19, 18, "Opção 2.4.2.1"); INSERT INTO lista_produtos (id, pai, nome) VALUES (20, 18, "Opção 2.4.2.2"); INSERT INTO lista_produtos (id, pai, nome) VALUES (21, 7, "Opção 2.3.1");
<?php /** * Construir Multiple-Level Optgroup * * @param String ID * @return String */ function BuildDDList($id="ddListOptions"){ $ddListItems=_BuildDDListItems("lista_produtos"); return " <label for='$id'><strong>Opção</strong></label> <select name='$id' id='$id'>$ddListItems</select> "; } /** * Construir item da lista * * @param Int Pertence a * @param Int Nivel de Profundidade * @param DBConnection Ligação à BD * @return String */ function _BuildDDListItems($table, $parent=0, $level=0, $db_cnn=null){ $indent = ""; if(!$db_cnn){$db_cnn=_connectDB();} for ($i=0; $i<$level; $i++){ $indent.= _DummySpaces(4); //fazer indentação com 4 espaços } $iSQL=" Select `lp`.`id`,`lp`.`pai`,`lp`.`nome`, ( Select Count(*) From `$table` As `clp` Where `clp`.`pai`=`lp`.`id` ) As `num_sub_nodes` From `$table` As `lp` Where `lp`.`pai` = '$parent' "; $sql = mysql_query($iSQL,$db_cnn) or die (mysql_error()); while ($myrow = mysql_fetch_array($sql)){ //se fôr um nó, serão efectuadas algumas modificações if (_IsNode($myrow["num_sub_nodes"],$myrow["pai"])){ $prefix=""; $class='class="node"'; } else{ $prefix="-"._DummySpaces(1); } //construir item $items.="<option value='$myrow[id]' $class>$indent$prefix$myrow[nome]</option>"; //chamar recursivamente a função $items.=_BuildDDListItems($table, $myrow["id"], $level+1,$db_cnn); } return $items; } /** * Verificar se o registo é 1 nó * * @param Int * @param Int * @return Boolean */ function _IsNode($num_nodes,$parent){ return ($num_nodes>0 || $parent==0);} /** * Construir espaços ( ) * * @param Int Número * @return String */ function _DummySpaces($num){ for ($i=0;$i<$num;$i++) $dummy.=" "; return $dummy; } /** * Efectuar ligação à BD * * @return DBConnection */ function _connectDB(){ $db = mysql_connect("localhost", "root", ""); mysql_select_db("tech",$db) or die("Impossivel aceder à base de dados"); return $db; }
<?php include_once("functions.php");?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>A Multiple-Level Optgroup</title> <link rel="Stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="container"> <?php echo BuildDDList(); ?> </div> </body> </html>