来自多维数组的$ _GET值?

时间:2019-01-12 19:21:25

标签: php html arrays multidimensional-array

在下面的代码中,我试图单击时分别更改每个图像。我已经通过在每个图像上使用超链接来开始此操作,单击该链接时应更改为第二个图像,其数组为['name'] ['1']。但是,当前代码仅会针对图像['name'] ['0']循环,因为我无法弄清楚如何测试是否已单击图像。

我已经发现,如果我能够以某种方式将$ _GET值用于每个数组的变量,我可以测试每个变量是['0']还是['1'],然后为循环创建一个函数以将每个数字设置为0或1,具体取决于是否单击了图像。

<head>
    <style>
        .floatleft {
            float: left;
        }
        .floatright {
            float: right;
        }
        div {
            width: 500px;
        }
    </style>
</head>

<body>
    <center>
        <?php

        $url = $_SERVER['REQUEST_URI'];

        //create arrays, one for left and right column when not clicked
        $leftheroes['wolverine']['0'] = "<a href='$url?leftheroes[wolverine][0]=leftheroes[wolverine][1]'><img src='images/superheroes/wolverine.png' width='150px' height='200px'></a>";
        $leftheroes['spiderman']['0'] = "<a href='$url&?spiderman=1'><img src='images/superheroes/spiderman.png' width='150px' height='200px'></a>";
        $leftheroes['ironman']['0'] = "<a href='$url&?ironman=1'><img src='images/superheroes/ironman.png' width='150px' height='200px'></a>";
        $leftheroes['gambit']['0'] = "<a href='$url&?gambit=1'><img src='images/superheroes/gambit.png' width='150px' height='200px'></a>";
        $leftheroes['hawkeye']['0'] = "<a href='$url&hawkeye=1'><img src='images/superheroes/hawkeye.png' width='150px' height='200px'></a>";
        $leftheroes['daredevil']['0'] = "<a href='$url&?daredevil=1'><img src='images/superheroes/daredevil.png' width='150px' height='200px'></a>";
        $lheroes = array("wolverine","spiderman","ironman","gambit","hawkeye","daredevil");

        $rightheroes['thor']['0'] = "<a href='$url&?thor=1'><img src='images/superheroes/thor.png' width='150px' height='200px'></a>";
        $rightheroes['hulk']['0'] = "<a href='$url&?hulk=1'><img src='images/superheroes/hulk.png' width='150px' height='200px'></a>";
        $rightheroes['captainamerica']['0'] = "<a href='$url&?captainamerica=1'><img src='images/superheroes/captainamerica.png' width='150px' height='200px'></a>";
        $rightheroes['bucky']['0'] = "<a href='$url&?bucky=1'><img src='images/superheroes/bucky.png' width='150px' height='200px'></a>";
        $rightheroes['punisher']['0'] = "<a href='$url&?punisher=1'><img src='images/superheroes/punisher.png' width='150px' height='200px'></a>";
        $rightheroes['doctorstrange']['0'] = "<a href='$url&?doctorstrange=1'><img src='images/superheroes/doctorstrange.png' width='150px' height='200px'></a>";
        $rheroes = array("thor","hulk","captainamerica","bucky","punisher","doctorstrange");

        // create arrays for left and right columns when clicked
        $leftheroes['wolverine']['1'] = "<img src='images/superheroes/wolverine.png' width='50px' height='100px'></a>";
        echo "<div>";

        //create left table for team 1
        echo "<table border='1' class='floatleft'>";
            echo "<tr>";
                echo "<th>Team 1</th>";
            echo "</tr>";

            foreach ($lheroes as $x) {
                echo "<tr>";
                    echo "<td>";
                        echo $leftheroes[$x]['0'];
                    echo "</td>";
                echo "</tr>";
            }
        echo "</table>";

        //create right table for team 2
        echo "<table border='1' class='floatright'>";
        echo "<tr>";
            echo "<th>Team 2</th>";
        echo "</tr>";

        foreach ($rheroes as $y) {
                echo "<tr>";
                    echo "<td>";
                        echo $rightheroes[$y]['0'];
                    echo "</td>";
                echo "</tr>";
            }
        echo "</table>";

        echo "</div>";
        ?>
    </center>
</body>

如果点击图片,我该如何做?可以像我说的那样用$ _GET做到吗?

0 个答案:

没有答案