xox Posted January 19, 2011 Share Posted January 19, 2011 I have two tables subcat and category structure subcat id_subcat|id_of_cat|subcat_name| and for main categories ID_cat|cat_name| now what I want to display them as Main category subcategory1 subcategory2 Main category2 subcategoryX etc... Here's the code I'm using but doesn't work,it doesn't display anything. <?php $query = " SELECT ID_cat, id_subcat, subcat_name, cat_name FROM subcat, category WHERE ID_cat=id_subcat"; $result = mysql_query($sql); $lastCat = null; while($row = mysql_fetch_assoc($result)) { # Check if you need to print the category_name if($lastCat != $row['ID_cat']) { # Not the same category as the last one! # Print it and save the ID $lastCat = $row['ID_cat']; echo "<br />". $row['ID_cat']; } # Print the sub category echo "<br />". $row['subcat_name']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/ Share on other sites More sharing options...
Pikachu2000 Posted January 19, 2011 Share Posted January 19, 2011 Have you tried to run that query in phpMyAdmin to see what it returns? Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162200 Share on other sites More sharing options...
xox Posted January 20, 2011 Author Share Posted January 20, 2011 Well it looks like query is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162387 Share on other sites More sharing options...
xox Posted January 20, 2011 Author Share Posted January 20, 2011 I've changed WHERE ID_cat=id_subcat"; to WHERE ID_cat=category.ID"; and still nothing changes... Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162393 Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2011 Share Posted January 20, 2011 The code you posted won't work because you are using the $query variable to make the query, but putting a non-existent $sql variable into the mysql_query() function. Are you developing and debugging your code on a system with error_reporting set to E_ALL (or a -1) and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects? You would save a TON of time on simple mistakes in your code. Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162395 Share on other sites More sharing options...
xox Posted January 20, 2011 Author Share Posted January 20, 2011 Ok thanks for that tip, I solved the issue with undefined variable $sql (it should be $query) There is still this warning for which I can't find the solution... googleing right now. mysql_fetch_assoc() expects parameter 1 to be resource, boolean given warning is located at while($row = mysql_fetch_assoc($result)) { Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162403 Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2011 Share Posted January 20, 2011 If you do a search for that error message, you will find that it generally means that your query failed due to an error, which is probably the reason Pikachu2000 asked this - Have you tried to run that query in phpMyAdmin to see what it returns? Make sure your query executes correctly in phpMyAdmin first. Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162409 Share on other sites More sharing options...
xox Posted January 20, 2011 Author Share Posted January 20, 2011 After executing query in phpMyAdmin It showed up this error #1052 - Column 'ID' in field list is ambiguous I googled for this error and I've found this It means that ID and/or Title exist in both tables Doesn't mysql/php sepereates from uppercase and downcase ? Because structure of both tables is almost the same Categories |ID||cat_name| Subcategory |id||id_main| subcat_name| Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162413 Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2011 Share Posted January 20, 2011 Column, index, stored routine, and event names are not case sensitive on any platform, nor are column aliases. Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162416 Share on other sites More sharing options...
xox Posted January 20, 2011 Author Share Posted January 20, 2011 Thank you, all errors are gone. The only problem is that now all main categories have the same subcategories Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162425 Share on other sites More sharing options...
xox Posted January 20, 2011 Author Share Posted January 20, 2011 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/225014-categories-and-subcategories-display/#findComment-1162579 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.