glen-rogers Posted March 28, 2013 Share Posted March 28, 2013 Hi I need some help! I have some code that displays a menu. The menu has categories of products, some of these categories have subcategories. When a category is clicked, the class is sent to some jquery/ajax and when a subcategory is clicked the class is also sent to the jquery/ajax. This is working fine. The problem I have is that if I click any subcategory in a category only the data for one subcat is shown for all the subcat. This is the code <?php include 'connect.php'; $q = "SELECT c.category, c.cat_id, s.subcat_id, GROUP_CONCAT(s.subcategory) AS sublist FROM categories AS c LEFT JOIN subcategories AS s ON c.cat_id = s.cat_id GROUP BY c.cat_id ORDER BY c.cat_id "; $r = mysql_query($q) or die( 'Could not execute query: ' . mysql_error() ); $output = '<ul id="nav">'; while($data = mysql_fetch_array($r)){ $output .= "<li><a href={$data['cat_id']} class='category'>{$data['category']}</a>"; if(!empty($data['sublist'])){ $subcats = explode(",", $data['sublist']); $output .="<ul>"; foreach($subcats as $s){ $output .= "<li><a href={$data['subcat_id']} class='subcategory'>$s</a></li>"; } $output .= "</ul>"; } $output .="</li>"; } $output .= '</ul>'; echo $output; Can anyone see what the problem is? Thanks for looking. Glen Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/ Share on other sites More sharing options...
jazzman1 Posted March 28, 2013 Share Posted March 28, 2013 Change the statement: SELECT c.category, c.cat_id, s.subcat_id, GROUP_CONCAT(s.subcategory) AS sublist FROM categories AS c LEFT JOIN subcategories AS s ON c.cat_id = s.cat_id WHERE c.cat_id = $_GET['cat_id'] GROUP BY c.cat_id ORDER BY c.cat_id Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421626 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 Thanks. The page gives me this error now: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\acraftyaffair\inc\productmenu.php on line 5 on this line WHERE c.cat_id = $_GET['cat_id'] Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421628 Share on other sites More sharing options...
jazzman1 Posted March 28, 2013 Share Posted March 28, 2013 My mistake, the $_GET valiable must be $_GET['subcat_id'] coming from $data['subcat_id'], right? Try this, $q = "SELECT c.category, c.cat_id, s.subcat_id, GROUP_CONCAT(s.subcategory) AS sublist FROM categories AS c LEFT JOIN subcategories AS s ON c.cat_id = s.cat_id"; if(isset($_GET['subcat_id'])) $q .= " WHERE s.cat_id=".$_GET['subcat_id']; $q .=" GROUP BY c.cat_id ORDER BY c.cat_id"; Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421631 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 Its still doing the same! showing the same data for every subcategory. Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421632 Share on other sites More sharing options...
jazzman1 Posted March 28, 2013 Share Posted March 28, 2013 (edited) Well, let us know how you designed the tables in the database. And......tell us what value you've got from $data['subcat_id'] too! Edited March 28, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421633 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 Ok, My tables are set up like this categories:cat_id - int(10) aicategory - varchar(100) subcategories:subcat_id - int(10) aisubcategory - varchar(100)cat_id - int(10) products:id - int(10) aiproduct_name - varchar(100)product_description - textproduct_price - decimal(10, 2)product_image - varchar(100)cat_id - int(10) null(yes)subcat_id - int(10) null(yes) When I click on a category it displays all the products with that categories cat_id, as it should. When I click on to a any subcategory belonging to a given category it displays only one subcategories products, for every subcategory in that group. So for example, if i have a category 'animals' with subcategories 'dogs' 'cats' 'rabbits' and click on any one of them it will display whats is in 'cats' every time! Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421647 Share on other sites More sharing options...
jazzman1 Posted March 28, 2013 Share Posted March 28, 2013 Hm.... you need to normalize your database tables. I recommend you to watch those Also, you would check here to find ready database schemata, otherwise you will have a lot of problems in the future. Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421657 Share on other sites More sharing options...
jazzman1 Posted March 28, 2013 Share Posted March 28, 2013 If you run this query inside phpMyAdmin for example what result do you get: Assuming that a category with id: 1 has many subcategories. SELECT c.category, c.cat_id, s.subcat_id, GROUP_CONCAT(s.subcategory) AS sublist FROM categories AS c LEFT JOIN subcategories AS s ON c.cat_id = s.cat_id WHERE s.cat_id = 1 GROUP BY c.cat_id ORDER BY c.cat_id Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421670 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 Wow, it all seems a bit above me! Do I not need all my fields though? In categories cat_id is the id of the category. In subcategories subcat_id is the id of the subcategory and cat_id is the same as cat_id in categories, so we know that subcategory belong to that category. In product subcat_id is the same as subcat_id in subcategories so we can tll the subcategory that product belongs to (if any). And cat_id is the same as cat_id in categories so we can tell what category the product belongs to. Have I got this all wrong? Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421671 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 If you run this query inside phpMyAdmin for example what result do you get: Assuming that a category with id: 1 has many subcategories. SELECT c.category, c.cat_id, s.subcat_id, GROUP_CONCAT(s.subcategory) AS sublist FROM categories AS c LEFT JOIN subcategories AS s ON c.cat_id = s.cat_id WHERE s.cat_id = 1 GROUP BY c.cat_id ORDER BY c.cat_id Where am I meant to be getting a result? This has come up SELECT c.category, c.cat_id, s.subcat_id, GROUP_CONCAT( s.subcategory ) AS sublist FROM categories AS c LEFT JOIN subcategories AS s ON c.cat_id = s.cat_id WHERE s.cat_id =1 GROUP BY c.cat_id ORDER BY c.cat_id LIMIT 0 , 30 Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421675 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 (edited) Sorry When I ran this SELECT c.category, c.cat_id, s.subcat_id, GROUP_CONCAT( s.subcategory ) AS sublist FROM categories AS c LEFT JOIN subcategories AS s ON c.cat_id = s.cat_id WHERE c.cat_id =8 GROUP BY c.cat_id ORDER BY c.cat_id LIMIT 0 , 30 8 is the cat_id of Ribbon which has subcat 1 inch, 7/8 inch, 5/8 inch, 3/8 inch I got this category cat_id subcat_id sublist Ribbon 8 1 1 inch,7/8 inch,5/8 inch,3/8 inch Edited March 28, 2013 by glen-rogers Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421677 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 It seems I'm only gett one subcat_id, even though there are four subcats! Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421680 Share on other sites More sharing options...
Barand Posted March 28, 2013 Share Posted March 28, 2013 (edited) You need to group_concat the sub_ids with their descriptions SELECT c.category, c.cat_id, GROUP_CONCAT(s.subcat_id,'|', s.subcategory ) AS sublist ... Edited March 28, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421688 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 (edited) Hey thanks for that. But now when i run that query in phpmyadmi it give me this category cat_id subcat_id sublist Ribbon 8 1 1|1 inch,2|7/8 inch,3|5/8 inch,4|3/8 inch Edited March 28, 2013 by glen-rogers Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421693 Share on other sites More sharing options...
Barand Posted March 28, 2013 Share Posted March 28, 2013 You don't select the subcat_id with the cat_id, you are now selecting it with each subcategory explode the sublist to get subcat_id|subcategory pairs. explode those pairs to get the id and subcat for your links Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421696 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 This is all getting a bit confusing for me, as you can tell I'm a novice! I explode the sublist on this line $subcats = explode(",", $data['sublist']); How do I explode the pairs? Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421698 Share on other sites More sharing options...
Barand Posted March 28, 2013 Share Posted March 28, 2013 foreach ($subcats as $pair) { list($subcat_id, $subcategory) = explode('|', $pair); // output link } Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421699 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 That works! Thank you, I appreciate it. Just one more thing though. This- SELECT c.category, c.cat_id, GROUP_CONCAT(s.subcat_id,'|', s.subcategory ) AS sublist ... Put subcat_id in sublist with subcategory. So now my subcategory button go have subcat_id along with the subcategory text, like '1|dogs' '2|cats'..... Is there any way not to show the subcat_id in the button? Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421701 Share on other sites More sharing options...
Solution Barand Posted March 28, 2013 Solution Share Posted March 28, 2013 (edited) The obvious way would be to explode them before creating the button Edited March 28, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421702 Share on other sites More sharing options...
glen-rogers Posted March 28, 2013 Author Share Posted March 28, 2013 Ah. What I was doing was this $output .= "<li><a href=$subcat_id class='subcategory'>$pair</a></li>"; When it should have been this $output .= "<li><a href=$subcat_id class='subcategory'>$subcategory</a></li>"; All sorted now! Thank you Glen Quote Link to comment https://forums.phpfreaks.com/topic/276269-only-getting-one-subcat_id-and-apply-to-all-subcategories/#findComment-1421704 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.