array('parent' => 0, 'name' => 'Televize', 'url' => 'televize'), 2 => array('parent' => 1, 'name' => 'LED', 'url' => 'led'), 3 => array('parent' => 2, 'name' => '101 palcu', 'url' => '101-palcu'), 4 => array('parent' => 3, 'name' => 'Samsung', 'url' => 'samsung'), 5 => array('parent' => 0, 'name' => 'Videa', 'url' => 'videa'), 6 => array('parent' => 5, 'name' => 'Samsung', 'url' => 'samsung'), 7 => array('parent' => 6, 'name' => 'DO 10 tisic', 'url' => 'do-10-tisic'), 8 => array('parent' => 1, 'name' => 'Televize - sub', 'url' => 'televize'), 9 => array('parent' => 8, 'name' => 'Vyrazene teleize', 'url' => 'vyrazene-televize'), ); public function __construct() { $this->url = explode("/", $_GET["url"]); $this->parent = array(); $this->urls = array(); $this->urlParent = array(); foreach ($this->products as $id => $row) { $this->parent[$row["parent"]][$id] = $row; $this->urls[$id] = $this->generateUrl($id); $this->urlParent[$row["parent"]][$row["url"]] = $id; } // 1 zpusob kompletni URL $this->urlsReverse = array_flip($this->urls); // 2 zpusob postupne overen if (isset($_GET["url"]) && count($this->url)) { $parent = 0; $foundedId = 0; foreach ($this->url as $url) { if (isset($this->urlParent[$parent][$url])) { $foundedId = $this->urlParent[$parent][$url]; $parent = $foundedId; } } } // if (isset($_GET["url"]) && count($this->url) && !array_key_exists($_GET["url"], $this->urlsReverse)) // { // echo "NEEXISTUJE VOLANA KATEGORIE"; // } } public function crumbs() { if (count($this->url)) { $id = $this->urlsReverse[ implode("/", $this->url) ]; echo "Nacházíte se na Katalog "; $data = $this->generateCrumbs($id); foreach ($data as $arr) { echo " / " . "".$arr["name"]." "; } } } public function generateCrumbs($id) { $data[] = array("name" => $this->products[$id]["name"], "url" => $this->urls[$id]); $parent = $this->products[$id]["parent"]; while ($parent > 0) { $data[] = array("name" => $this->products[$parent]["name"], "url" => $this->urls[$parent]); $parent = $this->products[$parent]["parent"]; } $data = array_reverse($data); return $data; } public function generateUrl($id) { $url = array(); $url[] = $this->products[$id]["url"]; $parent = $this->products[$id]["parent"]; while ($parent > 0) { $url[] = $this->products[$parent]["url"]; $parent = $this->products[$parent]["parent"]; } return implode("/", array_reverse($url)); } public function navigation($parent, $level = 0) { if (isset($this->parent[$parent])) { foreach ($this->parent[$parent] as $id => $row) { echo str_repeat("-", $level) . " ".$row['name']."
"; if ($this->url[$level] == $row["url"]) { $this->navigation($id, $level + 1); } } } } } header('content-type: text/html; charset=utf-8'); $shop = new shop; $shop->crumbs(); echo "
"; $shop->navigation(0, 0);