mam skrypt drzewek
klasy:
tree.class.php:
<?php
class trees {
//poziom zagniezdzenia
var $maxNest = 5; //liczone od zera
function trees() {
$this ->db =& new db;
return true;
}
/////////////////////////
// pobieranie drzewka
function getAll ( $cluster, $depth = null ) {
$depth = (is_null($depth))?
$this->maxNest:$depth;
return $this->db->getArray("
SELECT *, INSTR(level,'0')-1 as depth FROM `groups` WHERE cluster = ".$cluster." AND INSTR (level, '0')-` <= ".$depth." ORDER BY level
");
}
function getPart ( $parent_id, $depth = null ) {
$depth = (is_null($depth))?
$this->maxNest:$depth;
$r = $this->db->getRow("
SELECT cluster, SUBSTRING(level, 1, INSTR(level, '0')-1) as cutLevel, INSTR (level, '0')-1 as depth FROM `groups` WHERE id=".$parent_id
);
if(!$r) {return false;}
return $this->db->getArray("
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
");
}
function newGroup ($name){
$levelNum = $this->maxNest+2;
return $this->db->execute ("
INSERT INTO `groups` (cluster, name, level) SELECT MAX(cluster)+1, '".$name."', RPAD('0', ".$levelNum.", '0') FROM `groups`
");
}
function newChild ($name, $parentId) {
$levelNum = $this->maxNest+2;
$parent = $this->db->getRow("
SELECT cluster, SUBSTRING (level, 1, INSTR, (level,'0')-1) as cutLevel, INSTR(level,'0') - 1 as depth FROM `groups` WHERE id=".$parentId
);
if(!$parent) {return false;}
return $this->db->execute("
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
");
}
function delete ($id ) {
$r = $this->db->getRow("
select cluster, SUBSTRING(level, 1, INSTR(level, '0')-1) as cutLevel FROM `groups` WHERE id=".$id
);
if(!$r) {return false;}
return $this->db->execute("
DELETE FROM `groups` WHERE level like '".$r['cutLevel']."%' AND cluster=".$r['cluster']);
}
}
?>
a w bazie mam tak:
-- Struktura tabeli dla `groups`
--
CREATE TABLE `groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cluster` int(11) NOT NULL DEFAULT '0',
`name` varchar(100) NOT NULL DEFAULT '',
`level` decimal(10,0) NOT NULL DEFAULT '0',
UNIQUE KEY `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=63 ;
--
-- Zrzut danych tabeli `groups`
--
INSERT
INTO `groups` VALUES (55, 1, 'Win 2000', 1210000);
INSERT
INTO `groups` VALUES (54, 1, 'Win Milenium', 1130000);
INSERT
INTO `groups` VALUES (53, 1, 'Win 98', 1120000);
INSERT
INTO `groups` VALUES (51, 1, 'NT', 1200000);
INSERT
INTO `groups` VALUES (52, 1, 'Win 95', 1110000);
INSERT
INTO `groups` VALUES (49, 1, 'No NT', 1100000);
INSERT
INTO `groups` VALUES (48, 1, 'Linux', 2000000);
INSERT
INTO `groups` VALUES (47, 1, 'Windows', 1000000);
INSERT
INTO `groups` VALUES (46, 1, 'Systems', 0000000);
INSERT
INTO `groups` VALUES (57, 1, 'Win 2003', 1230000);
INSERT
INTO `groups` VALUES (56, 1, 'Win XP', 1220000);
INSERT
INTO `groups` VALUES (61, 1, 'Slack', 2100000);
INSERT
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