Page 1 of 1

Variable variables

Posted: Tue Feb 27, 2007 1:49 am
by tristanlee85

Code: Select all

$count = 0;
	while ($count < $_POST['num_loaders'])
	{
		${"loader_$count"} = $_POST['loader_{$count}'];
		echo ${"loader_$count"};
		$count++;
	}
Basically what I want this to do is get all the $loader_[ ] variables being posted. For instance, let's assume that $_POST['num_loaders'] = 2 The 2 values that are posted should be:

$loader_0 = "loader_0s name";
$loader_1 = "loader_1s name";

Here is the HTML of the form being posted:

Code: Select all

<html>
<body>
<div align='center'><b>Loader Productivity</b></div>
<div align='center'>Step 3: Enter in the loaders' names and select which doors they loaded

<form action='?mode=step4' method='post'>

	<table width='30%' align='center' border='1'>
		<tr>
			<td align='center' width='15%'>Loader</td>
			<td align='center' width='15%'>Door(s)</td>
		</tr>
		<tr>
			<td><input type=text name='loader_0'></td>
			<td>
				<select multiple name='loader_0doors' size='5'>
					<option name='door_402'>402</option>
					<option name='door_403'>403</option>
					<option name='door_404'>404</option>
					<option name='door_405'>405</option>
					<option name='door_406'>406</option>
					<option name='door_407'>407</option>
					<option name='door_408'>408</option>
					<option name='door_409'>409</option>
					<option name='door_410'>410</option>
					<option name='door_411'>411</option>
					<option name='door_412'>412</option>
					<option name='door_413'>413</option>
					<option name='door_414'>414</option>
					<option name='door_415'>415</option>
					<option name='door_416'>416</option>
					<option name='door_417'>417</option>
				</select>
			</td>
		</tr>
		<tr>
			<td><input type=text name='loader_1'></td>
			<td>
				<select multiple name='loader_1doors' size='5'>
					<option name='door_402'>402</option>
					<option name='door_403'>403</option>
					<option name='door_404'>404</option>
					<option name='door_405'>405</option>
					<option name='door_406'>406</option>
					<option name='door_407'>407</option>
					<option name='door_408'>408</option>
					<option name='door_409'>409</option>
					<option name='door_410'>410</option>
					<option name='door_411'>411</option>
					<option name='door_412'>412</option>
					<option name='door_413'>413</option>
					<option name='door_414'>414</option>
					<option name='door_415'>415</option>
					<option name='door_416'>416</option>
					<option name='door_417'>417</option>
				</select>
			</td>
		</tr>
	</table>
<input type=submit value='Step 4 ->'>
<input type=hidden name='num_loaders' value='4'>
</form>

</body>
</html>
How can I get $_POST['loader_{$count}'] to increment like ${"loader_$count"}?

Posted: Tue Feb 27, 2007 2:35 am
by Christopher
A more generic method might be something like:

Code: Select all

function get_prefixed_elements($array, $prefix) {
    $prefix_length = strlen($prefix);
    $elements = array();
    foreach ($array as $key => $value){
        if (substr($key , 0, $prefix_length) == $prefix) {
            $elements[$key] = $value;
        }
    }
    return $elements;
}

$loaders = get_prefixed_elements($_POST, 'loader_');
$num_loaders = count($loaders);

Posted: Tue Feb 27, 2007 9:14 am
by feyd
<option> tags don't have a "name" attribute. I'd side with arborint in regard to the question however, use an array.

Posted: Thu Mar 01, 2007 11:49 pm
by tristanlee85
I tried a little bit different approach with this code:

Code: Select all

if ($_GET['mode'] == "step4")
{
	//Get loaders from step3
	
	foreach ($_POST['loader'] AS $key => $value) { echo "{$key} => {$value} <br>"; }
	
	foreach ($_POST['loader_doors'] AS $key => $value) { echo "{$key} => {$value} <br>"; }
	
	$template = "productivity.tpl"; // Output the data to this template file
}
Here is a picture of that the site looks like:

Image

I get an output of:

Code: Select all

0 => Tristan
1 => Ben
0 => 402
1 => 403
2 => 405
Here is the HTML for that page:

Code: Select all

<html>
<body>
	<div align='center'><b>Loader Productivity</b></div>
	<div align='center'>Step 3: Enter in the loaders' names and select which doors they loaded
		<form action='?mode=step4' method='post'>
		<table width='30%' align='center' border='1'>
			<tr>
				<td align='center' width='15%'>Loader</td>
				<td align='center' width='15%'>Door(s)</td>
			</tr>
			<tr>
				<td>
					<input type=text name='loader[]'>
				</td>
				<td>
					<select multiple name='loader_doors[]' size='5'>
						<option>402</option>
						<option>403</option>
						<option>404</option>
						<option>405</option>
						<option>406</option>
						<option>407</option>
						<option>408</option>
						<option>409</option>
						<option>410</option>
						<option>411</option>
						<option>412</option>
						<option>413</option>
						<option>414</option>
						<option>415</option>
						<option>416</option>
						<option>417</option>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<input type=text name='loader[]'>
				</td>
				<td>
					<select multiple name='loader_doors[]' size='5'>
						<option>402</option>
						<option>403</option>
						<option>404</option>
						<option>405</option>
						<option>406</option>
						<option>407</option>
						<option>408</option>
						<option>409</option>
						<option>410</option>
						<option>411</option>
						<option>412</option>
						<option>413</option>
						<option>414</option>
						<option>415</option>
						<option>416</option>
						<option>417</option>
					</select>
				</td>
			</tr>
		</table>
		<input type=submit value='Step 4 ->'>
		<input type=hidden name='num_loaders' value='2'>
		</form>
