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:
  1. <?php
  2. include "lib/functions.php";
  3.  
  4. $arr2 = array();
  5.  
  6. $arr2 = Array (
  7. "0" => array ( "id" => "259", "name" => "Sklep | Promocje", "parent_id" => "0", "ip" => "259" ),
  8. "1" => array ( "id" => "238", "name" => "Sprzęt nowy", "parent_id" => "0", "ip" => "238" ),
  9. "2" => array ( "id" => "185", "name" => "Sprzęt używany", "parent_id" => "0", "ip" => "185" ),
  10. "3" => array ( "id" => "184", "name" => "WypoĹźyczalnia", "parent_id" => "0", "ip" => "184" ),
  11. "4" => array ( "id" => "200", "name" => "Agregaty prądotwórcze", "parent_id" => "184", "ip" => "184.200" ),
  12. "5" => array ( "id" => "201", "name" => "Betoniarki", "parent_id" => "184", "ip" => "184.201" ),
  13. "6" => array ( "id" => "202", "name" => "Drabiny", "parent_id" => "184", "ip" => "184.202" ),
  14. "7" => array ( "id" => "258", "name" => "Gilotyna do bruku", "parent_id" => "184", "ip" => "184.258" ),
  15. "8" => array ( "id" => "204", "name" => "Koparki", "parent_id" => "184", "ip" => "184.204" ),
  16. "9" => array ( "id" => "203", "name" => "Listwy wibracyjne", "parent_id" => "184", "ip" => "184.203" ),
  17. "10" => array ( "id" => "205", "name" => "Młotowiertarki", "parent_id" => "184", "ip" => "184.205" ),
  18. "11" => array ( "id" => "206", "name" => "Młoty", "parent_id" => "184", "ip" => "184.206" ),
  19. "12" => array ( "id" => "207", "name" => "Myjki ciśnieniowe", "parent_id" => "184", "ip" => "184.207" ),
  20. (...)
  21. "71" => array ( "id" => "273", "name" => "siala", "parent_id" => "270", "ip" => "265.267.270.273" ) );
  22. echo "<ul class=\"cat\">";
  23. $tree = getTree($arr2, 184);
  24. $j = 0;
  25. $iter = 0;
  26. $x = 0;
  27. $col = null;
  28. foreach ($tree as $sub) {
  29. $ip = explode(".", $sub['ip']);
  30. if(!isset($ip[2])) $x++;
  31. // Jeżeli jest podkategoria to dodaje <ul>, jeżeli nie, zamyka </li>
  32. if(isset($tree[$j+1]) && $tree[$j+1]["level"]>$sub["level"]) {
  33. $close = "<ul class=\"none\">";
  34. $hascat = "class=\"hascat\"";
  35. }
  36. else {
  37. $close = "</li>";
  38. $hascat = "";
  39. if($x%10 == 0) $col = "</ul><ul class=\"cat\">";
  40. else $col = null;
  41. }
  42. echo "<li><a href=\"#\" ".$hascat.">".$sub["name"]." </a>"." <small>".$sub["ip"]."; ".$x."</small>";
  43. echo $close;
  44. if(!isset($ip[2])) {
  45. echo $col;
  46. }
  47.  
  48. //Zamyka ul i li tyle razy ile trzeba
  49. if(isset($tree[$j+1]) && $tree[$j+1]["level"]<$sub["level"]) {
  50. for($i1=$tree[$j+1]["level"]; $i1<$sub["level"]; $i1++) {
  51. echo "</ul></li>";
  52. }
  53. }
  54. //Zamyka ul, li na końcu
  55. if(!isset($tree[$j+1])) {
  56. for($i2=0; $i2<$sub["level"]; $i2++) {
  57. echo "</ul></li>";
  58. }
  59. }
  60.  
  61. $j++;
  62. }
  63. echo "</ul>";
  64. ?>


Kod funkcji getTree()
  1. <?php
  2. function getTree($cat = null, $parentId = 0, $level = 0, $result = null){
  3. if ($result == null)
  4. $result = array();
  5. if ($cat == null) {
  6. echo "Brak tablicy";
  7. }
  8. foreach ($cat as $kat) {
  9. if ($kat["parent_id"] == $parentId) {
  10. $kat["level"] = $level;
  11. $result[] = $kat;
  12. $result = getTree($cat, $kat["id"], ($level+1), $result);
  13. }
  14. }
  15. return $result;
  16. }
  17. ?>