Jump to content

Archived

This topic is now archived and is closed to further replies.

jcbarr

Passing multiple variables...

Recommended Posts

I am creating a page that will allow a user to input a lot of data into a table. The number of values that will be passed for each entry is 4. For example each entry will have the following fields;

POS
NAME
LEAGUE
STAT

The user will be able to add as many entries as they see fit. So this could result in a bunch of variables. So my question is this. When the form is submitted what would be the best way to gather all of the variables so that I can insert them into the database? I'm a little stuck as to what to do. Should I name them pos1, pos2... name1...name2...? Or is there a better way to do this?

I don't want to have a hundred lines like this in my code;

[code]$pos1 = $_POST['pos1'];[/code]

I hope that I have explained this well enough. If you need more info I will be more than glad to give it to you.

Anyone have any ideas on how to do this? I have passed variables from a form before, but that was when I knew exactly what variables were being passed and exactly how many there would be, so I'm not a complete newb...

Thanks in advance to anyone willing to take the time to help.

Share this post


Link to post
Share on other sites
To program in a proper php fashion you will have to $_POST['every_varable'] unless you have set  your php.ini to globels on what will cause php problams.

you are coding the correct way always post each varable.

Share this post


Link to post
Share on other sites
The problem with that is that I don't know how many there will be coming from the form.

I was wondering if there was some way to grab everything being sent with the POST command and dump them in to arrays or something like that and then do a loop where it dumps every value of the array in to a table field? I'm not sure if that can be done, and I'm not too good with arrays, but I was thinking it might be able to be done.

The problem with the form is that the user will select how many entries they make.

Share this post


Link to post
Share on other sites
try this
[code]<input type="text" name="pos[]">
<input type="text" name="name[]">
<input type="text" name="league[]">
<input type="text" name="stat[]">

<input type="text" name="pos[]">
<input type="text" name="name[]">
<input type="text" name="league[]">
<input type="text" name="stat[]">
[/code]

and you'll get postdata like this when the form is submitted
[code]<?php
$_POST['pos'][0]
$_POST['name'][0]
...

$_POST['pos'][1]
$_POST['name'][1]
...
?>[/code]

So then you can use a loop
[code]foreach($_POST['pos'] as $k=>v)
{
  echo $v;
}[/code]

hope this helps.

Share this post


Link to post
Share on other sites
dont quit understand sorry as for how many entrys the user makes has no diffrence to having the posts within the page.

you might want to lookup sessions ok.

good luck.

Share this post


Link to post
Share on other sites
[quote author=ryanlwh link=topic=102628.msg407599#msg407599 date=1154469098]
[code]<?php
$_POST['pos'][0]
$_POST['name'][0]
...

$_POST['pos'][1]
$_POST['name'][1]
...
?>[/code]

So then you can use a loop
[code]foreach($_POST['pos'] as $k=>v)
{
  echo $v;
}[/code]

hope this helps.
[/quote]

Okay I set the form names like you did, but do I still have to grab all of them with the POST lines?

Share this post


Link to post
Share on other sites
yes yes yes

you got to post to get the varables to post ok.

Think off it like a letter, if you dont put a stamp on it you dont get the letter, but if the letter got a stamp you get that letter.

hope this helps.

Share this post


Link to post
Share on other sites
Yeah so I guess I'm not going to be able to do what I wanted. I thought there might be some way to do a catch all. Like some fucntion that would catch all the pos[] variables being passed in one shot rather than having to put multiple lines to catch each one.

Share this post


Link to post
Share on other sites
didn't you see this??
[code]<?php
foreach($_POST['pos'] as $k=>v)
{
  echo $v;
}
?>[/code]


if you want to catch "everything" easily
[code]<?php
foreach($_POST as $k=>$v)
{
  if($k=='pos')
  {
      foreach($_POST[$k] as $v)
      {
          //do stuff with each pos
      }
  }
}
?>[/code]

or in your original way
[code]foreach($_POST as $k=>$v)
{
  if (strpos($k,'pos')!== false)
    //do stuff for pos
  if (strpos($k,'name') !== false)
    //do stuff for name
}[/code]

Share this post


Link to post
Share on other sites
Yeah I did and when I add that to my code I get this error;

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /public_html/dynamic/all_star/allstar.php on line 10

This is the only code so far in the script;

[code]<?php
$create = $_GET['CREATE'];
$edit = $_GET['EDIT'];

foreach($_POST['pos'] as $k=>v)
{
  echo $v;
}
?>[/code]

Share this post


Link to post
Share on other sites
ryanlwh if you use the foreach to get the array and post the information would you have to set the name according to the foreach to insert the information to the database.

cheers.
so you got to get the correct name for the insert but also use the $_POST statement i dont see any other way.

<input type="text" name="pos[]">
<input type="text" name="name[]">
<input type="text" name="league[]">
<input type="text" name="stat[]">
<?php
foreach($_POST['pos'] as $k=>$v) {

$name=$v[1];
$league=$v[2];
stat=$v[3];

}

if($_POST['submit']){

insert code

}

?>

Share this post


Link to post
Share on other sites
[code]
<?php
$create = $_GET['CREATE'];
$edit = $_GET['EDIT'];

foreach($_POST['pos'] as $k=>$v)
{
  echo $v;
}
?>
[/code]

Share this post


Link to post
Share on other sites
[quote author=redarrow link=topic=102628.msg407640#msg407640 date=1154471620]
ryanlwh if you use the foreach to get the array and post the information would you have to set the name according to the foreach to insert the information to the database.

cheers.
so you got to get the correct name for the insert but also use the $_POST statement i dont see any other way.

<input type="text" name="pos[]">
<input type="text" name="name[]">
<input type="text" name="league[]">
<input type="text" name="stat[]">
<?php
foreach($_POST['pos'] as $k=>$v) {

$name=$v[1];
$league=$v[2];
stat=$v[3];

}

if($_POST['submit']){

insert code

}

?>
[/quote]
:) this will give you error :) $v is not an array

Share this post


Link to post
Share on other sites
<?php
foreach($_POST['pos'] as $v) {

$name=$v[1];
$league=$v[2];
stat=$v[3];

}

if($_POST['submit']){

insert code

}

?>

thats better cheers.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.