Thursday, 30 August 2018

The PHP foreach loop does not pass well as the data in the array

I have a weird issue with my PHP script. I have an array $keys that is defined at the beginning of the script:

$keys = array("name","date","event","location","address","description","link","linkname");

at some point later on I'm looping through the array, trying to print the keys:
foreach ($keys as $key_show) {
    echo ($key_show);
}

And nothing is actually printed. I put a var_dump($keys) before the loop, and it seems the array is still populated with the entries from above at this point in the script. Interestingly, as soon as I put the var_dump there, the keys also showed up in the foreach-loop.
Full script can be see here

From your link
} elseif (isset($_POST['editconfirm'])) {
    ...
    if ($jsonConcerts) {
        echo "<form method=\"POST\" action=\"edit.php\"";
        //var_dump($keys);
        foreach ($keys as $key_show) {
            echo ($key_show. ": ");
            //echo "<input class=\"wide\" name=\"".$key.
            //"\" value=\"".$jsonConcerts[$counter][$key]."\"><br>\n";
        }
        ...

You don't close the form tag, so all $key_show values are treated as attributes of the form tag and thus never show up in your html output.
If you run this script on the command line, you will see the array values with or without the var_dump().

0 comments:

Post a Comment