variables names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

variables names

Post by hmsg »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi
  

    I have a form that is created with diferents fields, the user pick 3 fields, so the form for this user as field1 field2 and field3, but other user may need 4 fields so the form is created with 4 fields, field1 field2 field3 and field4.

    So my question is: is possible create name of variables dynamic? the code that i want is to catch all the fields that i have in my form:

Code: Select all

<?php

        $field1 = $_GET['field1'];
        $field2 = $_GET['field2'];

      #to make this in a for cicle, for my variables names have the "field".$i - where $i is the nunmber  of the field 
     # example, if $i=1  i wnat to create a code line like this $field1 = $_GET['field1']; 
     # field is fixed name and the rest of the name of the variable came from $i

     #is this possible?
  ?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

It's possible. But there's a way that is usually easier.
try

Code: Select all

<html>
	<head><title>array test</title></head>
	<body>
		<pre><?php print_r($_POST); ?></pre>
		<form method="POST">
			<div>
				<input type="text" name="abc[]" value="a" /><br />
				<input type="text" name="abc[]" value="b" /><br />
				<input type="text" name="abc[]" value="c" /><br />
				<input type="text" name="abc[]" value="d" /><br />
				<input type="submit" />
			</div>
		</form>
	</body>
</html>
The "trick" are the [] after abc. It's working similar to the php code

Code: Select all

<?php
$abc = array();
$abc[] = 'a';
$abc[] = 'b';
// ...
?>
You can also use

Code: Select all

				<input type="text" name="abc[1]" value="a" /><br />
				<input type="text" name="abc[99]" value="b" /><br />
				<input type="text" name="abc[name]" value="c" /><br />
				<input type="text" name="abc[yaddayadda]" value="d" /><br />
or even multi-dimensional arrays

Code: Select all

				<input type="text" name="abc[1][name]" value="a" /><br />
				<input type="text" name="abc[1][zip]" value="b" /><br />
				<input type="text" name="abc[2][name]" value="c" /><br />
				<input type="text" name="abc[2][zip]" value="d" /><br />
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

Post by hmsg »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


For what i wnat i think the multi arrays is the best solution, but how do i get the values in the php code:

Code: Select all

$abc = array(); 
    $abc[$i][nome] = $_POST['abc[$i][nome]'];
    $abc[$i][ZIP] = $_POST['abc[$i][ZIP]'];
    $abc[$i][...] = $_POST['abc[$i][...]'];
#Is something like this?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hmsg wrote:$_POST['abc[$i][nome]']
The syntax would be $_POST['abc'][$i][nome].
But there's an even simpler way using foreach

Code: Select all

<html>
	<head><title>array test</title></head>
	<body>
	<?php
	if ( isset($_POST['abc']) && is_array($_POST['abc']) ) {
		foreach( $_POST['abc'] as $index=>$entry ) {
			echo $entry['name'], "<br />\n";
			echo $entry['zip'], "<br />\n";
		}
	}
	?>
		<form method="POST">
			<div>
				<input type="text" name="abc[1][name]" value="a" /><br />
				<input type="text" name="abc[1][zip]" value="b" /><br />
				<input type="text" name="abc[2][name]" value="a" /><br />
				<input type="text" name="abc[2][zip]" value="b" /><br />
				<input type="submit" />
			</div>
		</form>
	</body>
</html>
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

Post by hmsg »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Something is not working!!!

 in my form file i have this:

Code: Select all

<?php

                   for ($i=1; $i <= $num_rpms; $i++){
                           print(" RPM".$i.": <input type="."text"." name="."rpm[".$i."] size="."50"." maxlength="."200"." />");
                    }
         ?>

and in the file that process the form i have this:

Code: Select all

<?php
                    for($i=1; $i<=$num_rpms; $i++){ 
			$rpm_name[$i] = $_POST['name'][$i]; 
		        print($rpm_name[$i]."<br />"); 
               #the $num_rpms is the variable where i keep how many fields i have	
	        }

              ?>
But i can't see anything in the screen!! What i'm doing wrong?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Why _POST'name'] when your form field is called rpm?
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

Post by hmsg »

You're right .... stupid mistake !!!!


thank you very much for your help
Post Reply