|
Posted by Jan Morten Srensen on 02/08/07 15:47
"Rik" <luiheidsgoeroe@hotmail.com> skrev i meddelandet
news:op.tnfquerkqnv3q9@misant.kabel.utwente.nl...
> P Pulkkinen <perttu.POISTATAMA.pulkkinen@POISTATAMA.elisanet.fi> wrote:
>
> >
> > "Jan Morten Srensen" <jms@sensewave.com> kirjoitti
> > viestiss:55adb$45cb2e90$54d23962$12085@news.chello.no...
> >
> >> What then if I wanted to have multiple parents maybe even from
different
> >> tree levels?
> >> (Is that possible with your solution?)
> >>
> >> I wrote a solution to this last nite after posting this question and it
> >> was
> >> smashing. I am thinking about publishing it somewhere because I know
> >> others
> >> have the same problem as I do.
> >
> > I don't get what you mean. Do you mean that a category would have many
> > parents, like in a real world we have both father and mother? And those
> > parents were from different "generation".
>
> He means the data is not really a tree, just relations. An odd structure
> for a menu, but possible.
I'll put out an example menu for you!
main_category
---subcategory1
------subcategory2
---subcategory2
---subcategory3
main_category
---subcategory1
------subcategory2
---------subcategory3
main_category
main_category
etc...
as you can see the subcategories can be present in many variable levels and
have several parents and children...
I have solved this issue like this:
<?php
class MenuElement {
var $id;
var $text;
var $parents;
var $gotParents = false;
var $gotChildren = false;
var $children;
var $num_par = 0;
var $num_chi = 0;
var $level = '';
public function __construct($id, $text) {
$this->id = $id;
$this->text = $text;
}
public function isParent() {
return $this->gotChildren;
}
public function isChild() {
return $this->gotParents;
}
public function setParent($id) {
$this->parents[$this->num_par] = $id;
$this->num_par++;
}
public function loopChildren($element, &$list, &$k) {
for ($i=0;$i<count($this->children);$i++) {
for ($j=0;$j<count($element);$j++) {
if ($this->children[$i] == $element[$j]->getId()) {
if ($element[$j]->getLevel() == '')
$element[$j]->setLevel($this->level+1);
$list[$k] = $element[$j];
$k++;
if ($this->gotChildren) {
$element[$j]->loopChildren($element, $list, $k);
}
}
}
}
}
public function setChild($id) {
$this->children[$this->num_chi] = $id;
$this->num_chi++;
}
public function setParenthood($gotParent) {
$this->gotParents = $gotParent;
}
public function setChildhood($gotChild) {
$this->gotChildren = $gotChild;
}
public function getId() {
return $this->id;
}
public function getLevel() {
return $this->level;
}
public function setLevel($level) {
$this->level = $level;
}
public function getElementText() {
return $this->text;
}
}
// First fetch categories sorted.
$result = getCategories($link, $language);
$result2 = getAllRelationships($link, $language);
$i = 0;
// Then find main_categories
while ($row = mysql_fetch_assoc($result)) {
$gotParents = false;
$gotChildren = false;
$element[$i] = new MenuElement($row['id'], $row['navn']);
if (mysql_num_rows($result2) > 0) {
mysql_data_seek($result2, 0);
}
while ($row2 = mysql_fetch_assoc($result2)) {
if ($row['id'] == $row2['childid']) {
if ($row2['parentid'] != 0) {
$gotParents = true;
$element[$i]->setParent($row2['parentid']);
}
}
if ($row['id'] == $row2['parentid']) {
$gotChildren = true;
$element[$i]->setChild($row2['childid']);
}
}
$element[$i]->setParenthood($gotParents);
$element[$i]->setChildhood($gotChildren);
$i++;
}
$j = 0;
$k = 0;
while ($j < $i) {
if (!$element[$j]->isChild()) { // If main_category
$level = 0;
$element[$j]->setLevel($level);
$list[$k] = $element[$j];
$k++;
$element[$j]->loopChildren($element, $list, $k);
}
$j++;
}
echo '<ul>';
for ($i = 0;$i < count($list);$i++) {
echo '<li>' . str_repeat("--", $list[$i]->getLevel()) .
$list[$i]->getElementText();
}
echo '</ul>';
?>
Now you end up with a list of all the elements in a sorted order and with
all variable data present. Only need for two mysql queries...
--
Jan Morten
http://www.sitconsulting.no
Billig datahjelp i Oslo omrdet.
Navigation:
[Reply to this message]
|