Jump to content


Photo

Passing multiple variables...


  • Please log in to reply
15 replies to this topic

#1 jcbarr

jcbarr
  • Members
  • PipPipPip
  • Advanced Member
  • 219 posts

Posted 01 August 2006 - 09:36 PM

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;

$pos1 = $_POST['pos1'];

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.

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 09:45 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 jcbarr

jcbarr
  • Members
  • PipPipPip
  • Advanced Member
  • 219 posts

Posted 01 August 2006 - 09:50 PM

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.

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 09:51 PM

try this
<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[]">

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

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

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

hope this helps.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 09:53 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 jcbarr

jcbarr
  • Members
  • PipPipPip
  • Advanced Member
  • 219 posts

Posted 01 August 2006 - 10:01 PM

<?php
$_POST['pos'][0]
$_POST['name'][0]
...

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

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

hope this helps.


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

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 10:04 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 jcbarr

jcbarr
  • Members
  • PipPipPip
  • Advanced Member
  • 219 posts

Posted 01 August 2006 - 10:08 PM

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.

#9 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 10:22 PM

didn't you see this??
<?php
foreach($_POST['pos'] as $k=>v)
{
  echo $v;
}
?>


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

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

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#10 jcbarr

jcbarr
  • Members
  • PipPipPip
  • Advanced Member
  • 219 posts

Posted 01 August 2006 - 10:25 PM

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;

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

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


#11 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 10:33 PM

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

}

?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#12 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 10:42 PM

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

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

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#13 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 10:46 PM

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

}

?>

:) this will give you error :) $v is not an array
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#14 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 August 2006 - 10:48 PM

<?php
foreach($_POST['pos'] as $v) {

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

}

if($_POST['submit']){

insert code

}

?>

thats better cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#15 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 10:49 PM

have you tried it yourself?
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#16 jcbarr

jcbarr
  • Members
  • PipPipPip
  • Advanced Member
  • 219 posts

Posted 01 August 2006 - 11:11 PM

Wonderful!! Thanks for all the help!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users