I have created a tree like structure in Laravel and Mongodb using Jensengers Library using recursion. On the click of the <li cat_id="HGP00"> its child, are shown in a tree like structure. Up to this everything works perfectly fine. Now coming to the problem, there is a text box in which the user enters a keyword, if keyword exists, when the user clicks on the <li cat_id="HGP00">, this keyword is also posted to controller action. Now I have to search this keyword in every level until it searches upto the leaf category. I am having trouble in this because if it doesn't find keyword in any level, the loop breaks, but what I want is if keyword is not found in one level, it should find it in its child, and so on until leaf. If it finds in any leaf, the child of parent category ( on which clicked) should be added to <li>. I have the following database structure describing the parent, child relation. "parent" and the "cat_id" is the relation between these. Another thing is that I have lots of data in database, so it is a bit slow while creating tree, can I follow any other approach to create Recursive category tree.
Level 1
{
"_id": ObjectId("56b0a158083f11e1268b4568"),
"lang": "en",
"cat_id": "HGP00",
"name": "HOLLOW GLASS - Production",
"parent": NumberLong(0),
"type": "cat",
"slug": "hollow-glass-production"
}
Level 2
{
"_id": ObjectId("56b0a159083f11e1268b46a6"),
"lang": "en",
"cat_id": "HGP00-005",
"name": "Other Accessories and Tools",
"parent": "HGP00",
"type": "cat",
"slug": "other-accessories-and-tools"
}
Level 3
{
"_id": ObjectId("56b0a159083f11e1268b472a"),
"lang": "en",
"cat_id": "HGP00-005-007",
"name": "Other Hollow Production Accessories",
"parent": "HGP00-005",
"type": "cat",
"slug": "other-hollow-glass-production-accessories"
}
Level 4
{
"_id": ObjectId("56b0a159083f11e1268b4733"),
"lang": "en",
"cat_id": "HGP00-005-007-030",
"name": "Other Accessories for Hollow Glass Processing",
"parent": "HGP00-005-007",
"type": "cat",
"slug": "other-accessories-for-hollow-glass-processing"
}
Level 5
{
"_id": ObjectId("56b0a159083f11e1268b4733"),
"lang": "en",
"cat_id": "HGP00-005-007-030",
"name": "Other Accessories for Hollow Glass Processing",
"parent": "HGP00-005-007",
"type": "cat",
"slug": "other-accessories-for-hollow-glass-processing"
}
Lets say If I search Glass, if it finds keyword in every level, its works fine but if its not in any level, it breaks. In current scenario, its not in Level 2 and Level 3 but after that its there in all the levels. So I want Level 2 added to <li>. Similarly for others.
Below is my code: On click of <li> following action executes.
public function fetchSubCats() {
// $input['parent'] = 'HGP00';
// $input['term'] = 'Glass';
$input = Request::all();
$term = strtolower(str_replace(' ', '', trim($input['term'])));
if($term==='') {
$cat_list = $this->category_tree($input['parent'], $term='' , FALSE);
}
else {
$cat_list = $this->category_tree($input['parent'], $term , TRUE);
}
$cat_data="";
foreach ($cat_list as $cat) {
$cat_data .= $cat;
}
return Response::json (array(
'error' => false,
'data' => $cat_data
), 200 );
}
//Recursive php function
function category_tree($parent, $term, $flag = FALSE , $user_tree_array = '',$last=0){
if (!is_array($user_tree_array))
$user_tree_array = array();
if($flag === FALSE) {
$sql = Categories::where('lang', '=', 'en')->Where('parent', '=', $parent)->orderBy('name')->project( array('cat_id', 'name') )->get();
if(count($sql)){
$user_tree_array[]= '<ul>';
foreach ($sql as $row) {
$user_tree_array[] = '<li data-cat_id="'.$row->cat_id.'">' . $row->name;
$user_tree_array = $this->category_tree($row->cat_id, $term, $flag, $user_tree_array);
$user_tree_array[] = '</li>';
}
$user_tree_array[] = '</ul>';
}
}
elseif($flag === TRUE) {
$sql = Categories::where('lang', '=', 'en')
->Where('parent', '=', $parent)
->Where('name', 'like', '%'.$term.'%')
->orderBy('name')
->project(array('cat_id', 'name'))
->get();
if(count($sql)){
$user_tree_array[]= '<ul>';
foreach ($sql as $row) {
$user_tree_array[] = '<li data-cat_id="'.$row->cat_id.'">' . $row->name;
$user_tree_array = $this->category_tree($row->cat_id, $term, $flag, $user_tree_array);
$user_tree_array[] = '</li>';
}
$user_tree_array[] = '</ul>';
}
else {
// Do something here, but I am not getting what and how.
}
}
return $user_tree_array;
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire