Search the Community
Showing results for tags 'switch statement'.
-
GOAL: echo switch statement case which maps/matches to 'postType' PROBLEM: Regardless of what I try, the $_POST data seems to vanish, thus the switch statements default case is triggered and the form shows again instead of the intended/desired echo statement that I hope to show based on the postType contained within the $filter variable. I am working in PHP 5.4 A var_dump returns: array(2) { ["postType"]=> NULL ["myContent"]=> string(4) "dsfs" } I believe that I have both the form and the switch statement syntaxs/formatting correct. My problem, i think is somewhere in how the data is or isn't being passed to the switch via the filter_input_array(INPUT_POST, [...]) my humble code stub: error_reporting(E_ALL | E_STRICT); define('THIS_PAGE',basename($_SERVER['PHP_SELF'])); //test data //$_POST = [ 'postType' => 'myPlayby', 'myContent' => 'Anna King', ]; //build the array of data created by from for switch checks $filter = filter_input_array(INPUT_POST, [ 'postType' => [], 'myContent' => [], ]); var_dump($filter); echo "<br /><br />"; //$search = false; switch ($filter['postType']) { case 'myChar': echo "Character Check: " . $filter['myCharacter']; $search = $filter['myCharacter']; break; //test data ought to trigger myPlayby case showing 'Playby check: Anna King' here... frack case 'myPlayby': echo "Playbe Check: " . $filter['myPlayby']; $search = $filter['myPlayby']; break; case 'myFoobar': echo "Foo Check: " . $filter['myFooBar']; $search = $filter['myFooBar']; break; default: echo '<form type="submit" method="post" action="' . THIS_PAGE . '"; > <select name="postTypes"> <option default disable>-------------</option> <option value="myChar">Character</option> <option value="myPlayby">Playby</option> <option value="myFooBar">Foo</option> </select> <input type="text" value="" name="myContent"/> <input type="submit" name="submit" value="Submit"> </form>' ; break; } This is my first time working with filter_input_array to handle $_POST data, and after a day of trial, error, research and effort which include looking at Lynda.com, searching Google and reading what relevant posts I could find on StackOverflow, php.net, etc, I am flummoxed.test-switch-stack.php
- 1 reply
-
- postforms
- switch statement
-
(and 2 more)
Tagged with:
-
I can't figure out what's wrong with this. The program works, but the duplication doesn't work. The error I get is : Notice: Undefined variable: SongToAdd in C:\ITEC315\htdocs\users\ramojumder0\Reinforcement Exercises\Ch. 6\R.E.6-1_SongOrganizer.php on line 64 Notice: Undefined variable: ExistingSongs in C:\ITEC315\htdocs\users\ramojumder0\Reinforcement Exercises\Ch. 6\R.E.6-1_SongOrganizer.php on line64 Warning: in_array() expects parameter 2 to be array, null given in C:\ITEC315\htdocs\users\ramojumder0\Reinforcement Exercises\Ch. 6\R.E.6-1_SongOrganizer.php on line 64 Warning: fopen(SongOrganizer/songs.txt): failed to open stream: No such file or directory in C:\ITEC315\htdocs\users\ramojumder0\Reinforcement Exercises\Ch. 6\R.E.6-1_SongOrganizer.php on line 72 There was an error saving your message! I bolded line 64 below, which is an if (in_array ) statement. line 64 is: if (in_array($SongToAdd, $ExistingSongs)) { echo "<p>The song you entered already exists!<br />\n"; echo "Your song was not added to the list.</p>"; } <?php if (isset($_GET['action'])) { if ((file_exists("SongOrganizer/songs.txt")) && (filesize("SongOrganizer/songs.txt") != 0)) { $SongArray = file("SongOrganizer/songs.txt"); switch ($_GET['action']) { case 'Remove Duplicates': $SongArray = array_unique($SongArray); $SongArray = array_values($SongArray); break; case 'Sort Ascending': sort($SongArray); break; case 'Shuffle': shuffle($SongArray); break; } // end of switch statement if (count($SongArray)>0) { $NewSongs = implode($SongArray); $SongStore = fopen("SongOrganizer/songs.txt", "wb"); if ($SongStore === false) echo "There was an error updating the song file\n"; else { fwrite($SongStore, $NewSongs); fclose($SongStore); } } else unlink("SongOrganizer/songs.txt"); } } if (isset($_POST['submit'])) { $SongToAdd = stripslashes($_POST['SongName']) . "\n"; $ExistingSongs = array(); if (file_exists("SongOrganizer/songs.txt") && filesize("SongOrganizer/songs.txt") > 0) { $ExistingSongs = file("SongOrganizer/songs.txt"); } } if (in_array($SongToAdd, $ExistingSongs)) { echo "<p>The song you entered already exists!<br />\n"; echo "Your song was not added to the list.</p>"; } else { $SongFile = fopen("SongOrganizer/songs.txt", "ab"); if ($SongFile === false) echo "There was an error saving your message!\n"; else { fwrite($SongFile, $SongToAdd); fclose($SongFile); echo "Your song has been added to the list.\n"; } } if ((!file_exists("SongOrganizer/songs.txt")) || (filesize("SongOrganizer/songs.txt") == 0)) echo "<p>There are no songs in the list.</p>\n"; else { $SongArray = file("SongOrganizer/songs.txt"); echo "<table border=\"1\" width=\"100%\" style=\"background-color:lightgray\">\n"; foreach ($SongArray as $Song) { echo "<tr>\n"; echo "<td>" . htmlentities($Song) . "</td>"; echo "</tr>\n"; } echo "</table>"; } ?> <!-- Display hyperlinks for the three functions in the switch statement (Sort Ascending, Remove Duplicates, and Shuffle) --> <p> <a href="SongOrganizer.php?action=Sort%20Ascending">Sort Song List</a><br /> <a href="SongOrganizer.php?action= Remove%20Duplicates">Remove Duplicate Songs</a><br /> <a href="SongOrganizer.php?action=Shuffle"> Randomize Song List</a><br /> </p> <!-- web form for entering new song names into the song list: --> <form action="SongOrganizer.php" method="post"> <p>Add a New Song</p> <p>Song Name: <input type="text" name="SongName" /></p> <p><input type="submit" name="submit" value="Add Song to List" /> <input type="reset" name="reset" value="Reset Song Name" /></p> </form> Any help would be appreciated!
- 3 replies
-
- shuffle()
- switch statement
-
(and 3 more)
Tagged with: