Re: help: tree in child listbox
Posted by:
meisaputra (IP Logged)
Date: September 25, 2007 03:08PM
this is the function named dbtree_listbox (i use adodb to execute query)
//
// start function
//
function dbtree_listbox($table_name, $key, $value, $condition, $selected_key = 0) {
global $db;
$sql = "SELECT node.$key, CONCAT( REPEAT( ' ', (COUNT(parent.$value) - 1) ), node.$value) AS name ";
$sql .= "FROM $table_name AS node, ";
$sql .= "$table_name AS parent ";
$sql .= "WHERE 1 ";
$sql .= "AND node.lft BETWEEN parent.lft AND parent.rgt ";
if (strlen($condition) > 0) {
$sql .= "AND $condition ";
}
$sql .= "GROUP BY node.name ";
$sql .= "ORDER BY node.lft ";
print $sql;
if (!$r_sql = $db->Execute($sql)) {
print $db->ErrorNo() . $db->ErrorMsg();
exit;
}
if ($r_sql->RecordCount() > 0) {
$selected = "";
while (!$r_sql->EOF) {
$selected = ($r_sql->fields[$key] == $selected_key) ? " SELECTED" : "";
print "<option value=\"".$r_sql->fields[$key]."\"$selected>".$r_sql->fields[$value]."</option>\n";
$r_sql->MoveNext();
}
}
}
//
// end function
//
this is the table group strusture:
CREATE TABLE /*!32312 IF NOT EXISTS*/ `groups` (
`company_id` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
`lft` int(11) NOT NULL,
`rgt` int(11) NOT NULL,
`id` int(11) NOT NULL auto_increment,
PRIMARY KEY (`id`),
UNIQUE KEY `company` (`company_id`,`lft`,`rgt`)
) ENGINE=MyISAM AUTO_INCREMENT=22 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
and this is the samples content of the group table:
INSERT INTO `groups` (`company_id`, `name`, `lft`, `rgt`, `id`) VALUES
(10,'one',1,12,1),
(10,'one.one',2,7,2),
(10,'one.one.one',3,6,3),
(10,'one.one.one.one',4,5,4),
(10,'one.two',8,11,5),
(10,'one.two.one',9,10,6),
(2,'one',1,12,7),
(2,'one.one',2,3,8),
(2,'one.one',4,9,9),
(2,'three.one',10,11,10),
(2,'two.one.one',5,6,11),
(2,'two.two.one',7,8,12),
(1,'aaaa',1,18,13),
(1,'bbbbb',2,17,14),
(1,'cccc',3,8,15),
(1,'dddd',9,14,16),
(1,'eeee',15,16,17),
(1,'ffff',4,5,18),
(1,'gggg',6,7,19),
(1,'hhhh',10,11,20),
(1,'iiiii',12,13,21);
i have doing some review, maybe we need little modif in sc_select_ajax_handler.php but, i don't know where to put the parameter if we need plain/tree form in the list.
regard.
rms