[PHP] Array in a Loop Question
I have a form that gets repeated depending on number of members in a group(1 form surrounds all members). I separate the entries by assigning a count value to the names of the inputs (Ex. ). Does anyone know how I can pull the values back out of the $_POST[]? Example of ideal scenario that does not work: case "process1": for ($i=0;$i<$_POST[count];$i++) { $_POST[address$i] } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array in a Loop Question
for ($i=0;$i<$_POST[count];$i++) { echo $_POST['address' . $i] . ''; } Concanate array key 'address' with $i -- Julio Nobrega. Um dia eu chego lá: http://sourceforge.net/projects/toca Ajudei? Salvei? Que tal um presentinho? http://www.submarino.com.br/wishlistclient.asp?wlid=664176742884 "Jeff Hatcher" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I have a form that gets repeated depending on number of members in a group(1 form surrounds all members). I separate the entries by assigning a count value to the names of the inputs (Ex. ). Does anyone know how I can pull the values back out of the $_POST[]? Example of ideal scenario that does not work: case "process1": for ($i=0;$i<$_POST[count];$i++) { $_POST[address$i] } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array in a Loop Question
On Tuesday, March 26, 2002, at 01:18 PM, Jeff Hatcher wrote: > I have a form that gets repeated depending on number of members in a > group(1 form surrounds all members). I separate the entries by assigning > a count value to the names of the inputs (Ex. name=address$count value=>). Does anyone know how I can pull the values > back out of the $_POST[]? > > Example of ideal scenario that does not work: > case "process1": > for ($i=0;$i<$_POST[count];$i++) > { > $_POST[address$i] > } The initial form: for ($i = 1; $i <= $_POST['count']; $i++) { print "\n"; } print " What the above loop does is it makes a number of address inputs equal to $count... if $count is equal to 3, then you will get I changed your $i from 0 to 1 because it makes it mentally easier to work with (for me at least). Now in your next script, which is the target of the form, here is what you want to have: for ($i = 1; $i <= $_POST['count']; $i++) { // do some code with $_POST["address{$i}"], such as // entering it into a database or echoing it to the user in HTML } Some further notes -- the hidden form field was so that you could give the second script the "count" variable, so it knows how many loops to do (this value is otherwise not available to the second script). Also, the use of doublequotes in the second script ($_POST["address{$i}"]) is imperative because with singlequotes the variable $i will not expand to $_POST["address2"] or whatever. HTH, Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array in a Loop Question
I think to get the results you are wanting, you might try this. the address[] tells the form that this element is an array and when submitted, will be converted into an indexed array on the process page. so you take this new array on the process page and do something like this. foreach($address AS $k => $v) { echo "$k : $v"; } Jim Lucas www.bend.com - Original Message - From: "Jeff Hatcher" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, March 26, 2002 10:18 AM Subject: [PHP] Array in a Loop Question I have a form that gets repeated depending on number of members in a group(1 form surrounds all members). I separate the entries by assigning a count value to the names of the inputs (Ex. ). Does anyone know how I can pull the values back out of the $_POST[]? Example of ideal scenario that does not work: case "process1": for ($i=0;$i<$_POST[count];$i++) { $_POST[address$i] } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array in a Loop Question
Brilliant thanks ! I've got three of these in my form ! lol > I think to get the results you are wanting, you might try this. > > > the address[] tells the form that this element is an array and when > submitted, will be converted into an indexed array on the process page. > > so you take this new array on the process page and do something like this. > > > foreach($address AS $k => $v) > { > echo "$k : $v"; > } > > Jim Lucas > www.bend.com > > - Original Message - > From: "Jeff Hatcher" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Tuesday, March 26, 2002 10:18 AM > Subject: [PHP] Array in a Loop Question > > > I have a form that gets repeated depending on number of members in a > group(1 form surrounds all members). I separate the entries by assigning > a count value to the names of the inputs (Ex. name=address$count value=>). Does anyone know how I can pull the values > back out of the $_POST[]? > > Example of ideal scenario that does not work: > case "process1": > for ($i=0;$i<$_POST[count];$i++) > { > $_POST[address$i] > } > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Array in a Loop Question
for ($i=0;$i<$_POST[count];$i++) { echo $_POST['address' . $i] . ''; } Concanate array key 'address' with $i This works fine and I also can do the address[] but my ultimate result is to put this in the database. So address='$_POST['address' . $i]' fails and address='$_POST[address][$i]' fails. I can reassign the values to new variables i.e. $address = $_POST['address' . $i]; DB Update address='$address' Or $address $_POST[address][$i]; DB Update address='$address' So my question is how can I put this in the database without having to reassign my variable name? Current Solution: case "process1": for ($i=0;$i<$_POST[count];$i++) { $postal=$_POST[postal1][$i] . " " . $_POST[postal2][$i]; $state=$_POST[state][$i]; $address=$_POST[address][$i]; $email=$_POST[email][$i]; $city=$_POST[city][$i]; $customerid=$_POST[customerid][$i]; mssql_query("UPDATE customer SET postal='$postal',state='$state',address='$address',email=$_POST['email' . $i],city='$city' WHERE id='$customerid'") or die("DATABASE ERROR: updating customer, residentchange process 1"); } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array in a Loop Question
On Tuesday, March 26, 2002, at 02:12 PM, Jeff Hatcher wrote: > So my question is how can I put this in the database without having to > reassign my variable name? You have to "jump out" of the quoted string that represents your SQL query when you echo the value of the $_POST variable. Like this: $sql = "SELECT a FROM b WHERE c='" . $_POST['d'] . "'"; Note that I 'jumped out' immediately after the singlequote mark (which you need if your value is going into a string field) by closing the double-quoted string, using the dot to append the value of the variable $_POST['d'], and then using a dot to append another double-quoted string (which contains nothing but a single quote in it). Sometimes, depending on what your variable names look like, you can just escape your variable by using braces ('{' and '}'), but I think it's just easier to use the dot do appending from string to variable value to string again. Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php