Eric Kincl wrote:
Hey,
First, I dont know how you structuring SID, but if you just have it setup so
you can only have 1 sub-menu, then according to normal forms, then that
d-base design is wrong. I would retitle "sid" to "parent", which refers to
the "id" of the parent menu. ("0" would be the main parent of course)
As far as displaying them, unless you want just 1 parent and its immediate
underlings, then you are going to want to retrive the entire table and sort
it in PHP, so the SQL you would use is "SELECT * FROM menu_table;" Like I
said, you would have to use PHP to sort everything and format it.
This is probably a good solution. Getting to the "use PHP to sort
everything and format it" issue, I recommend a recursive function.
hmmm.. I seem to have written the whole dang thang :/
well, that was fun :)
It's an XML implementation, If you want me to show you how to make it
output HTML, just ask.
<?php
class MenuMaker {
var $objDom;
var $arrResult;
function MenuMaker($arrResult) {
$this->arrResult = $arrResult;
$this->objDom = domxml_new_doc("1.0");
$ndRoot =
$this->objDom->append_child($this->objDom->create_element("menu"));
foreach($this->arrResult AS $arrTopRow) {
if($arrTopRow["parent_id"] == 0) {
$this->handleMenuItem($arrTopRow,$ndRoot);
}
}
}
function handleMenuItem($arrRow,$nd) {
// display the thing...
$ndItem = $this->printMenuItem($arrRow,$nd);
//{{{ create a temporrary stack of immediate children
$arrTmp = array();
foreach($this->arrResult AS $arrSubRow) {
if($arrRow["id"] == $arrSubRow["parent_id"]) {
$arrTmp[] = $arrSubRow;
}
}
//}}}
// execute the immdiate children in a row (ordered)
foreach($arrTmp AS $arrSubRow){
$this->handleMenuItem($arrSubRow,$ndItem);
}
}
function printMenuItem($arrRow,$nd) {
$ndItem = $nd->append_child($this->objDom->create_element("item"));
foreach($arrRow AS $key=>$val) {
$ndTitle =
$ndItem->append_child($this->objDom->create_element($key));
$ndTitle->set_content($val);
}
return $ndItem;
}
function getXml() {
return $this->objDom->dump_mem(true);
}
}
$arrFakeResult = array();
$arrFakeResult[] =
array("id"=>1,"parent_id"=>0,"title"=>"project","h ref"=>"project/");
$arrFakeResult[] =
array("id"=>2,"parent_id"=>0,"title"=>"faq","href" =>"faq/");
$arrFakeResult[] =
array("id"=>3,"parent_id"=>0,"title"=>"contact","h ref"=>"contact.php");
$arrFakeResult[] =
array("id"=>4,"parent_id"=>1,"title"=>"aims","href "=>"project/aims/");
$arrFakeResult[] =
array("id"=>5,"parent_id"=>1,"title"=>"objectives" ,"href"=>"project/obj/");
$arrFakeResult[] =
array("id"=>6,"parent_id"=>4,"title"=>"easy","href "=>"project/aims/easy.php");
$arrFakeResult[] =
array("id"=>7,"parent_id"=>4,"title"=>"scalable"," href"=>"project/aims/sclable.php");
$arrFakeResult[] =
array("id"=>8,"parent_id"=>4,"title"=>"robust","hr ef"=>"project/aims/robust.php");
$arrFakeResult[] = array("id"=>9,"parent_id"=>5,"title"=>"object
oriented","href"=>"project/obj/oo.php");
$arrFakeResult[] =
array("id"=>10,"parent_id"=>5,"title"=>"xml","href "=>"project/obj/xml.php");
$objMM = new MenuMaker($arrFakeResult);
header("Content-Type: text/xml");
echo $objMM->getXml();
?>