Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Multidimensional array from textbox and checkbox values

Tags:

html

php

I'm hoping to solve a problem with a baseball team! ;)

I've a form with multiple textboxes - each of which has a series of checkboxes associated with it. To get a better feel, create a file with the following, and open in a browser. A picture speaks a thousand words.

<html>
<head></head>
<body>
        <h2>Enter Player Name and Positions They Can Play:</h2>
                        <form name="player" method="POST">

                <p>Player 01: <input type="text" name="p1" value="" />
                | P <input type="checkbox" name="pitch1" value="P" />
                | C <input type="checkbox" name="catch1" value="C" />
                | 1B <input type="checkbox" name="first1" value="1B" />
                | 2B <input type="checkbox" name="second1" value="2B" />
                | SS <input type="checkbox" name="short1" value="SS" />
                | 3B <input type="checkbox" name="third1" value="3B" /> |</p>

                <p>Player 02: <input type="text" name="p2" value="" />
                | P <input type="checkbox" name="pitch2" value="P" />
                | C <input type="checkbox" name="catch2" value="C" />
                | 1B <input type="checkbox" name="first2" value="1B" />
                | 2B <input type="checkbox" name="second2" value="2B" />
                | SS <input type="checkbox" name="short2" value="SS" />
                | 3B <input type="checkbox" name="third2" value="3B" /> |</p>

                <p>Player 03: <input type="text" name="p3" value="" />
                | P <input type="checkbox" name="pitch3" value="P" />
                | C <input type="checkbox" name="catch3" value="C" />
                | 1B <input type="checkbox" name="first3" value="1B" />
                | 2B <input type="checkbox" name="second3" value="2B" />
                | SS <input type="checkbox" name="short3" value="SS" />
                | 3B <input type="checkbox" name="third3" value="3B" /> |</p>
    <p><input type="submit" value="Submit" name="Submit" /></p>
    </form>
</body>
</html>

So, if a user typed in:

"Fred" and checked 1B, SS, 3B (first base and shortstop)

"Ted" and checked P, 2B (pitcher and second base)

"Joe" and checked P, 3B (pitcher and third base)

On submit, I'd like to see the following array (named roster, with sub-arrays named from the textbox entry and the sub-array contents only the checked values:

roster(

Fred(1B,SS,3B),

Ted(P,2B),

Joe(P,3B)

)

I'm confident I can handle everything beyond this... I'm just having a devil of a time figuring out how to create each sub array and associating them with the textboxes and adding the checkbox values to each array.

Any help is much appreciated! Thank you!

like image 977
Mike M Avatar asked Apr 14 '26 06:04

Mike M


1 Answers

It's not exactly what you asked, but a better way to manage this kind of forms is to choose vars names like this:

<p>Player 01: <input type="text" name="player[1][name]" value="" />
     | P <input type="checkbox" name="player[1][pitch]" value="P" />
            | C <input type="checkbox" name="player[1][catch]" value="C" />
            | 1B <input type="checkbox" name="player[1][first]" value="1B" />
            | 2B <input type="checkbox" name="player[1][second]" value="2B" />
            | SS <input type="checkbox" name="player[1][short]" value="SS" />
            | 3B <input type="checkbox" name="player[1][third]" value="3B" /> |</p>

            <p>Player 02: <input type="text" name="player[2][name]" value="" />
            | P <input type="checkbox" name="player[2][pitch]" value="P" />
            | C <input type="checkbox" name="player[2][catch]" value="C" />
            ...

You will obtain something like that:

Array
(
    [player] => Array
    (
        [1] => Array
        (
            [name] => Marcel
            [pitch] => P
            [short] => SS
            [third] => 3B
        )
        [2] => Array
        (
            [name] => Auguste
            [catch] => C
            [first] => 1B
        )
        [3] => Array
        (
            [name] => Ulysse
            [catch] => C
            [second] => 2B
        )
    )
)

Then, you can easily access each element of each player:

$players = $_POST['player'];

echo $players[1]['name'] . '<br/>';

or you can construct this kind of array:

foreach($players as $key=>$player) {
    $temp[] = $player['name'];
    unset($player['name']);
    $temp[] = $player;
    $result[] = $temp;
    unset($temp);
}
print_r($result);
like image 181
Casimir et Hippolyte Avatar answered Apr 16 '26 20:04

Casimir et Hippolyte