I have a recursive function which lists categories, sub-categories and the products from each categories
The function start to iterate from the root and creates the tree. Next I describe the 2 tables in use.
The tables are:
categories:
CatId---CatParentId--CatName
1--------0-----------------xxxx0
2--------1-----------------xxxx0-1
3--------1-----------------xxxx0-2
4--------3-----------------xxxx1-1
5--------3-----------------xxxx1-2
6--------0-----------------xxxx1
This would be a visual representation produced by the function, actually its a unsorted list (ul-li):
xxxx0 (CatId=1, CatParentId=0)
------xxxx0-1(CatId=2, CatParentId=1)
------xxxx0-2(CatId=3, CatParentId=1)
-----------------xxxxx1-1 (CatId=4, CatParentId=3)
-----------------xxxxx1-2 (CatId=5, CatParentId=3)
xxxx1 (CatId=6, CatParentId=0)
Products table:
imported_data_copy ( products)
ImpDatId----CatId---ProdName
1--------------1--------prod aaa
2--------------1--------prod bbb
3--------------3--------prod cccc
4--------------3--------prod cccc jjjj
5--------------6--------prod cccc hhhh
Because there are around 1000 products, I decided to not list the tree products at initialization, just the categories. Each time the user clicks on a category ( the tree is "closed" this means that only the root categories are shown, CatParentId=0 ), an ajax calling is executed, this one returns the products from the clicked category
At this point all works fine
As you can see, categories and products are related with CatId(fk), this field is in both tables. With CatId we know on which category a product belongs.
The problem starts when I need to add a seach input text, the objective of this seach engine ( it seaches on ProdName ), is to ONLY show the categories FROM THE ROOT NODE that are related to the searched product.
Example: the user seaches "cccc", the output should be:
xxxx0 (CatId=1, CatParentId=0, root node)
------xxxx0-2 (CatId=3, CatParentId=1)
xxxx1 (CatId=6, CatParentId=0)
My function:
Code: Select all
function recursiveTree($categoryId){
$arrStr = array();
$sql='SELECT CatId, CatName FROM '.tblCATEGORIES.' WHERE CatParentId ='.$categoryId;
$result=mysql_query($sql);
if(mysql_num_rows($result)){
if($categoryId>0){
$arrStr[]='<ul style="display:none;" class="ulCat">';
}
while($row = mysql_fetch_array($result)){
$arrStr[]='<li id="idtree_'.$row['CatId'].'" class="catnode">'
.'<div class="divcat">'
.'<div style="float:left">'
.'<img src="./images/elbow-end-plus-nl.gif" style="cursor:pointer" onclick="defineImageNode(this);seekUl(document.getElementById(\'idtree_'.$row['CatId'].'\'));callProductsFromCat('.$row['CatId'].');" />'
.'</div>'
.'<div style="white-space: nowrap;padding-left:3px" id="catname_'.$row['CatId'].'">'
.$row['CatName'].$row['CatId']
.'</div>'
.'<div style="clear:both"></div>'
.'</div>'
.'<ul style="display:none;" id="ulProd_'.$row['CatId'].'"></ul>';
$arrStr[]=$this->recursiveTree($row['CatId']);
$arrStr[]='</li>';
}
$arrStr[]='</ul>';
}
return implode('',$arrStr);
}
Ok, I hope someone could help me
Thanks and sorry about my english.