Forum Moderators: coopster

Message Too Old, No Replies

arrays and $ POST

         

tkd1987

8:06 pm on Oct 21, 2007 (gmt 0)

10+ Year Member



hi guys, first time on webmasterworld,
have been helped jsut by reading before.

what i am trying to do is from one form i select the amount of fields to be displayed, on the next page (which then displays it)
on this page it gets all the information and adds to mysql, sounds easy, so why am i struggling.

here are the fields


<br>Description <input type="text" name="description[]" size="20"><br>
Quantity <input type="text" name="quantity[]" size="20"><br>
Price <input type="text" name="price[]" size="20"><P>

that works fine.

havent really used an array before if thats what i should be using.
i assumed i would use something like this


$price[] = $_POST['price'];

all help is more than appreciated

Thank you.

Aaron

[edited by: jatar_k at 11:42 am (utc) on Oct. 22, 2007]
[edit reason] no urls thanks [/edit]

Habtom

12:50 pm on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, tkd1987.

I can't be sure what exactly you are trying to achieve. Perhaps you might explain it better.

Description <input type="text" name="description[]" size="20"><br>
Quantity <input type="text" name="quantity[]" size="20"><br>
Price <input type="text" name="price[]" size="20"><P>

Can you tell me why you put the name of the input boxes in an array form?

Why are you trying to transfer the values basically from one array to another?

$price[] = $_POST['price'];

Habtom

tkd1987

3:54 pm on Oct 22, 2007 (gmt 0)

10+ Year Member



hiya, thanks for the reply.

i just put the form values as arays because i was trying to work around something that could work.

on the 1st page you select how many fields you want to be displayed on the next page.

then page 2 displays that many

page three should upload them to the database, but as they all have the same name how would i go about this? or how do i get it to recognise how many fields it should look for?

sorry if it sounds confusing!

Regards

d40sithui

5:55 pm on Oct 22, 2007 (gmt 0)

10+ Year Member



your question is more based on array usage i think. are the field types going to be all text, or are you planning to have dynamic field types as well(drop down, radio, checkboxes, etc). this could make things more complicated but still doable.

but if u have only one predetermined type(text), you can get the variable by using the function array_keys() like below.

<?
$price = $_POST['price'];
foreach(array_keys($price) as $key){
echo "\$price[$key] = ".$price[$key]. "<br>\n";
}
?>

or try this loop if you know all your keys are going to be integers.

<?
for($i = 0; $ i < sizeof($price); $i++){
echo "\$price[$i] = ". $price[$i]. "<br>\n";
}
?>

PHP_Chimp

5:55 pm on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im not sure if I understand (its a little late, so im a bit slow) but are you saying that one page 1 you have -
description
quantity
price
The you have the same named, but with different data, on page 2. Then the same variables on page 3 but with different data?

If that is so then you dont need to worry as the $_GET/$_POST variables will only hold the information from that submission i.e.

page 1
There will be no $_POST or $_GET when this page is called.
Well there may be both, but as far as the workings of your script is concerned they are not being used.

page 2
The POST variables are coming from page 1 when this page is first called.
$_POST['price'] = '100';
$_POST['description'] = 'widget';
$_POST['quantity'] = '1';

page 3
the $_POST variables will contain whatever information was entered on page 2. So it may look completely different -
$_POST['description'] = 'widgets1';
$_POST['quantity'] = '10000';

So you dont need to worry about arrays.

tkd1987

8:56 pm on Oct 23, 2007 (gmt 0)

10+ Year Member



hey guys thanks for the reply

d40sithui, a big thanks to you

this


for($i = 0; $i < sizeof($price); $i++)
{
echo "\$price[$i] = ". $price[$i]. "<br>\n";
}

worked great, just got to tweak and play a little, would it work in a similar sort of way inorder to add all those to mysq, just instead of the echo for the mysql query?

regards

A

d40sithui

9:35 pm on Oct 23, 2007 (gmt 0)

10+ Year Member



you are w/c.
yea to insert into the database you would do the loop and instead of printing it, you would do something like

for($i = 0; $i < sizeof($price); $i++){
/* where MyTable is your table name
and MyPrice is the column name
*/

$query = "insert into MyTable (MyPrice) values(".$price[$i].")";
mysql_query($query);
}

my real concern is what your tables look like, and what kind of environment you're working in to allow such a big update to take place. looks like youll be updating alot. don't forget to validate your inputs before inserting.

tkd1987

10:16 am on Oct 24, 2007 (gmt 0)

10+ Year Member



yea thats all cool..

thanks again,
i have changed the original code slightly so that instead of showing just the price it does the price description and quantity, (i just used price to get started), and now i will work on the db part.

it is going to be an invoice script!

so all the details are inputted, then you have the option at the end to view and print a pdf invoice...
but the reason i have done it to select how many items there are, is because it creates a random reference number and that needs to be the same on all items on the same invoice...

thanks once more

A

tkd1987

2:47 pm on Oct 24, 2007 (gmt 0)

10+ Year Member



d40sithui,
just one more small thing? what do you mean by valiadate before inserting?

confirming the result then going to another page to insert?
if that is how i guess it would would sessions to do so, but would it work the same as the values did first time round?

Thanks

d40sithui

3:25 pm on Oct 24, 2007 (gmt 0)

10+ Year Member



well you'd want to check that the inputs are valid. in this case since ur working with price, i guess you would need to check that the inputs are numbers and not letters, special chars, etc.