Jump to content


Photo

determine variable names for dynamic variables *RESOLVED*


  • Please log in to reply
10 replies to this topic

#1 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 15 September 2006 - 11:45 PM

heres the situation... a form is posted by a user saying i am bringing X number of guests with me to an event.... they are brought to a new page that have x number of boxes so tehy can enter in the name and email address of the people they are bringing......

how do i know how to get these variables back because i dont know what they are on the form because i dont know what X is....

the form i can just have it echo field_id=guest<?echo guest_num?> but once that is posted how do i know how many of those variables i am going to be looking for?  can i make the variables in my php code dynamic as in if($guest<?echo guest_num?>

#2 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 16 September 2006 - 12:27 AM

How are they specifying the amount?  If they're doing it by a small text-area, just give it a name and then it's easy to do.  You can just access the number of people coming with them with $_POST['guests'] or whatever you choose to name the element.

Then just do

if(!empty($_POST['guests'])) {
$guests = $_POST['guests'];
$_SESSION['guests'] = $guests;
for($i = 0; $i < $guests; $i++) {
   echo "Guest " . $i . "<br />";
   echo "<input type=text size=25 name=" . $i . "><br />";
}
}
The session var is just so you know how many names you'll be checking on the next page. 

#3 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 12:38 AM

http://207.5.19.133/...ent&event_id=41

click the I want to go and then on the next page make the dropdown selector anything other then 0 and you will see whats going on....  right now those form values all have the same name... thats what i need to change but i need to do it in a manner so when i post that second form i know what variables im reading and what they are called....

#4 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 04:05 AM

any ideas?

#5 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 16 September 2006 - 04:15 AM

while(is_set($_GET['guestnum'.$i)) {
  do something with $_GET['guestnum'.$i];
  $i++;
}

should work

you can also "eval" code to do what you were talking about:
eval("if($guest".$guest_num");");

but eval can get pretty confusing

#6 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 04:26 AM

arent your quotes in the wrong places??

shoulndt it read

$_POST['$guestnum.$i'] witht the quotes outside both vars?

#7 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 16 September 2006 - 04:29 AM

in standard strings, it should work either way. as an array pointer, not so sure. To be safe, I always attach variables outside -- but that's just me.

#8 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 04:31 AM

this isnt right because its not making me a new variable... its putting data in a variable...

i need to be able to know that $guest1_name exists and guest1_email exists and i need to be able to process all these variables until all the guests are through.... but if there were 100 guests then that means i would have to write the code out for 100 propeple and then counter to stop on time but thats a lot of bloat....  so im not trying to load data into a variable... im trying to make the variables on the fly.....

#9 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 04:41 AM

case "proc_guests":
		$i='1';
		$guests=3;
		$guestnum1="test";
		$guestnum2="tesingt";
		$guestnum3="tessafasfd sdft";
		//$guests=mysql_real_escape_string($_POST['guests']);
		while($i<=$guests){
			echo "$guestnum.$i";
			$i++;
			}
	break;

which is a variation of your example outputted=
.1.2.3

without the " is outputs 123

#10 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 05:17 AM

does anyoen have any alternative ideas for how i could work this code??  im just getting stuck on how i know what the name of the form fields im fetchign data from will be....  if there is a better way then let me know......

#11 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 05:49 AM

ok i got it... woohoo

$i='1';
		$guests=3;
		$guestnum1="test";
		$guestnum2="tesingt";
		$guestnum3="tessafasfd sdft";
		//$guests=mysql_real_escape_string($_POST['guests']);
		while($i<=$guests){
			echo ${guestnum.$i};
			$i++;
			}

returns what i need





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users