Witam.
Mam skrypt do wyświetlania drzewa kategorii ale mam z nim problem ponieważ chcę wyświetlić kategorie w kolumnach. Ale jak jakaś kategoria ma podkategorię to nie tworzy się nowa kolumna tylko dopisuje do tej samej. Prosiłbym o pomoc.
Oto kod:
<?php
include "lib/functions.php";
"0" => array ( "id" => "259", "name" => "Sklep | Promocje", "parent_id" => "0", "ip" => "259" ), "1" => array ( "id" => "238", "name" => "SprzÄt nowy", "parent_id" => "0", "ip" => "238" ), "2" => array ( "id" => "185", "name" => "SprzÄt uĹźywany", "parent_id" => "0", "ip" => "185" ), "3" => array ( "id" => "184", "name" => "WypoĹźyczalnia", "parent_id" => "0", "ip" => "184" ), "4" => array ( "id" => "200", "name" => "Agregaty prÄ
dotwĂłrcze", "parent_id" => "184", "ip" => "184.200" ), "5" => array ( "id" => "201", "name" => "Betoniarki", "parent_id" => "184", "ip" => "184.201" ), "6" => array ( "id" => "202", "name" => "Drabiny", "parent_id" => "184", "ip" => "184.202" ), "7" => array ( "id" => "258", "name" => "Gilotyna do bruku", "parent_id" => "184", "ip" => "184.258" ), "8" => array ( "id" => "204", "name" => "Koparki", "parent_id" => "184", "ip" => "184.204" ), "9" => array ( "id" => "203", "name" => "Listwy wibracyjne", "parent_id" => "184", "ip" => "184.203" ), "10" => array ( "id" => "205", "name" => "MĹotowiertarki", "parent_id" => "184", "ip" => "184.205" ), "11" => array ( "id" => "206", "name" => "MĹoty", "parent_id" => "184", "ip" => "184.206" ), "12" => array ( "id" => "207", "name" => "Myjki ciĹnieniowe", "parent_id" => "184", "ip" => "184.207" ), (...)
"71" => array ( "id" => "273", "name" => "siala", "parent_id" => "270", "ip" => "265.267.270.273" ) ); echo "<ul class=\"cat\">"; $tree = getTree($arr2, 184);
$j = 0;
$iter = 0;
$x = 0;
$col = null;
foreach ($tree as $sub) {
// Jeżeli jest podkategoria to dodaje <ul>, jeżeli nie, zamyka </li>
if(isset($tree[$j+1
]) && $tree[$j+1]["level"]>$sub["level"]) { $close = "<ul class=\"none\">";
$hascat = "class=\"hascat\"";
}
else {
$close = "</li>";
$hascat = "";
if($x%10 == 0) $col = "</ul><ul class=\"cat\">";
else $col = null;
}
echo "<li><a href=\"#\" ".$hascat.">".$sub["name"]." </a>"." <small>".$sub["ip"]."; ".$x."</small>"; }
//Zamyka ul i li tyle razy ile trzeba
if(isset($tree[$j+1
]) && $tree[$j+1]["level"]<$sub["level"]) { for($i1=$tree[$j+1]["level"]; $i1<$sub["level"]; $i1++) {
}
}
//Zamyka ul, li na końcu
if(!isset($tree[$j+1
])) { for($i2=0; $i2<$sub["level"]; $i2++) {
}
}
$j++;
}
?>
Kod funkcji getTree()
<?php
function getTree($cat = null, $parentId = 0, $level = 0, $result = null){
if ($result == null)
if ($cat == null) {
}
foreach ($cat as $kat) {
if ($kat["parent_id"] == $parentId) {
$kat["level"] = $level;
$result[] = $kat;
$result = getTree($cat, $kat["id"], ($level+1), $result);
}
}
return $result;
}
?>