Ezio Posted September 27, 2013 Share Posted September 27, 2013 i have 3 tables in mysql categories, subcategories, values. now i want to get the values from categories table then count the number of values returned if it is greater than 0 then i fetch the category id from the categories table using while loop, next i check if the subcategory table has any data with the category id that i got from previous table if number of values is greater than 0 then i fetch the subcategory id from the subcategories table using while loop, now again i check the values table, if it has any data with subcategory id, if it is greater than 0 then i fetch all the values... my code looks something like this... $category_query = mysql_query("SELECT * FROM categories"); $num_category = mysql_num_rows($category_query); if($num_category > 0){ while ($row = mysql_fetch_object($category_query)) { $category_id[] = $row->category_id; $category_name[] = $row->category; $sub_query = mysql_query("SELECT * FROM subcategories WHERE category_id ='".$row->category_id."';"); $num_subcategory = mysql_num_rows($sub_query); if($num_subcategory > 0){ while ($sub_row = mysql_fetch_object($sub_query) { $subcategory_id[] = $sub_row->subcategory_id; $subcategory_name[] = $sub_row->subcategory; } for ($j=0; $j < $num_subcategory; $j++) { $subcategory_data[] = array("subcategory id"=> $subcategory_id[$j]); } } } for($i=0; $i<$num_category; $i++){ $category_data[] = array("category id"=>$category_id[$i], "category name"=>$category_name[$i],); } i want to get the subcategory_data[] into category_data[]...........i cannot figure this out.. :-( Link to comment Share on other sites More sharing options...
Ingolme Posted September 27, 2013 Share Posted September 27, 2013 Where in $category_data[] do you want the subcategories? Is it a property of category_data? To start off, initialize all your arrays before filling them up: $category_data = array(); You can avoid a lot of the extra loops like this: $category_data = array();while ($row = mysql_fetch_object($category_query)) { $category_data[] = array( "category id" => $row->category_id, "category name" => $row->category; ); // // Do the subcategory things here //} Link to comment Share on other sites More sharing options...
justsomeguy Posted September 27, 2013 Share Posted September 27, 2013 If you can nest categories to any depth then you'll need to use a recursive function to do that instead of one loop structure per depth level. Link to comment Share on other sites More sharing options...
Ezio Posted September 30, 2013 Author Share Posted September 30, 2013 @inglome every category has a sub category, i want the category id and then run the select query to find all the subcategories inside each categories and then save it to an array. Link to comment Share on other sites More sharing options...
divinedesigns1 Posted September 30, 2013 Share Posted September 30, 2013 @inglome every category has a sub category, i want the category id and then run the select query to find all the subcategories inside each categories and then save it to an array. then you would need a column in subcategories to have the category id, so you can get the query to find all the subcategory for that id Link to comment Share on other sites More sharing options...
Ezio Posted September 30, 2013 Author Share Posted September 30, 2013 then you would need a column in subcategories to have the category id, so you can get the query to find all the subcategory for that id yes i have that. where from i am SELECTING the values from sub-categories Link to comment Share on other sites More sharing options...
divinedesigns1 Posted September 30, 2013 Share Posted September 30, 2013 yes i have that. where from i am SELECTING the values from sub-categories do what inglome say to do and once you get the category id, your sub-category query should say something like "select * from sub-categories where X=category_id" etc Link to comment Share on other sites More sharing options...
Ezio Posted September 30, 2013 Author Share Posted September 30, 2013 I did that something like this: $category_data = array();while ($row = mysql_fetch_object($category_query)) { $category_data[] = array( "category id" => $row->category_id, "category name" => $row->category; ); $sub_query = mysql_query("SELECT * FROM subcategories WHERE category_id ='".$row->category_id."';"); $num_subcategory = mysql_num_rows($sub_query); if($num_subcategory > 0){ while ($sub_row = mysql_fetch_object($sub_query) { $subcategory_id[] = $sub_row->subcategory_id; $subcategory_name[] = $sub_row->subcategory; } } then i have to take it into $category_data[ ] array..that is where i get the error.. Link to comment Share on other sites More sharing options...
divinedesigns1 Posted September 30, 2013 Share Posted September 30, 2013 I did that something like this: $category_data = array();while ($row = mysql_fetch_object($category_query)) { $category_data[] = array( "category id" => $row->category_id, "category name" => $row->category; ); $sub_query = mysql_query("SELECT * FROM subcategories WHERE category_id ='".$row->category_id."';"); $num_subcategory = mysql_num_rows($sub_query); if($num_subcategory > 0){ while ($sub_row = mysql_fetch_object($sub_query) { $subcategory_id[] = $sub_row->subcategory_id; $subcategory_name[] = $sub_row->subcategory; } } then i have to take it into $category_data[ ] array..that is where i get the error.. what does the error say? and you might want to remove those spaces from category id and name also remove the [] from them Link to comment Share on other sites More sharing options...
justsomeguy Posted September 30, 2013 Share Posted September 30, 2013 Don't immediately push the new array onto the other array. Create it as a variable first: $cat = array("category id" => $row->category_id,"category name" => $row->category); Then add your subcategory data to the $cat array, and after it's done then push $cat onto $category_data. You can also refer to the last element as $category_data[count($category_data) - 1]. Link to comment Share on other sites More sharing options...
Ezio Posted October 1, 2013 Author Share Posted October 1, 2013 (edited) what does the error say? and you might want to remove those spaces from category id and name also remove the [] from them it does not shows an error, it just shows a blank page when i print_r the array...so there must be some error in my code... & i have removed the errors that you said still with no luck :-( Edited October 1, 2013 by Ezio Link to comment Share on other sites More sharing options...
Ezio Posted October 1, 2013 Author Share Posted October 1, 2013 Don't immediately push the new array onto the other array. Create it as a variable first: $cat = array("category id" => $row->category_id,"category name" => $row->category); Then add your subcategory data to the $cat array, and after it's done then push $cat onto $category_data. You can also refer to the last element as $category_data[count($category_data) - 1]. will try your way and see if it works :-) Link to comment Share on other sites More sharing options...
divinedesigns1 Posted October 1, 2013 Share Posted October 1, 2013 it does not shows an error, it just shows a blank page when i print_r the array...so there must be some error in my code... & i have removed the errors that you said still with no luck :-( trying display your errors Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now