</body>
</html>
The problem is, Tristan is in charge of doors 402 and 403. Ben is in charge door 405. When I read the $_POST values, how can associate the correct doors with the loader instead of having them list in the array they are currently in? It wouldn't be so hard if the amount of loaders were consistant, like 2 loaders, but it could vary from 1 to 10, maybe more.

Posted: Fri Mar 02, 2007 12:23 am
by nickvd
Have you printed out what is contained within $_POST, so you can get an idea of how things are being given to you? You would also be wise to add value="" attributes to your option tags.

When you receive the data in the $_POST superglobal, you can probably assume that $_POST['loader'][0] will be related to $_POST['loader_doors'][0] and therefore you should be able to process it accordingly...

Posted: Fri Mar 02, 2007 12:42 am
by feyd
I'd rename the fields.

For example. The names could be "loader[0][name]" and "loader[1][name]" respectively. Likewise, the doors could then be "loader[0][doors][]" and "loader[1][doors][]" also respectively.

Posted: Fri Mar 02, 2007 1:09 am
by tristanlee85
Right now I have:

Code: Select all

foreach($_POST['loader'] AS $z => $loader)  
	{  
     		echo "Looking At {$loader}<br>  -  ";  
     		foreach ($_POST['loader_doors'][$z] AS $x => $doors) 
     		{ 
          		echo "&nbsp;&nbsp;&nbsp;&nbsp;{$doors}<br/>"; 
    		} 
     		echo "<br/><br/>"; 
 
	}
but I'm getting this error:

Code: Select all

Looking At a
-
Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/productivity/steps.php on line 166


Looking At b
-
Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/productivity/steps.php on line 166
Line 166 is:

Code: Select all

foreach ($_POST['loader_doors'][$z] AS $x => $doors) 

Posted: Fri Mar 02, 2007 7:49 am
by feyd
Note how your code expects different information than what my example would provide.

Posted: Fri Mar 02, 2007 6:50 pm
by tristanlee85
So to get "loader[0][name]" and "loader[1][name], I use this:

Code: Select all

<input type=text name='loader[" . $loader_count . "][]'>
I use the same method to get "loader_doors[0][]". Here is my HTML out to prove it:

Code: Select all

<input type=text name='loader[0][]'>
<select multiple name='loader_doors[0][]' size='5'>
Assuming that's correct, what will I have to change here?

Code: Select all

foreach($_POST['loader'] AS $z => $loader)  
	{  
     		echo "Looking At {$loader}<br>  -  ";  
     		foreach ($_POST['loader_doors'][$z] AS $x => $doors) 
     		{ 
          		echo "&nbsp;&nbsp;&nbsp;&nbsp;{$doors}<br/>"; 
    		} 
     		echo "<br/><br/>"; 
 
	}

Posted: Fri Mar 02, 2007 7:08 pm
by nickvd

Code: Select all

<input type=text name='loader[0][name]'>
<select multiple name='loader[0][door]' size='5'>

Code: Select all

echo $_POST['loader'][0]['name'];
echo $_POST['loader'][0]['door'];

Posted: Fri Mar 02, 2007 8:04 pm
by tristanlee85

Code: Select all

foreach($_POST['loader'] AS $key => $value)
foreach($_POST['loader'][][name] AS $key => $value)

The bold is the part I need to focus on, but i don't know how to make it increment from say:

foreach($_POST['loader'][0][name] AS $key => $value)

to

foreach($_POST['loader'][1][name] AS $key => $value)

and so on...

If it helps, my array is coming out right using

Code: Select all

echo "<pre>" . print_r($_POST) . "</pre>";

Code: Select all

Array ( [loader] => Array ( [0] => Array ( [0] => tristan ) [1] => Array ( [0] => ben ) ) [loader_doors] => Array ( [0] => Array ( [0] => 402 [1] => 403 ) [1] => Array ( [0] => 404 ) ) [num_loaders] => 2 )

Posted: Fri Mar 02, 2007 8:15 pm
by tristanlee85

Code: Select all

$num = $_POST['num_loaders'];

	$count = 0;
	while ($count < $num)
	{
		$loader = $_POST['loader'][$count];
		$doors = implode("," , $_POST['loader_doors'][$count]);
		echo $loader . " has doors " . $doors;
		$count++;
	}
gives me

Code: Select all

Array has doors 402,403
Array has doors 404
I'm close, but I don't know what to try next...