如何打印用户选择的多维数组?

时间:2017-03-14 15:43:19

标签: php html arrays multidimensional-array

3个选项是名称,击球顺序和位置。用户必须选择他/她想要对值进行排序的选项。目前,它打印已排序的击球顺序值,因为它是我调用回声之前的最后一个排序顺序。当用户选择一个选项时,如何才能这样做,只显示该选项的输出?没有选择什么时没有输出?

这是HTML

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="Assignment.css">
    </head>
    <body>
        <main>
            <form action="Assignment1.php" method="post">
        <h2>Rays Lineup Display</h2>
        <div id="data">

            <p>Select Size</p>
            <input type="radio" name="order" value="player_name">Player Name<br>
            <input type="radio" name="order" value="pos">Position<br>
            <input type="radio" name="order" value="bats">Batting Order<br>

          </div>

            <div id="buttons">
                <label>&nbsp;</label>
                <input type="submit" value="Submit"><br>
            </div>
            </form>
        </main>
    </body>
</html>

PHP文件

    <!DOCTYPE html>
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="Assignment.css">
        </head>
        <body>                    

         <?php

            $selection_list = array('pos', 'player_name', 'bats');
            $players = array (
            array('pos' => 'SS', 'player_name' => 'Beckham', 'bats' => 1),
            array('pos' => 'C', 'player_name' => 'Casali', 'bats' => 8),
            array('pos' => '2B', 'player_name' => 'Forsythe', 'bats' => 3),
            array('pos' => '1B', 'player_name' => 'Loney', 'bats' => 7),
            array('pos' => 'LF', 'player_name' => 'Jennings', 'bats' => 2),
            array('pos' => 'CF', 'player_name' => 'KierMaier', 'bats' => 9),
            array('pos' => '3B', 'player_name' => 'Longoria', 'bats' => 4),
            array('pos' => 'RF', 'player_name' => 'Mahtook', 'bats' => 5),
            array('pos' => 'DH', 'player_name' => 'Souza', 'bats' => 6),
            );


  function getSortFunction( $sortKey ) {
  return function( $personA, $personB ) use ( $sortKey ) {
    return ( $personA[$sortKey] < $personB[$sortKey] ) ? -1 : 1;
  };
}



 $order = filter_input(INPUT_POST,'order');
            usort( $players, getSortFunction( 'pos' ) );
            usort( $players, getSortFunction( 'player_name' ) );
             usort( $players, getSortFunction( 'bats' ) );
            echo '<pre>'; print_r($players); echo '<pre/>';
?>

</body>

1 个答案:

答案 0 :(得分:0)

很明显,它会全部显示出来。 首先你应该使用。$ _ POST [order] 然后是每个案例的开关或If语句

编辑:

switch($order){
case "pos":
    usort( $players, getSortFunction( 'pos' ) );
    echo '<pre>'; print_r($players); echo '<pre/>';
    break;
case "player_name":
    usort( $players, getSortFunction( 'player_name' ) );
    echo '<pre>'; print_r($players); echo '<pre/>';
    break;
case "bats":
    usort( $players, getSortFunction( 'bats' ) );
    echo '<pre>'; print_r($players); echo '<pre/>';
    break;
case "";
    break;
}