Ich möchte aus einem Array eine Navigation (Liste) erzeugen.
Die Navigation aktivePfade, Startseiten etc. anbieten - funzt auch soweit.
Was mir jetzt noch fehlt. Ich möchte jeder Ebene (Layer) eine ID zuweisen können.
Ich habe es an dieser Stelle im Code versucht, erhalte dann aber immer die Selbe ID.
[b]Eine Id Könne so aussehen[b]
<ul id="3">...</ul>
Hat jmd Ideen?
aufruf:
Die Navigation aktivePfade, Startseiten etc. anbieten - funzt auch soweit.
Was mir jetzt noch fehlt. Ich möchte jeder Ebene (Layer) eine ID zuweisen können.
PHP-Code:
$tmp = str_replace('{$layer}', $layer, $this->navTpl['layer'] );
[b]Eine Id Könne so aussehen[b]
<ul id="3">...</ul>
Hat jmd Ideen?
PHP-Code:
<?php
class Navigation {
var $_url;
var $_urlArray;
var $_page;
var $_pathArray;
var $_invPathArray;
var $_depth;
var $showEmptyFolder;
var $maxDepth;
var $activePath;
var $showDepth;
var $showStartPage;
// nav Tpl
var $navTpl;
// nav/ folder Array
var $navArray;
function Navigation($navArray, $navTpl)
{
$this->navTpl = $navTpl;
$this->navArray = $navArray;
$this->_urlArray = array();
$this->_pathArray = array();
}
function _getStartPageUrl($folder) {
foreach($folder as $key => $subfolder) {
if($subfolder['start'] == 1) {
return $subfolder['link'];
}
}
}
function _showFolder($folder) {
$tmp = "";
if(($this->_depth < $this->maxDepth) or ($this->maxDepth == "0")) {
// new layer?
if (is_array($folder)) {
// loop array
$layer = "";
foreach($folder as $key => $subfolder) {
// folder, evtl kann irgendwie auf den type verzichtet werden?
if (is_array($subfolder[0]) || $subfolder['type'] == "folder" ) {
// increase depth
$this->_depth ++;
// Unterordner Rekursiv anzeigen
if(in_array($subfolder['folderLabel'], $this->_invPathArray) && $this->activePath ) {
$subsub = "\n".$this->_showFolder($subfolder);
} elseif($this->_depth < $this->showDepth) {
$subsub = "\n".$this->_showFolder($subfolder);
} else {
$subsub = "";
}
// active folder?
if(in_array($subfolder['folderLabel'], $this->_invPathArray) && $this->activePath) {
$tmp_folder = str_replace('{$folder}', $subsub, str_replace('{$label}', $subfolder['label'], $this->navTpl['folderactive']));
} else {
$tmp_folder = str_replace('{$folder}', $subsub, str_replace('{$label}', $subfolder['label'], $this->navTpl['folder']));
}
// link der startseite
$tmp_folder = str_replace('{$link}', $this->_getStartPageUrl($subfolder), $tmp_folder);
$layer .= $tmp_folder;
// _page?
} else {
// dont show the folder data
if (is_array($subfolder)) {
//startseite anzeigen?
if($subfolder['start'] && $this->showStartPage){
// active _page
if($subfolder['link'] == $this->_url) {
$tmp_page = str_replace('{$label}', $subfolder['label'], $this->navTpl['pageactive']);
} else {
$tmp_page = str_replace('{$label}', $subfolder['label'], $this->navTpl['page']);
}
$tmp_page = str_replace('{$link}', $subfolder['link'], $tmp_page);
$layer .= $tmp_page;
} elseif ( !$subfolder['start'] ) {
// active _page ! ACHTUNG wiederholung von oben
if($subfolder['link'] == $this->_url) {
$tmp_page = str_replace('{$label}', $subfolder['label'], $this->navTpl['pageactive']);
} else {
$tmp_page = str_replace('{$label}', $subfolder['label'], $this->navTpl['page']);
}
$tmp_page = str_replace('{$link}', $subfolder['link'], $tmp_page);
$layer .= $tmp_page;
}
}
}
} // end foreach
// new layer
$tmp = str_replace('{$layer}', $layer, $this->navTpl['layer'] );
} else {
vd("Fehler: Kein Array übergeben");
}
return $tmp;
}
}
function _doNav()
{
$_activeDepth = 0;
$nav ="";
$nav .= $this->navTpl['start'];
$nav .= $this->_showFolder($this->navArray);
$nav .= $this->navTpl['end'];
return $nav;
}
function display()
{
echo $this->_doNav();
}
function getNavSource()
{
return $this->_doNav();
}
function setActive($url)
{
$this->_url = $url;
$this->_urlArray = split("/", $url);
$this->_page = array_pop($this->_urlArray);
$this->_pathArray = $this->_urlArray;
$this->_invPathArray = array_reverse($this->_urlArray);;
}
}
?>
PHP-Code:
// tempalte 1
$navArray = array();
$navArray[] = array( "label" => "Produkte",
"type" => "folder");
$navArray[0][1] = array ( "start" => true,
"type"=> "folder",
"label" => "Silos",
"folderLabel" => "Silos"
);
$navArray[0][1][0] = array ( "start" => true,
"type"=> "page",
"label" => "Aluminium-Silos",
"link" =>
"www/Produkte/Silos/Aluminium-Silos.html"
);
$navArray[0][1][1] = array ( "start" => false,
"type"=> "page",
"label" => "Edelstahl-Silos",
"link" => "www/Produkte/Silos/Edelstahl-Silos.html"
);
$navArray[0][2] = array ( "start" => true,
"type"=> "folder",
"label" => "Schüttgutbehalter",
"folderLabel" => "Schuttgutbehalter"
);
$navArray[0][2][0] = array ( "start" => true,
"type"=> "page",
"label" => "Tagesbehälter",
"link" =>
"www/Produkte/Schuttgutbehalter/Tagesbehalter.html"
);
$navArray[0][2][1] = array ( "start" => false,
"type"=> "page",
"label" => "Schüttgut-Container",
"link" =>
"www/Produkte/Schuttgutbehalter/Schuttgut-Container.html"
);
$navTpl = array();
$navTpl1['start'] = '<div id="ULnav1">';
$navTpl1['end']= '</div> ';
$navTpl1['layer']= '<ul>{$layer}</ul> ';
$navTpl1['page']= '<li><a href="{$link}">{$label}</a></li>';
$navTpl1['pageactive']= '<li><strong><a href="{$link}" class="active">{$label}</a></strong></li>';
$navTpl1['folder']= '<li><a href="{$link}" >{$label}</a>{$folder}</li>';
$navTpl1['folderactive']= '<li><strong><a href="{$link}" class="active">{$label}</a></strong>{$folder}</li>';
$Navigation1 = new Navigation($navArray, $navTpl1);
$Navigation1->showEmptyFolder = true;
$Navigation1->maxDepth = 0;
$Navigation1->showDepth = 2;
$Navigation1->activePath = true;
$Navigation1->showStartPage = false;
$Navigation1->setActive($url);
$navSource1 = $Navigation1->getNavSource();
Kommentar