Jump to content

Retrieving form values from multi dimensional array


Recommended Posts

Hi,

 

I'm having problems retrieving the values from a form posting without getting a warning

 

My form records playername, hours played and minutes played

 

<li id="input1" style="margin-bottom:4px;" class="clonedInput">
	Name:
        <select name="player[1][userid]" id="player[1][userid]" class="playerInput" style="width: 7em;" >
        	<option value=""></option>
        <?php
			while($row = mysql_fetch_array($sql)) {
				echo '<option value="'.stripslashes($row['userid']).'">'.stripslashes($row['name']).'</option>';	
			}
			mysql_data_seek($sql,0);
		?>
        </select>        
        <select name="player[1][hours]" id="player[1][hours]">
        <option value="">Hours</option>
        <option value=""></option>
        	<?php
			for($x = 0; $x <= 23; $x++ ) {
            		echo '<option value="'.$x.'">'.$x.'</option>';
			}
		?>
        </select>
        <select name="player[1][minutes]" id="player[1][minutes]">
        <option value="">Minutes</option>
        <option value=""></option>
        	<?php
			for($y = 15;  $y <= 59; $y+=15) {
            		echo '<option value="'.$y.'">'.$y.'</option>';
			}
		?>
        </select>
</li>

 

I use some javascript to create new players as necessary so every player has a userid, hours, minutes played...

After posting the query string looks like this

 

/cLeagueHandler.php?player[1][userid]=1&player[1][hours]=3&player[1][minutes]=15&player[2][userid]=3&player[2][hours]=6&player[2][minutes]=45&submit=Save+Points

 

player1

userid = 1

hours = 3

minutes = 15

 

player2.......

 

The code i'm using to access the values is

 

        print_r ($_GET); // test display form values
       
foreach ($_GET as $key => $value) {
	echo '<br/>'.$key.'<br/>	';
	foreach ($value as $iKey => $iValue) { // Line 72
		foreach ($iValue as $xKey => $xValue) {
			echo " The $xKey for Player $iValue is $xValue <br/>";
		}
	}
}

 

The above code displays

 

Array ( [player] => Array ( [1] => Array ( [userid] => 1 [hours] => 3 [minutes] => 15 ) [2] => Array ( [userid] => 3 [hours] => 6 [minutes] => 45 ) ) [submit] => Save Points )

player

The userid for Player Array is 1

The hours for Player Array is 3

The minutes for Player Array is 15

The userid for Player Array is 3

The hours for Player Array is 6

The minutes for Player Array is 45

 

submit

 

Warning: Invalid argument supplied for foreach() in .................cLeagueHandler.php on line 72

 

Line 72 is foreach ($value as $iKey => $iValue) {

 

If anyone could help me with this it would be great appreciated.......I want to be able to build and sql string to Insert a players id, hours, minutes into a database......(obviously not incl the submit value!)

Maybe this is not even the best way of approaching this.....I'm quiet new to PHP so apologies if this is long winded...I'm trying to give as much info as possible....

 

thanks in advance

 

tmfl

$_GET['submit'] is nut array

try

        print_r ($_GET); // test display form values
       
foreach ($_GET as $key => $value) {
	echo '<br/>'.$key.'<br/>	';
                     if($key != 'submit'){
	foreach ($value as $iKey => $iValue) { // Line 72
		foreach ($iValue as $xKey => $xValue) {
			echo " The $xKey for Player $iValue is $xValue <br/>";
		}
	}
                    }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.