Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: drzewo categorii
Forum PHP.pl > Forum > PHP
atomp3
Witam chcialbym zbudowac drzewo kategorii do sklepu int.

Przyklad:
video
dvd
action

:MYSQL
categoryID
name
parent
...

Przykladowe dane z tabeli:

0 video 0
1 dvd 0
2 action 1

Jak zbudowac taka funckje?

Znalazlem przykladaowa ale nie dziala jak powinna nie wiem czemu

  1. <?php
  2. function fillTheCList($parent,$level) //completely expand category tree
  3. {
  4.  
  5. global $sql_table,$default_order,$db;
  6.  
  7. $q = $db->query("SELECT categoryID, name, products_count, products_count_admin, parent FROM ".$sql_table." WHERE categoryID<>0 and parent='".$parent."' ORDER BY name");
  8. $a = array(); //parents
  9.  
  10.  
  11. while ($row = $db->db_fetch_row($q))
  12. {
  13.  
  14. $row[5] = $level;
  15. $a[] = $row;
  16. //process subcategories
  17. $b = fillTheCList($row[0],$level+1);
  18.  
  19. //add $b[] to the end of $a[]
  20. for ($j=0; $j<count($b); $j++)
  21. {
  22. $a[] = $b[$j];
  23. }
  24. }
  25.  
  26. return $a;
  27.  
  28. } //fillTheCList
  29.  
  30. $smarty->assign('categories', fillTheCList(0,0));
  31. ?>


Dzieki za wszelka pomoc
arecki
Ja mam np. tak:

  1. <?php
  2. function getCategoriesTree($params) {
  3. $categoryTree = array();
  4. $stmt = $params['db']->prepare('SELECT *, id as parentID, (SELECT count(*) AS childrenNo FROM categories WHERE parent = parentID) AS childrenNo
  5.  FROM categories WHERE parent = :parentID ORDER BY parent, sort, name');
  6. $stmt->bindValue(':parentID', (key_exists('parent', $params) ? $params['parent'] : 0), PDO::PARAM_INT);
  7. if($stmt->execute()) {
  8. $categoryTree = $stmt->fetchAll();
  9. foreach ($categoryTree as $key=>$leaf) {
  10. if($leaf['childrenNo'] > 0 )
  11. $categoryTree[$key]['childrens'] = $this->getCategoriesTree(array('db'=>$params['db'], 'parent'=>$leaf['id']));
  12. }
  13. return $categoryTree;
  14. }
  15. return false;
  16. }
  17.  
  18. $categoryTree = getCategoriesTree(array('db'=>$pdoObject, 'parent'=>0));
  19. ?>
atomp3
a moze jakis prostszy przyklad? taki nie wyciagniety z klasy?

Mam taka funckje

Kod
function pokazkategorie($kategoria = 0, $lev = 0)
{

global $sql_table,$default_order,$db;

$sql = $db->query("SELECT * FROM ".$sql_table." WHERE parent = $kategoria");
while($row = $db->fetcharray($sql))
{

pokazkategorie($row['categoryID'], ++$lev);
echo $row['name'];
}
}
pokazkategorie();


Wszytko pieknie ntylko dlaczego zatrzymuje sie na pierwszej kategorii i pierwszej podaktegorii. Naprawde juz mi brakuje pomyslow
Xniver
A to działa poprawnie?
  1. <?php
  2. class CategoryTree
  3. {
  4. protected $childs  = array();
  5. protected $categories = array();
  6.  
  7. public function __construct()
  8. {
  9. $this->categories = $this->getCategories();
  10. $this->childs  = $this->getChilds();
  11. }
  12.  
  13. protected function getChilds()
  14. {
  15. foreach($this->categories as $category)
  16. {
  17. $childs[$category['parent']][] = $category['categoryID'];
  18. }
  19.  
  20. return $childs;
  21. }
  22.  
  23. protected function getCategories()
  24. {
  25. global $sql_table, $default_order, $db;
  26.  
  27. $categories = array();
  28.  
  29. $query = $db->query('SELECT * FROM ' . $sql_table);
  30.  
  31. while($row = $db->fetcharray($query))
  32. {
  33. $categories[$row['categoryID']] = $row;
  34. }
  35.  
  36. return $categories;
  37. }
  38.  
  39. public function displayCategories($catId = 0, $lev = '')
  40. {
  41. if($catId != 0)
  42. {
  43. echo $lev . ' ' . $this->categories[$catId]['name'];
  44. }
  45.  
  46. if(is_array($this->childs[$catId]))
  47. {
  48. foreach($this->childs[$catId] as $childId)
  49. {
  50. $this->displayCategories($childId, $lev . '-');
  51. }
  52. }
  53. }
  54. }
  55.  
  56. $category = new CategoryTree();
  57.  
  58. $category->displayCategories();
  59. ?>
atomp3
Dzieki za bardzo dobry przyklad klasy. Wlasnie o to mi chodzilo smile.gif

Jeszcze szybkie pytanie potrzebuje wynik w tablicy tak aby wyswietlic w petli poprzez smarty

  1. <?php
  2. jak dam return $this->categories[$catId]; to mi nie zwraca tablicy dziala tylko print_r i echo
  3. ?>
Xniver
  1. <?php
  2. class CategoryTree
  3. {
  4. protected $childs  = array();
  5. protected $categories = array();
  6. protected $dataArray = array();
  7.  
  8. public function __construct()
  9. {
  10. $this->categories = $this->getCategories();
  11. $this->childs  = $this->getChilds();
  12. }
  13.  
  14. protected function getChilds()
  15. {
  16. foreach($this->categories as $category)
  17. {
  18. $childs[$category['parent']][] = $category['categoryID'];
  19. }
  20.  
  21. return $childs;
  22. }
  23.  
  24. protected function getCategories()
  25. {
  26. global $sql_table, $default_order, $db;
  27.  
  28. $categories = array();
  29.  
  30. $query = $db->query('SELECT * FROM ' . $sql_table);
  31.  
  32. while($row = $db->fetcharray($query))
  33. {
  34. $categories[$row['categoryID']] = $row;
  35. }
  36.  
  37. return $categories;
  38. }
  39.  
  40. public function generateCategories($catId = 0, $lev = '')
  41. {
  42. if($catId != 0)
  43. {
  44. $this->dataArray[] = $lev . ' ' . $this->categories[$catId]['name'];
  45. }
  46.  
  47. if(is_array($this->childs[$catId]))
  48. {
  49. foreach($this->childs[$catId] as $childId)
  50. {
  51. $this->generateCategories($childId, $lev . '-');
  52. }
  53. }
  54. }
  55.  
  56. public function getGeneratedArray()
  57. {
  58. return $this->dataArray;
  59. }
  60. }
  61.  
  62. $category = new CategoryTree();
  63.  
  64. $category->generateCategories();
  65.  
  66. $categories = $category->getGeneratedArray();
  67. ?>
atomp3
super! dzieki bardzo.

Ciagnac dalej temat nasuwa sie pytanie jak sortowac kategorie? Czy zrobic to na poziomie tablicy czy zapytania?
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.