Results 1 to 3 of 3

Thread: Help Needed: Multi SELECT item & Arrays

  1. #1
    Join Date
    Nov 2008
    Location
    Atlanta, GA
    Posts
    15
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Help Needed: Multi SELECT item & Arrays

    I'm trying to take the input from a <SELECT> Form item that has the MULTIPLE option turned on.

    Once I can do this properly - I plan to populate that list with databse values - but I need to be able to process the users selections in the resulting page call.

    Code:
    print("<select name=subcategory[] multiple=multiple >");
    I have a simple list of items and I want the user to be able to select more than one item.

    I created my FORM variable as an array - so that the $_POST would process this as an array. I know the $_POST item can return an array - but how do you access that information on the other side?

    All examples that I seen really do not address how to move the FORM variable back into a PHP array that can be used.

    Basic FORM item:

    PHP Code:
    <?php
    [SIZE="2"]
    print(
    "<form action=./chk_value.php method=post name=newitem>");
       print(
    "<fieldset>");
        print(
    "<legend class=formTitle> Select A Value </legend>");
        print(
    "<P class=ptight>");
       print(
    "<label for=subcategory>Product Subcategory</label>");
    print(
    "<select name=subcategory[] multiple=multiple >");
    print(
    "<option value=1>Apple</option>");
    print(
    "<option value=2>Apricots</option>");
    print(
    "<option value=3>Bananna</option>");
    print(
    "<option value=4>Blueberry</option>");
    print(
    "<option value=5>Cherry</option>");
    print(
    "<option value=6>Cranberry</option>");
    print(
    "<option value=7>Figs</option>");
    print(
    "<option value=8>Grapes</option>");
    print(
    "<option value=9>Oranges</option>");
    print(
    "<option value=10>Strawberry</option>");
    print(
    "</select>");               
     print(
    "</P>");

        print(
    "<P>");
    print(
    "<input name=add_button type=submit value=Save class=formText>");
        print(
    "</P>");
       print(
    "</fieldset>");
     print(
    "</form>");
    [/
    SIZE]
    ?>

    At this point - I'm just trying to see the that the values are in my PHP variable in the page that process it - chk_value.php

    Here's all the code in the <BODY> tag (pretty simple at this point):
    PHP Code:
    <?php
       $subcategory_value 
    = array();

       
    # Get need key value SUBCATEGORY ID 
       
    print("The size of the Subcategory array is -> "sizeof($_POST['subcategory']) . "<BR>" );
      
       for (
    $i=0;$i<=count($_POST['subcategory']);$i++){  
        
    $subcategory_value[]  = $_POST['subcategory'];           
       }
       
       
       foreach (
    $subcategory_value as $key => $value
          {   echo 
    $key." - " $value "<BR>";              
          }


    ?>

    I suspect that this code

    Code:
     $subcategory_value[]  = $_POST['subcategory'];
    in the FOR LOOP is causing the problem.

    So when "chk_value.php" executes - How do I properly move the FORM variable captured into a PHP array variable that I can use????
    Last edited by krisjohnson; 08-28-2009 at 02:36 AM. Reason: to make more clear

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    In my current painkiller induced haze this is my suggestion.

    The [] in the SELECT create an array that is passed as an array to PHP.

    Try this
    PHP Code:
    $subcategory_value=$_POST['subcategory'];

       for (
    $i=0;$i<=count($subcategory_value);$i++){   
           echo 
    $i." - " $subcategory_value[i] . "<BR>";                         
       } 

    I haven't tested it but I think it's on the right track, perhaps somebody with a clearer head might be able to help further
    Last edited by forum_amnesiac; 08-29-2009 at 08:16 AM.

  3. #3
    Join Date
    Nov 2008
    Location
    Atlanta, GA
    Posts
    15
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Fourm_Amnesiac:

    I actually came up with a solution too.

    This would be for the page that process the multi-select field:

    PHP Code:

       $subcategory_value 
    = array();

       
    # Store values from multi-select field
        
    if (is_array($_POST) && (count($_POST) > 1) ){
         
    #
           
    foreach ($_POST['subcategory'] as $key => $value
              {   

                
    $subcategory_value[] = $value;
                echo 
    $key." - " $value "<BR>";              
              }
        

       
    }
       
         
    # Display values of assigned array
          
    foreach($subcategory_value as $key => $value) {
          echo 
    $key." - " $value "<BR>";              
                  } 
    This was a code sample enroute to a bigger exercise.

    I used this sample to help with a page that has a multi-select field.
    The data in the field is generated from a database table.

    I then used the selected values to perform multiple inserts in the processing page.

    Thanks for taking time to answer my question

    krj

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •