Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> drzewka - wyswietlanie w php
Legro
post
Post #1





Grupa: Zarejestrowani
Postów: 233
Pomógł: 3
Dołączył: 18.07.2005

Ostrzeżenie: (40%)
XX---


mam skrypt drzewek

klasy:

tree.class.php:

  1. <?php
  2.  
  3. class trees {
  4.  
  5. //poziom zagniezdzenia
  6. var $maxNest = 5; //liczone od zera
  7.  
  8. function trees() {
  9.  
  10. $this ->db =& new db;
  11.  
  12. return true;
  13.  
  14. }
  15.  
  16. /////////////////////////
  17.  
  18. // pobieranie drzewka
  19.  
  20. function getAll ( $cluster, $depth = null ) {
  21.  
  22. $depth = (is_null($depth))?$this->maxNest:$depth;
  23.  
  24. return $this->db->getArray("
  25. SELECT *, INSTR(level,'0')-1 as depth FROM `groups` WHERE cluster = ".$cluster." AND INSTR (level, '0')-` <= ".$depth." ORDER BY level
  26. ");
  27.  
  28.  
  29. }
  30.  
  31. function getPart ( $parent_id, $depth = null ) {
  32.  
  33. $depth = (is_null($depth))?$this->maxNest:$depth;
  34.  
  35. $r = $this->db->getRow("
  36. SELECT cluster, SUBSTRING(level, 1, INSTR(level, '0')-1) as cutLevel, INSTR (level, '0')-1 as depth FROM `groups` WHERE id=".$parent_id
  37. );
  38.  
  39. if(!$r) {return false;}
  40.  
  41. return $this->db->getArray("
  42. SELECT *, INSTR(level,'0')-1 as depth, INSTR(level,'0')-1 - ".$r['depth']." as relativeDepth FROM `groups` WHERE cluster = ".$r['cutLevel']."%' AND INSTR (level, '0')-1 <= (".$depth."+".$r['depth'].") ORDER BY level
  43. ");
  44.  
  45.  
  46. }
  47.  
  48. function newGroup ($name){
  49.  
  50. $levelNum = $this->maxNest+2;
  51.  
  52. return $this->db->execute ("
  53. INSERT INTO `groups` (cluster, name, level) SELECT MAX(cluster)+1, '".$name."', RPAD('0', ".$levelNum.", '0') FROM `groups`
  54. ");
  55.  
  56. }
  57.  
  58. function newChild ($name, $parentId) {
  59.  
  60. $levelNum = $this->maxNest+2;
  61. $parent = $this->db->getRow("
  62. SELECT cluster, SUBSTRING (level, 1, INSTR, (level,'0')-1) as cutLevel, INSTR(level,'0') - 1 as depth FROM `groups` WHERE id=".$parentId
  63. );
  64.  
  65. if(!$parent) {return false;}
  66.  
  67. return $this->db->execute("
  68. INSERT INTO groups (cluster, name, level) select ".$parent['cluster'].", '".$name."', RPAD(SUBSTRING(g.level, 1, ".$parent['depth']." + 1)+1, ".$levelNum.",'0') FROM groups AS g LEFT JOIN groups AS p ON CONCAT (g.cluster,'|',RPAD(SUBSTRING(g.level, 1, ".$parent['depth']." + 1) +1, ".$levelNum.", '0')) = CONCAT (p.cluster, '|', p.level) WHERE g.cluster = ".$parent['cluster']." adn g.level like CONCAT ('".$parent['cutLevel']."', '%') and ".$parent['depth']." + 1 <= ".$levelNum."-1 adn p.level is null limit 1
  69. ");
  70.  
  71. }
  72.  
  73. function delete ($id ) {
  74.  
  75. $r = $this->db->getRow("
  76. select cluster, SUBSTRING(level, 1, INSTR(level, '0')-1) as cutLevel FROM `groups` WHERE id=".$id
  77. );
  78.  
  79. if(!$r) {return false;}
  80.  
  81. return $this->db->execute("
  82. DELETE FROM `groups` WHERE level like '".$r['cutLevel']."%' AND cluster=".$r['cluster']);
  83.  
  84. }
  85.  
  86. }
  87.  
  88. ?>


a w bazie mam tak:

  1. -- Struktura tabeli dla `groups`
  2. --
  3.  
  4. CREATE TABLE `groups` (
  5. `id` int(11) NOT NULL AUTO_INCREMENT,
  6. `cluster` int(11) NOT NULL DEFAULT '0',
  7. `name` varchar(100) NOT NULL DEFAULT '',
  8. `level` decimal(10,0) NOT NULL DEFAULT '0',
  9. UNIQUE KEY `id` (`id`)
  10. ) TYPE=MyISAM AUTO_INCREMENT=63 ;
  11.  
  12. --
  13. -- Zrzut danych tabeli `groups`
  14. --
  15.  
  16. INSERT
  17. INTO `groups` VALUES (55, 1, 'Win 2000', 1210000);
  18. INSERT
  19. INTO `groups` VALUES (54, 1, 'Win Milenium', 1130000);
  20. INSERT
  21. INTO `groups` VALUES (53, 1, 'Win 98', 1120000);
  22. INSERT
  23. INTO `groups` VALUES (51, 1, 'NT', 1200000);
  24. INSERT
  25. INTO `groups` VALUES (52, 1, 'Win 95', 1110000);
  26. INSERT
  27. INTO `groups` VALUES (49, 1, 'No NT', 1100000);
  28. INSERT
  29. INTO `groups` VALUES (48, 1, 'Linux', 2000000);
  30. INSERT
  31. INTO `groups` VALUES (47, 1, 'Windows', 1000000);
  32. INSERT
  33. INTO `groups` VALUES (46, 1, 'Systems', 0000000);
  34. INSERT
  35. INTO `groups` VALUES (57, 1, 'Win 2003', 1230000);
  36. INSERT
  37. INTO `groups` VALUES (56, 1, 'Win XP', 1220000);
  38. INSERT
  39. INTO `groups` VALUES (61, 1, 'Slack', 2100000);
  40. INSERT
  41. INTO `groups` VALUES (62, 1, 'Debian', 2200000);



i jak w php wyswietlic całe drzewko (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
tz. nie chodzi mi o samo zapytanie bo mam ale jak wysiwertlic to jeszcze na stronie w php (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

Ten post edytował Legro 11.08.2005, 14:18:23
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 5.10.2025 - 18:44