Hey look, i have a table named “cats” with 3 columns: “parent”, “ID” and “categ”… This is about categories and sub categories…
the column “parent” refers to the ID of an existent entry…
the column “ID” refers to it’s ID…
and “categ” is its name…
so if i have this
ID – categ – parent
1 – abc – 0
2 – def – 1
the category “abc” will be in the root category and the entry “def” will be a subcategory of the “abc” category
Now… I was makin a function for filling a combobox with all the categories, BUT if theres a subcategory, i’d like to put it below it’s parent category and depending of it’s sublevel, add “-” (i.e. if it’s sublevel is 3 then add “—“)
So this is the code:
[code=php]
<?php
function categorias($parent=0, $sub=0, $exc=array(“0”)) {
global $catID;
$sql = “SELECT * FROM cats WHERE parent = ‘”.$parent.”‘”;
$doSql = @mysql_query($sql);
while ($row_doSql = @mysql_fetch_array($doSql)) {
$result .= ($row_doSql[‘ID’] == $catID) ? ‘<option value=”‘.$row_doSql[‘ID’].'” selected>’ : ‘<option value=”‘.$row_doSql[‘ID’].'”>’;
for ($i=0; $i<$sub; $i++) {
$result .= “-“;
}
$result .= $row_doSql[‘categ’];
$result .= ‘</option>’.”n”;
}
$sql2 = “SELECT * FROM cats WHERE”;
foreach ($exc as $k => $v) {
$sql2 .= ” parent != ‘”.$v.”‘ AND”;
}
$sql2 = substr($sql2,0,strlen($sql2)-4);
$doSql2 = @mysql_query($sql2);
while ($row_doSql2 = @mysql_fetch_array($doSql2)) {
$exc[] = $row_doSql2[‘parent’];
$result .= categorias($row_doSql2[‘parent’], $sub+1, $exc);
$sql3 = “SELECT * FROM cats WHERE ID = ‘”.$row_doSql2[‘parent’].”‘ LIMIT 1″;
$doSql3 = @mysql_query($sql3);
$row_doSql3 = @mysql_fetch_array($doSql3);
$parent = $row_doSql3[‘parent’];
$sub=0;
}
return $result;
}
?>
The problem is that the subcategory appears at the bottom not below it’s parent category…
How could i fix this..??
Thanx…