Question: - PHP Assignment help - PHP Programming with my SQL # 2 - Please include proper comments for both php and html files and use
- PHP Assignment help - PHP Programming with my SQL # 2
- Please include proper comments for both php and html files and use proper indentation. If you are not sure how to do the assignment, pleaseeeeeeee skip!
Create a Song Organizer script that stores songs in a text file. Include functionality that allows users to view the song list and prevents the same song name from being entered twice. Also, include code that sorts the songs by name, deletes duplicate entries, and randomizes the song list with the shuffle() function.
-
Create a new document in your text editor.
-
Type the declaration, element, header information, and
element. Use the strict DTD and Song Organizer as the content of theelement. -
Add the following XHTML code and script section to the document body:
Song Organizer
4. Add the following code to the script section to handle any
parameters in the URL:
if (isset($_GET['action'])) { if ((file_exists("SongOrganizer/songs.txt")) && (filesize("SongOrganizer/songs.txt") != 0)) { $SongArray = file( "SongOrganizer/songs.txt"); switch ($_GET['action']) { } // End of the switch statement | 370 |
} }
-
Add the following code to the body of the switch statement to handle the three options (Sort Ascending, Remove Duplicates, and Shuffle):
case 'Remove Duplicates': $SongArray = array_unique(
$SongArray); $SongArray = array_values(
$SongArray); break;
case 'Sort Ascending': sort($SongArray);
break; case 'Shuffle':
shuffle($SongArray); break;
-
Add the following code immediately after the switch state- ment to save the song list after it has been modified:
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 ";
else { fwrite($SongStore, $NewSongs); fclose($SongStore);} }
else unlink("SongOrganizer/songs.txt");
7. Add the following code to the end of the script section to handle any data submitted from the Web form:
if (isset($_POST['submit'])) { $SongToAdd = stripslashes( $_POST['SongName']) . " "; $ExistingSongs = array(); if (file_exists("SongOrganizer/songs.txt") } }
&& filesize("SongOrganizer/songs.txt") > 0) { $ExistingSongs = file( "SongOrganizer/songs.txt");
-
Add the following if statement immediately after the block where the song file data was read into the $ExistingSongs array. This if statement checks to see if the song name entered is already in the song list, and displays a message if the song already exists.
if (in_array($SongToAdd, $ExistingSongs)) { echo "The song you entered already
exists!
"; echo "Your song was not added to thelist.
"; -
Add the following else clause to the preceding if statement.
This else clause adds the new song to the song list file.
else { $SongFile = fopen("SongOrganizer/songs.txt", "ab"); if ($SongFile === false)
echo "There was an error saving your message! ";
else { fwrite($SongFile, $SongToAdd); fclose($SongFile); echo "Your song has been added tothe list. ";
} }
-
Add the following code to the end of the script section to dis- play the song list, or a message that there are no songs in the list if the list is empty:
if ((!file_exists("SongOrganizer/songs.txt")) || (filesize("SongOrganizer/songs.txt") == 0)) echo "There are no songs in the
list. ";
else { $SongArray = file("SongOrganizer/songs.txt"); echo "
"; }style=\"background-color:lightgray\"> "; foreach ($SongArray as $Song) {echo "
"; echo " ";" . htmlentities($Song) . "; echo ""
}
echo "
-
Add the following XHTML code immediately after the PHP script section to display hyperlinks for the three functions in the switch statement (Sort Ascending, Remove Duplicates, and Shuffle):
Sort Song List
Remove Duplicate Songs
Randomize Song list
-
Next, add the following XHTML code to create a Web form for entering new song names into the song list:
-
Save the document as SongOrganizer.php in the Projects directory for Chapter 6 and upload the file to the server.
-
Open the SongOrganizer.php file in your Web browser by entering the following URL: http://
/PHP_ Projects/Chapter.06/Projects/SongOrganizer.php . -
Close your Web browser window.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
