Jump to content

dhimo

Members
  • Posts

    38
  • Joined

  • Last visited

dhimo's Achievements

Newbie

Newbie (1/7)

0

Reputation

  1. dhimo

    This tree menu

    Hello, Can someone help me with this recursive function. As the function is right now it shows all parents and children. What I want to do is to expend parent to show children and when i click on a child then the menu does not collapse but remains. any ideas? thanks in advance function menu($parent = 0, $level = 0){ // where parent of 0 means it's top-level $retval = ""; $sql = mysql_query("SELECT * FROM " . TABLE_CATEGORIES . " WHERE parent='$parent' ORDER BY sort ASC"); $retval .= "<div id=\"verticalMenu\">\n"; $retval .= "<ul>\n"; while ($row = dbFetchAssoc($sql)) { extract($row); $retval .= "<li><a href=\"".friendlyURL($category_id)."\">" .$category."</a></li>\n"; $retval .= menu($category_id, intval($level) + 1); } $retval .= "</ul>\n"; $retval .= "</div>\n"; return $retval;}
  2. dhimo

    Help with php mysql

    Thanks a lot man. I think everything works fine now. Thanks again
  3. dhimo

    Help with php mysql

    I tested and it works very well. I was wondering though how to make this with only categories and subcategories. Like we still have not clicked to get a product idWhen we are still on a subcategory we getParent > sub1 > sub2and when we click on a product we getParent > sub1 > sub2 > productand take away the "No products fund"If u know what i mean
  4. dhimo

    Help with php mysql

    thanks man, I will test it and i am sure it will work fine hehe
  5. dhimo

    Help with php mysql

    Thank you, I think the above function does the trick. It was exactly what I was looking for. Thanks again. I have another question though. It is related to this topic.How can I create a navigation menu like the one in this forum:W3Schools Forum > Server Scripting > PHP > Help with php mysqlbut in my case are the categories, subs and productsMain category > subcategory1 > subcategory3 > My productI am still using same db tables and fieldsCan u give me a good function for this, I would much appreciated. Thanks again
  6. dhimo

    Help with php mysql

    Hello again. I came up with this solution. But it doesnt loop to the end of the tree. It loops only one level down. Any idea how to make it better? thanks <?php$catID = $_GET['catID']; $result = mysql_query("SELECT * FROM categories WHERE parent_id = '$catID'") or die('MySQL Error: ' . mysql_error()); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { $cats[] = $row["category_id"]; } foreach($cats as $key => $value) { $result = mysql_query("SELECT * FROM products WHERE category_id = '$value'") or die('MySQL Error: ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { echo $row["product_name"] . "<br>"; } } } else { $result = mysql_query("SELECT * FROM products WHERE category_id = '$catID'") or die('MySQL Error: ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { echo $row["product_name"] . "<br>"; } } ?>
  7. dhimo

    Help with php mysql

    Thanks for your answer. But maybe I wasnt clear enough in my question.I have 2 tables. 1) categories: which holds the recursive menu (category_id, category_name, parent_id)2) products: which holds the products and the category_id that shows to which category it belongs to (product_id, category_id, product_name)Now I have the menu (categories) and the productsLets say the menu has 3 top categories (Mobiles, Tvs, Cameras)Each of these categories has sub categoriesMobiles --> Nokia, Ericsson, MotorolaTvs --> Philips, Sony, ToshibaCameras --> Cannon, OlympusEach of sub categories contain their Top categories which contain subcategories cannot contain products, so Mobiles, Tvs, Cameras cannot have their id on products table.I use $_GET to retrieve the products.What I want now is:When I get querystring for Mobiles, I want to list all the products which reside in all its sub categories, so I want to list all products in Nokia, Ericsson, Motorola.When I get querystring for Nokia, I want to list all the products which reside only in Nokia tableand so onHope I explained myself well, Thanks again
  8. dhimo

    Help with php mysql

    Hello everyone.I have a mysql table named categories which has these fields: table name: categoriesFields in table categories: category_id, category_name, parent_id The above tables builds a tree menu with sub categories, parent, child, like a recursive function, which will be placed as menu.Now I have another table named products which has these fields: table name: productsFields in products: product_id, category_id, product_name The category which holds sub categories cannot hold products and vice versa:Now lets say that tree looks like this MOBILES - Nokia ------ N70 ------ B80 ------ etc - Motorola ------ Model1 ------ Model2 ------ etc - Samsung ------ Model1 ------ Model2 ------ etcTV I use $_GET to run the sql query to list products under appropriate category. So when i click on Nokia and lets say ?catID=20 and I list all products that have category_id = 20What I am trying to accomplish is that when I click on MOBILES lets sat ?catID=5 then i list all pruducts under Nokia, Motorola, Samsung which are child categories of MOBILESSo, anyone one knows a good function and sql query to make this work?Thanks in advance. Any answer is appriciated
  9. This looks fine. But how can I list the Children in the tree menu with the rest of the top menu under the parent id
  10. Hello there. I have this recursive function which lists all categories and subcategories from the database. I have three fields.category_id, category_name and parent_id. Top category holds parent_Id = 0 while subcategories get the id from their parent. Here s the functions. Justsomeguy helped me with this and he probably will again <?phpfunction listCategory($parent = 0, $level = 0){ // where parent of 0 means it's top-level $retval = ""; $indent = "<br />"; for ($i=0; $i<$level; $i++) $indent .= " "; $result = mysql_query("SELECT * FROM categories WHERE parent_id='$parent'"); while ($row = mysql_fetch_assoc($result)) { extract($row); $retval .= $indent . "<a href=\"?c=$category_id\">" . $category_name . "</a>"; $retval .= listCategory($category_id, intval($level) + 1); } return $retval;}?> As the function is it lists all categories and subcategories. I want to list only top categories which have parent_id = 0 and I want to get appropriate children after getting the querystring as u can see above in the code.I want to mention to justsomeguy that his help is very appriciated and the reason i am posting a new thread is that is faster to get a response then paging to the prieviews threads which are far behind
  11. It would be very nice to have the menu expanded to get to the child menus. Some javascript would be great. I can fix that myself. So how can i only show main parents and then go deeper show sub parents and then children? Would be nice if only children are linked to the show page. If u could help me to put children in divs that would be great function show_categories($parent = 0, $depth = 0){ $retval = "<br />"; $padding = $depth * 10; $result = mysql_query("SELECT * FROM tbl_category WHERE cat_parent_id='$parent'"); while ($row = mysql_fetch_assoc($result)) { extract($row); $retval .= "<div style=\"padding-left: {$padding}px;\"><a href=\"?$cat_id\">".$cat_name."</a></div>"; $retval .= show_categories($cat_id, $depth + 1); } return $retval;}
  12. Hi justsomeguy. First of all thanks for your help. I see that you give a lot of help in this forum. If u remember u helped with the code below. You think you can give me some more tips how to use html table, divs or <li> to make the menu look nicer and to show the corresponding child menu when i click on the parent. Newbie u know. Thanks in advance function show_categories($parent = 0, $depth = 0){ $retval = "<br />"; $indentval = " "; $result = mysql_query("SELECT * FROM tbl_category WHERE cat_parent_id='$parent'"); while ($row = mysql_fetch_assoc($result)) { extract($row); for ($i = 0; $i < $depth; $i++) $retval .= $indentval; $retval .= "<a href=\"?$cat_id\">".$cat_name."</a>\n"; $retval .= show_categories($cat_id, $depth + 1); } return $retval;}
  13. Oh, I see that you have answered in the PHP section. Didnt see it until now. I am gonne work on that and see how far I goThanks
  14. Hello everyone. Anyone have any idea on how to list all the records from this mysql table.As you can see it has main categories and subcategories, wich are identified by parent id and how deep they go.I am using php mysql.So how can i list them where each child is listed under parent id. like a tree menu rightAny ideas anyone? Thanks in advancetbl categories---------------------------------------------------------------------| id | cat_name | parent_id | depth |---------------------------------------------------------------------| 1 | Mobiles | 0 | 0 |---------------------------------------------------------------------| 2 | Computers | 0 | 0 |---------------------------------------------------------------------| 3 | Nokia | 1 | 1 |---------------------------------------------------------------------| 4 | Motorola | 1 | 1 |-----------------------------------------------------------------------| 5 | N70 | 3 | 2 |----------------------------------------------------------------------| 6 | KRZR | 4 | 2 |----------------------------------------------------------------------| 7 | Acer | 2 | 1 |------------------------------------------------------------------------| 8 | IBM | 2 | 1 |------------------------------------------------------------------------| 9 | TravelMate | 7 | 2 |-----------------------------------------------------------------------| 10 | ThinkPad | 8 | 2 |------------------------------------------------------------------------
  15. Hello everyone. Anyone have any idea on how to list all the records from this mysql table.As you can see it has main categories and subcategories, wich are identified by parent id and how deep they go.I am using php mysql.So how can i list them where each child is listed under parent id. like a tree menu rightAny ideas anyone? Thanks in advancetbl categories---------------------------------------------------------------------| id | cat_name | parent_id | depth |---------------------------------------------------------------------| 1 | Mobiles | 0 | 0 |---------------------------------------------------------------------| 2 | Computers | 0 | 0 |---------------------------------------------------------------------| 3 | Nokia | 1 | 1 |---------------------------------------------------------------------| 4 | Motorola | 1 | 1 |-----------------------------------------------------------------------| 5 | N70 | 3 | 2 |----------------------------------------------------------------------| 6 | KRZR | 4 | 2 |----------------------------------------------------------------------| 7 | Acer | 2 | 1 |------------------------------------------------------------------------| 8 | IBM | 2 | 1 |------------------------------------------------------------------------| 9 | TravelMate | 7 | 2 |-----------------------------------------------------------------------| 10 | ThinkPad | 8 | 2 |------------------------------------------------------------------------
×
×
  • Create New...