Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: drzewka - wyswietlanie w php
Forum PHP.pl > Forum > Przedszkole
Legro
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 questionmark.gif
tz. nie chodzi mi o samo zapytanie bo mam ale jak wysiwertlic to jeszcze na stronie w php questionmark.gif
NuLL
Napisałeś sobie skrypt drzewka którego nie umiesz wyśwetlić ? Może lepiej go nie pisać questionmark.gif
Legro
nie to nie ja pisalem to jest z pliku pdf rzSeatla.. cały przepisywałem kod chyba 3 razy z pliku pdf bo byly bledy..

odczytuje np. tak:

  1. <?php
  2.  
  3. mysql_pconnect ("localhost", "***", "***");
  4. mysql_select_db ("kategorie");
  5. include ("tree.class.php");
  6.  
  7. $t =& new trees;
  8. $arr = $t->getPart(47);
  9.  
  10. foreach ($arr as $val) {
  11.  
  12. print str_repeat(" ", $val['depth']).' '.$val['id'].' '.$val['name']."<br />";
  13.  
  14. }
  15.  
  16. ?>


i jak otwieram plik to pisze:

Cytat
Fatal error: Class 'db' not found in c:\usr\krasnal\www\kategorie\tree.class.php on line 10


i własnie nie wiem jak inaczej to zrobic :/
zeby sie bvlad nie pokazywał :/

ta linie 10 mam w poprzedmim poscie
aleksander
@NuLL: to nie jego drzewka tongue.gif

http://www.php.pl/artykuly/aplikacje_inter...ny_wyglad_klasy

@Legro: LOL, chyba musisz sie podszkolic skoro nie widzisz tego:
  1. <?php
  2. function trees() {
  3.  
  4. $this ->db =& new db;
  5.  
  6. return true;
  7.  
  8. }
  9. ?>
Legro
Cytat
@Legro: getAll() questionmark.gif


tzn..? :roll2:

edit:
co tam jest zle ?
NuLL
Alek - nie pamiętałem co było w arcie tongue.gif

@Legro - jeśli nie widzisz że tu jest zastosowana osoba klasa do obsługi DB to nazie poucz się porządnie php.

MEGALOL swoją drogą..
Legro
dobra zrobilem ten poczatek klas tak:

  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. mysql_connect ("localhost","root","krasnal");
  13. mysql_select_db ("kat");
  14.  
  15. }
  16. ?>


i jak to mam wywolac to polaczenie z baza w pliku d.php gdzie mam:

  1. <?php
  2.  
  3. mysql_pconnect ("localhost", "root", "krasnal");
  4. mysql_select_db ("kat");
  5.  
  6. $result = "SELECT *, INSTR (level,'0')-1 AS depth FROM `groups` WHERE cluster = 1 AND INSTR (level,'0')-1 <= 5 ORDER BY level";
  7.  
  8. $wykonaj = mysql_query ($result);
  9.  
  10. $row = mysql_fetch_array ($wykonaj);
  11.  
  12. echo ''.$row = 'level'.'<br><br><br>';
  13.  
  14. ?>


mam usunac z d.php connect i selecta do bazy i jaka mam wpisac tam zeby mi sie przez klase łaczyło?

p.s czemu mi pokazuje w pliku d.php:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\usr\krasnal\www\kategorie\d.php on line 10
level

10 linia to:

$row = mysql_fetch_array ($wykonaj);

:/
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.