dubc07 Posted May 21, 2009 Share Posted May 21, 2009 I currently have 2 tables on my database 1service and 2subs I have been trying to use a while loop function to pull the category's and the subcategory's But when it pulls them it echo like this cat1 sub1 cat1 sub2 cat1 sub3 and so forth..... I'm just wondering of it is possible to echo each category once and then list each subcat that associated with the main category. Like this cat1 sub1 sub2 sub3 cat2 sub1 sub2 sub3 Any help is appreciated... Thanks This is what i have so far but it doesn't work. it will echo Services1 - sub1 Services2 - sub1 Services1 - sub2 Services2 -sub2 Services1 - sub1 Services2 - sub1 <?php $query = "SELECT service, subserv "."FROM services, subserv "; $result = mysql_query($query) or die(mysql_error()); // Print out the contents of each row into a table while($row = mysql_fetch_array($result)){ echo $row['service']. " - ". $row['subserv']; echo "<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/159162-solved-categorys-and-subcategorys/ Share on other sites More sharing options...
Michdd Posted May 21, 2009 Share Posted May 21, 2009 You could have a reference to the subcategories of a category within the table. For example category ID. Say you have Category with the ID of 1. Then within the sub- table you could have all sub categories which link to that main category have a field named category ID (or whatever). Then when outputting it create 1 query to loop through the main categories table, and within that create a new query that will get the sub category. $result = mysql_query('SELECT * FROM catageories'); while($row = mysql_fetch_array($result)) { //Echo out each main $sub = mysql_query('SELECT * FROM subcategories WHERE categoryID="' . $row['categoryID'] . '"'); while($subrow = mysql_fetch_array($sub)) { //Echo out each sub. } } Link to comment https://forums.phpfreaks.com/topic/159162-solved-categorys-and-subcategorys/#findComment-839393 Share on other sites More sharing options...
dubc07 Posted May 21, 2009 Author Share Posted May 21, 2009 That Solved it for now THANKS ALOT!!!! Link to comment https://forums.phpfreaks.com/topic/159162-solved-categorys-and-subcategorys/#findComment-839414 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.