Jump to content

loop inside loop inside loop inside.... :-(


Ezio

Recommended Posts

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

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

@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

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

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

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

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

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 by Ezio
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...