Jump to content

Need help with multidimensional Arrays


rfeio

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/130188-need-help-with-multidimensional-arrays/
Share on other sites

for ease of reading please use code tags ( the # button above the message box)

 

<?php
$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)
?>

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:

 

    $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))

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.