Hi!
I am developing a page where I have categories at many levels. I would like to always show the categories on level 1. When one category at level 1 is selected, I would like to show the categories that belong to this category and at the same time show all categories at level 1. I would like to use undetermined number of levels. And it would be different numbers of levels at every category.
I am using a mySQL databse. I have tried to make this script, but I cant make it work.
I am using a tabel with:
category_id,category_dad and category_name
category_dad is the category_id to the category that this category belongs to.
Categories at level 1 has a category_dad=0.
The code i have tried:
<?php
function write_category($category_id,$category_dad){
$sql ="select category_name, category_id,category_dad from category";
$sql=$sql." where category_dad=$category_dad";
$sql=$sql." order by category_name";
$resultat = mysql_query($sql);
$resultat2 = mysql_query($sql);
$s=0;
$r=0;
while($rad = mysql_fetch_array($resultat)){
if($category_id==$category_dad){
$s=1;
$r=1;
}
if($s==0){
if(write_category($category_id,$rad["category_id"])){
$r=1;
$s=1;
}
}
if($s==0){
if($category_id==$rad["category_id"]){
$s=1;
$r=1;
}
}
}
if($s==1){
while($rad = mysql_fetch_array($resultat2)){
echo "<tr><td ><a class='menu' target='_self' href='meny.php?category_id=".$rad["category_id"]."'>".$rad["category_name"]."</a></td></tr>";
}
}
if($r==1){
return true;
}
}
if(!isset($category_id))$category_id=0;
write_category($category_id,0);
?>
This code almost work. But i would like to print it out like this:
Airplanes
Cars
Opel
BMW
Mercedes
Motobikes
Not like this:
Opel
BMW
Mercedes
Airplanes
Cars
Motobikes
When Opel,BMW and Mercedes belong to the category Cars..
Can anybody please help me?
Jørn Arild Andenæs
jaa@jaa.no
I am developing a page where I have categories at many levels. I would like to always show the categories on level 1. When one category at level 1 is selected, I would like to show the categories that belong to this category and at the same time show all categories at level 1. I would like to use undetermined number of levels. And it would be different numbers of levels at every category.
I am using a mySQL databse. I have tried to make this script, but I cant make it work.
I am using a tabel with:
category_id,category_dad and category_name
category_dad is the category_id to the category that this category belongs to.
Categories at level 1 has a category_dad=0.
The code i have tried:
<?php
function write_category($category_id,$category_dad){
$sql ="select category_name, category_id,category_dad from category";
$sql=$sql." where category_dad=$category_dad";
$sql=$sql." order by category_name";
$resultat = mysql_query($sql);
$resultat2 = mysql_query($sql);
$s=0;
$r=0;
while($rad = mysql_fetch_array($resultat)){
if($category_id==$category_dad){
$s=1;
$r=1;
}
if($s==0){
if(write_category($category_id,$rad["category_id"])){
$r=1;
$s=1;
}
}
if($s==0){
if($category_id==$rad["category_id"]){
$s=1;
$r=1;
}
}
}
if($s==1){
while($rad = mysql_fetch_array($resultat2)){
echo "<tr><td ><a class='menu' target='_self' href='meny.php?category_id=".$rad["category_id"]."'>".$rad["category_name"]."</a></td></tr>";
}
}
if($r==1){
return true;
}
}
if(!isset($category_id))$category_id=0;
write_category($category_id,0);
?>
This code almost work. But i would like to print it out like this:
Airplanes
Cars
Opel
BMW
Mercedes
Motobikes
Not like this:
Opel
BMW
Mercedes
Airplanes
Cars
Motobikes
When Opel,BMW and Mercedes belong to the category Cars..
Can anybody please help me?
Jørn Arild Andenæs
jaa@jaa.no