Need help with multidimensional Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rfeio
Forum Newbie
Posts: 15
Joined: Fri Aug 22, 2008 4:23 am

Need help with multidimensional Arrays

Post by rfeio »

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.
rfeio
Forum Newbie
Posts: 15
Joined: Fri Aug 22, 2008 4:23 am

Re: Need help with multidimensional Arrays

Post by rfeio »

Ok, I've found the solution for the problem.

The idea was to be able to load from table the subcategories that had been selected and mark them as checked on the checklist.

All the code was already in place (see previous messages) and what was missing was to loadm from table and feed the $subcategory array that is used in the checklist.

Here's the missing code:

Code: Select all

 
    $select = "SELECT subcategory_id FROM selected_products WHERE (merchant_id = '$merchant_id') ORDER BY subcategory_id";
    $query  = mysql_query($select);
    
    $subcategory = array();
    $i = 0;   //Resets counter
    
    while ($record = mysql_fetch_array($query))
    {
      $subcategory_id = $record["subcategory_id"];
      
      $subcategory[$i] = $subcategory_id;    //Loads record into array $subcategory
      
      $i++;  //increments counter
 
    }  //ENDS while ($record = mysql_fetch_array($query))
 
Post Reply