Problem with array...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leszek

    Problem with array...

    I have a form with 5 dropdown lists I want to put all selected values to an
    array called $data[] so i wrote sometthing like this:

    for(i=0;i<5;i++ ){
    echo"<select name=\"\$data[]\">";
    echo"<option value=\"1\">1</option>";
    echo"<option value=\"1\">2</option>";
    echo"<option value=\"1\">3</option>";
    echo"</select>";
    }

    So I think this will put all selected values into an array.
    But the problem is that when user submits the form this array goes to $_POST
    array
    And I have no idea how to read a value from an array $data which is into
    array $_POST and use it as a part of "if" statement

    for example
    if (value_from_arr ay==5){
    // instructions
    }

    Thanks for help
    Leszek


  • J.O. Aho

    #2
    Re: Problem with array...

    Leszek wrote:[color=blue]
    > I have a form with 5 dropdown lists I want to put all selected values to an
    > array called $data[] so i wrote sometthing like this:
    >
    > for(i=0;i<5;i++ ){
    > echo"<select name=\"\$data[]\">";
    > echo"<option value=\"1\">1</option>";
    > echo"<option value=\"1\">2</option>";
    > echo"<option value=\"1\">3</option>";
    > echo"</select>";
    > }
    >
    > So I think this will put all selected values into an array.
    > But the problem is that when user submits the form this array goes to $_POST
    > array
    > And I have no idea how to read a value from an array $data which is into
    > array $_POST and use it as a part of "if" statement[/color]


    $value_from_arr ay=$_POST['$data'];
    //for example
    if ($value_from_ar ray[0]==5){
    // instructions
    }

    But would be easier you didn't use the $ in the name

    for(i=0;i<5;i++ ){
    echo"<select name=\"\data[]\">";
    echo"<option value=\"1\">1</option>";
    echo"<option value=\"1\">2</option>";
    echo"<option value=\"1\">3</option>";
    echo"</select>";
    }

    $value_from_arr ay=$_POST['data'];
    if ($value_from_ar ray[0]==5){
    // instructions
    }



    //Aho

    Comment

    • my.hero@web.de

      #3
      Re: Problem with array...

      I don't exactly understand what you want to do but I hope this will
      help ypu getting your values:

      <!-- *snip* -->

      <html>
      <form action="NewFile .php" method="POST">

      <?

      for($i=0;$i<5;$ i++){
      echo"<select name=\"test[]\">";
      echo"<option value=\"1\">1</option>";
      echo"<option value=\"2\">2</option>";
      echo"<option value=\"3\">3</option>";
      echo"</select>";



      }

      ?>
      <INPUT type="submit">
      </form>


      <?

      # this will show you all values that are in $_POST
      echo '<pre>', print_r($_POST) , '</pre>';

      # extract the array that contains all 5 values of the drop downs
      $your_data = $_POST["test"];

      # display the array
      echo '<pre>', print_r($your_d ata), '</pre>';

      # run through the array
      foreach ($your_data as $value_from_arr ay)
      {
      # ...and do with the values whatever you want to do
      if ($value_from_ar ray==5)
      {
      // instructions
      # :-)
      }
      }

      ?>

      </html>

      <!-- *snap* -->






      Leszek schrieb:
      [color=blue]
      > I have a form with 5 dropdown lists I want to put all selected values to an
      > array called $data[] so i wrote sometthing like this:
      >
      > for(i=0;i<5;i++ ){
      > echo"<select name=\"\$data[]\">";
      > echo"<option value=\"1\">1</option>";
      > echo"<option value=\"1\">2</option>";
      > echo"<option value=\"1\">3</option>";
      > echo"</select>";
      > }
      >
      > So I think this will put all selected values into an array.
      > But the problem is that when user submits the form this array goes to $_POST
      > array
      > And I have no idea how to read a value from an array $data which is into
      > array $_POST and use it as a part of "if" statement
      >
      > for example
      > if (value_from_arr ay==5){
      > // instructions
      > }
      >
      > Thanks for help
      > Leszek[/color]

      Comment

      Working...