feha
Posts: 712
|
Posted: 02/19/2006, 10:57 PM |
|
Did you ever wanted to have tree like structure in listbox ?
Here is the function:
function list_categories() {
$names=array();
$cats=array();
global $dyn_menu;
$dyn_menu = "";
$db = new clsDBcms();
$dyn_menu .="<select name=\"category\">";
// adjust the query below with the proper field names and table name
// DON'T change the 'id', 'name' and 'parent' aliases in the query!
$SQL = "SELECT menu_id AS id, menu_name AS name, menu_id_parent AS parent FROM cms_menu";
$result = $db->query($SQL);
//while ($row = mysql_fetch_assoc($result))
while ($db->next_record())
{
//$names[$row['id']] = $row['name'];
//$cats[$row['id']] = $row['parent'];
$names[$db->f("id")] = $db->f("name");
$cats[$db->f("id")] = $db->f("parent");
}
display_options(hierarchize($cats, 0), $names);
$db->close();
$dyn_menu .="</select>";
return $dyn_menu;
}
function hierarchize(&$cats, $parent) {
$subs = array_keys($cats, $parent);
$tree = array();
foreach ($subs as $sub) {
$tree[$sub] = hierarchize($cats, $sub);
}
return count($tree) ? $tree : $parent;
}
function display_options(&$tree, &$names, $nest = 0) {
global $dyn_menu;
foreach ($tree as $key => $branch) {
$indent = $nest ? str_repeat('--', $nest) . '| ' : '';
$dyn_menu .="<option value=\"$key\">$indent{$names[$key]}</option>\n";
if (is_array($branch)) {
display_options($branch, $names, $nest + 1);
}
}
}
this will produce:
<select name="category"><option value="1">Products</option>
<option value="32">--| Fisnik Hasani</option>
<option value="3">Support</option>
<option value="4">Purchase</option>
<option value="2">--| Downloads</option>
<option value="26">----| Downloads 2</option>
<option value="27">------| downloads3</option>
<option value="5">Company</option>
<option value="6">All</option>
<option value="7">CodeCharge</option>
<option value="8">CodeCharge Studio</option>
<option value="28">--| fisnik</option>
<option value="29">----| fisniks programms</option>
<option value="30">----| fisnik icons</option>
<option value="31">------| blue color</option>
<option value="9">DemoCharge Studio4</option>
<option value="10">Comparison</option>
<option value="11">CodeCharge Studio</option>
<option value="12">DemoCharge Studio2</option>
<option value="13">CodeCharge</option>
<option value="14">CodeCharge Studio</option>
<option value="15">DemoCharge Studio5</option>
<option value="16">Support</option>
<option value="17">Forums</option>
<option value="18">KB</option>
<option value="19">Store</option>
<option value="20">Resellers</option>
<option value="21">Affiliate</option>
<option value="22">About Us</option>
<option value="23">Contact Us</option>
<option value="24">Press Releases</option>
<option value="25">TEST</option>
</select>
Coming more in Part II
_________________
Regards
feha
www.vision.to
feedpixel.com |
|
|
feha
Posts: 712
|
Posted: 02/21/2006, 5:20 AM |
|
The SQL table used is:
DROP TABLE IF EXISTS `cms_menu`;
CREATE TABLE `cms_menu` (
`menu_id` int(11) NOT NULL auto_increment,
`menu_id_parent` int(11) default '0',
`section_id` int(11) default '0',
`menu_name` varchar(50) default NULL,
`id_name` varchar(100) default NULL,
`class_names` varchar(100) default NULL,
`icon` varchar(250) default NULL,
`menu_link` varchar(50) default NULL,
`target` varchar(10) default '_self',
`file_name` varchar(250) default NULL,
`tooltip` blob,
`description` blob,
`lang_id` char(2) default 'en',
`active` tinyint(3) default '1',
`group_id` tinyint(3) default '0',
`display_order` int(11) default NULL,
PRIMARY KEY (`menu_id`)
) TYPE=MyISAM;
To put any menu item on sub-sub- ... etc ...
it needs to have on menu_id_parent the menu_id of desired node ...
the main menu is allways with menu_id_parent == 0 .
_________________
Regards
feha
www.vision.to
feedpixel.com |
|
|
feha
Posts: 712
|
Posted: 02/21/2006, 5:30 AM |
|
To call-draw this menu you will need to create a label my_tree_menu (HTML -property)
and beforeshow event of label:
$your_form_component->my_tree_menu ->SetValue(list_categories() );
To process submited value use:
CCGetParam("category","");
So you are able with this to update some hidden field in your record form:
On Update or On Insert Event ...
if(CCGetParam("category",""))
{
$your_form_component->my_hidden_field->SetValue(CCGetParam("category",""));
}
Some more features can be added as to show current selected value etc ...
But this requires some more coding.
Hope you like this feature
_________________
Regards
feha
www.vision.to
feedpixel.com |
|
|
|