|
<?php
include 'common.php';
class Tree {
public $lvl = 1;
public $nodes = array();
public function __construct() {
$this->fill_nodes();
}
public function fill_nodes() {
$sql = "SELECT id, title, parent, p_order FROM pages ORDER BY p_order";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$vals = array();
$vals['id'] = $row['id'];
$vals['title'] = $row['title'];
$vals['parent'] = $row['parent'];
$vals['p_order'] = $row['p_order'];
$this->nodes[] = $vals;
}
}
public function has_children($id) {
foreach ($this->nodes as $row)
if ($row['parent'] == $id) return true;
return false;
}
public function tree($pid = 0) {
foreach ($this->nodes as $row) {
if ($row['parent'] == $pid) {
if ($this->has_children($row['id'])) {
print str_repeat('-', $this->lvl).$row['title']. '<br />';
$this->lvl++;
$this->tree($row['id']);
} else {
print str_repeat('-', $this->lvl).$row['title']. '<br />';
}
}
}
$this->lvl = 1;
}
public function drop_down($current = 0, $op = '') {
foreach ($this->nodes as $row) {
if ($row['parent'] == $pid) {
if ($this->has_children($row['id'])) {
$op .= '<option value = "'.$row['id'].'"';
$op .= ($row['id'] == $current) ? ' SELECTED' : '';
$op .= '>'.str_repeat('-', $this->lvl).$row['title'].'</option>';
$this->lvl++;
$this->tree($row['id'], $op);
} else {
$op .= '<option value = "'.$row['id'].'"';
$op .= ($row['id'] == $current) ? ' SELECTED' : '';
$op .= '>'.str_repeat('-', $this->lvl).$row['title'].'</option>';
}
}
}
$this->lvl = 1;
return $op;
}
}
$tree = new Tree();
$tree->tree();
print '<br /><br />';
print '<select name = "pages">';
print $tree->drop_down();
print '</select>';
?>
|