By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,825 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,825 IT Pros & Developers. It's quick & easy.

Efficiently & safely (re)filling array from $_POST

P: n/a
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=>someValue
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.

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Raptor wrote:
After doing a quick google search, I tried:

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


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.

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.