Code:
--
-- Table structure for table `lokacije`
--
CREATE TABLE IF NOT EXISTS `lokacije` (
`Ime` text NOT NULL,
`id` int(11) NOT NULL,
`parent` int(11) NOT NULL,
`map` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `lokacije`
--
INSERT INTO `lokacije` (`Ime`, `id`, `parent`, `map`) VALUES
('Srbija', 1, 0, 'RS'),
('Beograd', 2, 1, 'x'),
('Hrvatska', 3, 0, 'HR'),
('Rusija', 4, 0, 'RU'),
('Zrenjanin', 8, 1, 'ZR');
--
-- Table structure for table `lokacije`
--
CREATE TABLE IF NOT EXISTS `lokacije` (
`Ime` text NOT NULL,
`id` int(11) NOT NULL,
`parent` int(11) NOT NULL,
`map` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `lokacije`
--
INSERT INTO `lokacije` (`Ime`, `id`, `parent`, `map`) VALUES
('Srbija', 1, 0, 'RS'),
('Beograd', 2, 1, 'x'),
('Hrvatska', 3, 0, 'HR'),
('Rusija', 4, 0, 'RU'),
('Zrenjanin', 8, 1, 'ZR');
Iz koje napravi ovako:
Code:
Array
(
[0] => Array
(
[0] => Srbija
[Ime] => Srbija
[1] => 1
[id] => 1
[2] => 0
[3] => RS
[map] => RS
[child] => Array
(
[0] => Array
(
[0] => Beograd
[Ime] => Beograd
[1] => 2
[id] => 2
[2] => 1
[3] => x
[map] => x
[child] => Array
(
)
)
[1] => Array
(
[0] => Zrenjanin
[Ime] => Zrenjanin
[1] => 8
[id] => 8
[2] => 1
[3] => ZR
[map] => ZR
[child] => Array
(
)
)
)
)
[1] => Array
(
[0] => Hrvatska
[Ime] => Hrvatska
[1] => 3
[id] => 3
[2] => 0
[3] => HR
[map] => HR
[child] => Array
(
)
)
[2] => Array
(
[0] => Rusija
[Ime] => Rusija
[1] => 4
[id] => 4
[2] => 0
[3] => RU
[map] => RU
[child] => Array
(
)
)
)
Array
(
[0] => Array
(
[0] => Srbija
[Ime] => Srbija
[1] => 1
[id] => 1
[2] => 0
[3] => RS
[map] => RS
[child] => Array
(
[0] => Array
(
[0] => Beograd
[Ime] => Beograd
[1] => 2
[id] => 2
[2] => 1
[3] => x
[map] => x
[child] => Array
(
)
)
[1] => Array
(
[0] => Zrenjanin
[Ime] => Zrenjanin
[1] => 8
[id] => 8
[2] => 1
[3] => ZR
[map] => ZR
[child] => Array
(
)
)
)
)
[1] => Array
(
[0] => Hrvatska
[Ime] => Hrvatska
[1] => 3
[id] => 3
[2] => 0
[3] => HR
[map] => HR
[child] => Array
(
)
)
[2] => Array
(
[0] => Rusija
[Ime] => Rusija
[1] => 4
[id] => 4
[2] => 0
[3] => RU
[map] => RU
[child] => Array
(
)
)
)
Pomocu ovog koda:
Code:
<?php
include 'baza.php';
$query="select * from lokacije";
$q=mysql_query($query);
$nodes=array();
$ime="";
$id="";
$parent="";
$oznaka="";
while($row=mysql_fetch_array($q)) {
$nodes[]=$row;
}
$p = array(0 => array());
foreach($nodes as $n)
{
$pid = $n['parent'];
$id = $n['id'];
if (!isset($p[$pid]))
$p[$pid] = array('child' => array());
if (isset($p[$id]))
$child = &$p[$id]['child'];
else
$child = array();
$p[$id] = $n;
$p[$id]['child'] = &$child;
unset($p[$id]['parent']);
unset($child);
$p[$pid]['child'][] = &$p[$id];
}
$nodes = $p['0']['child'];
unset($p);
print "<pre>";
print_r($nodes);
print "</pre>";
?>
<?php
include 'baza.php';
$query="select * from lokacije";
$q=mysql_query($query);
$nodes=array();
$ime="";
$id="";
$parent="";
$oznaka="";
while($row=mysql_fetch_array($q)) {
$nodes[]=$row;
}
$p = array(0 => array());
foreach($nodes as $n)
{
$pid = $n['parent'];
$id = $n['id'];
if (!isset($p[$pid]))
$p[$pid] = array('child' => array());
if (isset($p[$id]))
$child = &$p[$id]['child'];
else
$child = array();
$p[$id] = $n;
$p[$id]['child'] = &$child;
unset($p[$id]['parent']);
unset($child);
$p[$pid]['child'][] = &$p[$id];
}
$nodes = $p['0']['child'];
unset($p);
print "<pre>";
print_r($nodes);
print "</pre>";
?>
Kako da to pretvorim u
Srbija
- Beograd
- Zrenjanin
Hrvatska
Rusija
da ide rekurzivno ili kako vec, da nije limitirano na dubinu od 2-3...nego da moze i 10 u dubinu?