473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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=>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
1 3144
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Joshua Neuheisel | last post by:
The following code compiles with gcc 3.2.2 and Visual C++ 6: #include <stdio.h> int main() { int a = {3, 4}; printf ("%d\n", 0); return 0; }
12
by: Alexander Stippler | last post by:
Hello, I've got a quite simple question. I want to have something like int main() { double array = {{1,2,3}, {4,5,6}, {7,8,9}}; double **field; field = array;
9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
6
by: Carl | last post by:
Hi, I want to pass an array of object of different type to a function and use it in the function. I am not able to have it working. Function that create the array... public virtual DataSet...
2
by: ramesh | last post by:
hi, I am using Com+ in my application. It will have InsertRecords,selectRecords,updateRecords function. In the Web Form i have Drop-down list. I want to select records from SQL and add it to this...
3
by: Omar Llanos | last post by:
I have Form1 and Form2 (which is inherited from Form1), and I created a button in Form2 that will fill up a textbox in Form1. What code would do that? I tried the simplest way: //from child...
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
16
by: David Lozzi | last post by:
Hello, I have some code that adds a new user. The new user has a checkboxlist of items which they can be associated with. I would like to send this list of items to TSQL along with the new user...
13
by: Pat | last post by:
I can't figure out how to set up a Python data structure to read in data that looks something like this (albeit somewhat simplified and contrived): States Counties Schools Classes Max...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.