Need help with multidimensional Arrays

Discussion in 'PHP' started by rfeio, Oct 26, 2008.

  1. #1
    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))
    
    Code (markup):

    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)
    
    Code (markup):

    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, Oct 26, 2008 IP
  2. Limotek

    Limotek Peon

    Messages:
    165
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You'll need to edit the line

    echo    '<input type="checkbox" name="subcategory['.$category_id.'][]" value="'.$subcategory_id.'"';
    PHP:
    Effectively, what you want to do is check the database to see whether each checkbox should or shouldn't be checked. If it should, use the checked="checked" parameter.

    e.g.

    if($checked) {
    echo    '<input type="checkbox" name="subcategory['.$category_id.'][]" value="'.$subcategory_id.'" checked="checked"';
    } else {
    echo    '<input type="checkbox" name="subcategory['.$category_id.'][]" value="'.$subcategory_id.'"';
    }
    PHP:
    In order to find whether a checkbox should be checked, I would search MySQL using something like:

    SELECT COUNT(*) FROM list_subcategories WHERE (category_id = '$category_id') AND (subcategory_id = '$subcategory_id');

    and then display it as checked if the resultant value is greater than 0.
     
    Limotek, Oct 27, 2008 IP
  3. rfeio

    rfeio Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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))
    
    Code (markup):
     
    rfeio, Oct 28, 2008 IP