Efficiently & safely (re)filling array from $_POST

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

    #1

    Efficiently & safely (re)filling array from $_POST

    I'm using a single script to generate a table with <input>s in each row.
    I fill the array with initial values, then write it out to the table
    and let the user edit the values. Something like:

    $myarray = $array(1, 2, 3, ... 100);
    echo 'Enter your changes, then click Submit:';
    foreach ($array as $i)
    echo '<table tags> <input value="'.$i.'" name="index.'$i .'"> <table
    tags>';

    When the user clicks Submit, I need to pull the values back into the
    array so I can re-display & operate on it. The data is in $_POST, which
    is associative and contains stuff other than my array. $_POST contains
    something like:

    Submit=>submit
    someVariable=>s omeValue
    anotherVariable =>anotherValue
    index1=>1
    index2=>99
    index3=>93.2
    ....
    index100=>-3

    After doing a quick google search, I tried:

    for ($i = 0; $i < count($array); $i++)
    {
    $name = 'index'.$i;
    $array[$i] = $_POST["$name"];
    }

    It runs, but I get nothing out of $_POST - those key values are unset.

    Is my only option to inefficiently iterate through $_POST AND $myarray,
    and doing the assignment to the proper element when I find the
    corresponding name in $_POST's key?

    Can I use the $_POST keys as indexes in a loop? If so, is it *safe* to
    do that, and what's the syntax? Something vaguely like:

    for ($i = 0, $key = index1; $i < 100 && $key < index100; $i++, $key++)
    array[$i] = $_POST[$key];

    --
    --
    Lynn Wallace http://www.xmission.com/~lawall
    "I'm not proud. We really haven't done everything we could to protect
    our customers. Our products just aren't engineered for security."
    --Microsoft VP in charge of Windows OS Development, Brian Valentine.

  • Raptor

    #2
    Re: Efficiently &amp; safely (re)filling array from $_POST

    Raptor wrote:[color=blue]
    > After doing a quick google search, I tried:
    >
    > for ($i = 0; $i < count($array); $i++)
    > {
    > $name = 'index'.$i;
    > $array[$i] = $_POST["$name"];
    > }[/color]

    Never mind, figured it out. Take out the "" and and you can pull any
    value out of $_POST using the posted name.

    --
    --
    Lynn Wallace http://www.xmission.com/~lawall
    "I'm not proud. We really haven't done everything we could to protect
    our customers. Our products just aren't engineered for security."
    --Microsoft VP in charge of Windows OS Development, Brian Valentine.

    Comment

    Working...