mysql_connect("localhost", "47_rjwebdesign", "4XieHtft");
mysql_select_db("47_rjwebdesign");
$q = mysql_query("SELECT * FROM pages order by nazev_stranky");
$tree = array();
while ($r = mysql_fetch_object($q))
{
$tree['rodic'][$r->pageID] = $r->parrentID;
$tree['deti'][$r->parrentID][] = $r->pageID;
$tree['pages'][$r->pageID] = $r;
}
//print_r($tree);
$menu = new Tree($tree);
echo "
Drobeckova navigace
";
if (isset($_GET['p']))
{
$menu->crumbs($_GET['p']);
}
echo "Najednou (napr. pro SITEMAP)
";
foreach ($menu->buildCategory(0, 0) as $item)
{
echo $item[1] . "
";
}
echo "Postupne se rozbaluje
";
$menu->dump(0);
class tree
{
public $tree;
public $parents = array();
public $childs = array();
public function __construct($tree)
{
$this->tree = $tree;
$this->p = isset($_GET['p']) ? $_GET['p'] : -1;
if ($this->p > -1)
{
$this->parents = $this->getParent($this->p);
//print_r($this->parents);
}
}
public function getParent($id)
{
$tmp = array();
while($this->tree['rodic'][$id] > 0)
{
$id = $this->tree['rodic'][$id];
$tmp[] = $id;
}
return $tmp;
}
public function crumbs($id)
{
$crumbs = $this->getParent($id);
$out = array();
$row = $this->tree['pages'][$id];
$out[] = ''.$row->nazev_stranky.'';
foreach ($crumbs as $id)
{
$row = $this->tree['pages'][$id];
$out[] = ''.$row->nazev_stranky.'';
}
echo implode(' / ', array_reverse($out));
}
public function buildCategory($parent, $indent = 0)
{
foreach ($this->tree['deti'][$parent] as $item)
{
$pair[0] = $item;
$pair[1] = str_repeat(" ", $indent * 3) . ' + '. $this->tree['pages'][$item]->nazev_stranky;
$this->childs[] = $pair;
if (isset($this->tree['deti'][$item]))
{
$this->buildCategory($item, $indent + 1);
}
}
return $this->childs;
}
public function dump($parent)
{
if (isset($this->tree['deti'][$parent]))
{
echo "";
foreach ($this->tree['deti'][$parent] as $item)
{
if ($_GET['p'] == $item)
echo "- ";
else
echo "
- ";
echo "".$this->tree['pages'][$item]->nazev_stranky."";
if ( ($this->p == $item || in_array($item, $this->parents))
)
{
$this->dump($item);
}
echo "
";
}
echo "
";
}
}
}
/*
# --------------------------------------------------------
#
# Struktura tabulky `pages`
#
CREATE TABLE `pages` (
`pageID` tinyint(3) unsigned zerofill NOT NULL auto_increment,
`parrentID` tinyint(3) unsigned zerofill default '000',
`nazev_stranky` varchar(255) NOT NULL default '',
`nazev_odkazu` varchar(255) NOT NULL default '',
PRIMARY KEY (`pageID`,`pageID`),
KEY `pageID` (`pageID`)
) TYPE=MyISAM AUTO_INCREMENT=255 ;
#
# Vypisuji data pro tabulku `pages`
#
INSERT INTO `pages` VALUES (001, 100, 'prvni.php', 'prvni');
INSERT INTO `pages` VALUES (002, 100, 'druha.php', 'druhy');
INSERT INTO `pages` VALUES (003, 100, 'treti.php', 'treti');
INSERT INTO `pages` VALUES (004, 200, 'ctvrta.php', 'ctrta');
INSERT INTO `pages` VALUES (005, 200, 'pata.php', 'pata');
INSERT INTO `pages` VALUES (006, 255, 'sesta.php', 'sesta');
INSERT INTO `pages` VALUES (007, 255, 'sedma.php', 'sedma');
INSERT INTO `pages` VALUES (255, NULL, 'parrent3.php', 'parrent3');
INSERT INTO `pages` VALUES (200, NULL, 'parrent2.php', 'parrent2');
INSERT INTO `pages` VALUES (100, NULL, 'parrent1.php', 'parrent1');
*/
?>