Need help with multidimensional Arrays
Posted: Sun Oct 26, 2008 2:56 pm
Hi!
Here's the thing; I have a MySQL table that contains the catagories and subcategories indexes for products. Example:
Product Category_id Subcategory_id
1 3 11
1 2 5
2 1 2
To create a selectable checklist, where a list of all the categories and subcategories is listed, I've coded:
$select = "SELECT * FROM list_categories ORDER BY category";
$query = mysql_query($select);
while ($categories = mysql_fetch_array($query))
{
$category_name = $categories["category"];
$category_id = $categories["category_id"];
$select2 = "SELECT * FROM list_subcategories WHERE (category_id = '$category_id') ORDER BY subcategory";
$query2 = mysql_query($select2);
echo '<li>'.$category_name;
echo '<ul>';
while ($subcategories = mysql_fetch_array($query2))
{
$subcategory_name = $subcategories["subcategory"];
$subcategory_id = $subcategories["subcategory_id"];
echo '<li>';
echo '<input type="checkbox" name="subcategory['.$category_id.'][]" value="'.$subcategory_id.'"';
echo ((count($subcategory) <> 0) and (in_array($subcategory_id,$subcategory))) ? 'checked' : '';
echo '/>';
echo $subcategory_name;
echo '</li>';
} //ENDS while ($subcategories = mysql_fetch_array($query2))
echo '</ul>';
echo '</li>';
} //ENDS while ($record = mysql_fetch_array($query))
When I select the categories/subcategories from the checklist then this info is stored on a table:
$categories_list = $_POST["subcategory"];
foreach ($categories_list as $category => $subcategories)
{
foreach ($subcategories as $subcategory)
{
$insert = "INSERT INTO selected_products
(product_id, category_id, subcategory_id)
VALUES ('$product_id', '$category', '$subcategory')";
mysql_query($insert);
} //ENDS foreach ($subcategories as $subcategory)
} //ENDS foreach ($categories_list as $category => $subcategories)
This all works fine and I'm able to store the selections. Now, what I need to do and so far I haven't been able to is to get the info from the table and pre-select the checkboxes that I have chosen before and kept record on the table.
Any ideas?
Thanks!
Cheers.
Here's the thing; I have a MySQL table that contains the catagories and subcategories indexes for products. Example:
Product Category_id Subcategory_id
1 3 11
1 2 5
2 1 2
To create a selectable checklist, where a list of all the categories and subcategories is listed, I've coded:
$select = "SELECT * FROM list_categories ORDER BY category";
$query = mysql_query($select);
while ($categories = mysql_fetch_array($query))
{
$category_name = $categories["category"];
$category_id = $categories["category_id"];
$select2 = "SELECT * FROM list_subcategories WHERE (category_id = '$category_id') ORDER BY subcategory";
$query2 = mysql_query($select2);
echo '<li>'.$category_name;
echo '<ul>';
while ($subcategories = mysql_fetch_array($query2))
{
$subcategory_name = $subcategories["subcategory"];
$subcategory_id = $subcategories["subcategory_id"];
echo '<li>';
echo '<input type="checkbox" name="subcategory['.$category_id.'][]" value="'.$subcategory_id.'"';
echo ((count($subcategory) <> 0) and (in_array($subcategory_id,$subcategory))) ? 'checked' : '';
echo '/>';
echo $subcategory_name;
echo '</li>';
} //ENDS while ($subcategories = mysql_fetch_array($query2))
echo '</ul>';
echo '</li>';
} //ENDS while ($record = mysql_fetch_array($query))
When I select the categories/subcategories from the checklist then this info is stored on a table:
$categories_list = $_POST["subcategory"];
foreach ($categories_list as $category => $subcategories)
{
foreach ($subcategories as $subcategory)
{
$insert = "INSERT INTO selected_products
(product_id, category_id, subcategory_id)
VALUES ('$product_id', '$category', '$subcategory')";
mysql_query($insert);
} //ENDS foreach ($subcategories as $subcategory)
} //ENDS foreach ($categories_list as $category => $subcategories)
This all works fine and I'm able to store the selections. Now, what I need to do and so far I haven't been able to is to get the info from the table and pre-select the checkboxes that I have chosen before and kept record on the table.
Any ideas?
Thanks!
Cheers.