Question Description
Im trying to learn for my Computer Science class and Im stuck. Can you help?
Exercise 6-1 Create a Song Organizer script that stores songs in a text ?le. 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 shuf?e() function. 1. Create a new document in your text editor. 2. Type the declaration, element, header information, and element. Use the strict DTD and Song Organizer as the content of the element. 3. 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 ((?le_exists("SongOrganizer/songs.txt")) && (?lesize("SongOrganizer/songs.txt") != 0)) { $SongArray = ?le( "SongOrganizer/songs.txt"); switch ($_GET['action']) { } // End of the switch statement } }
5. Add the following code to the body of the switch statement to handle the three options (Sort Ascending, Remove Duplicates, and Shu?e): case 'Remove Duplicates': $SongArray = array_unique( $SongArray); $SongArray = array_values( $SongArray); break; case 'Sort Ascending': sort($SongArray); break; case 'Shuf?e': shuf?e($SongArray); break;
6. Add the following code immediately after the switch state- ment to save the song list after it has been modi?ed: if (count($SongArray)>0) { $NewSongs = implode($SongArray); $SongStore = fopen( "SongOrganizer/songs.txt", "wb"); if ($SongStore === false) echo "There was an error updating the song ?len"; 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']) . "n"; $ExistingSongs = array(); if (?le_exists("SongOrganizer/songs.txt") && ?lesize("SongOrganizer/songs.txt") > 0) { $ExistingSongs = ?le( "SongOrganizer/songs.txt"); } }
8. Add the following if statement immediately after the block Although this where the song ?le data was read into the $ExistingSongs form does not allow for dupli- array. This if statement checks to see if the song name cate entries, entered is already in the song list, and displays a message if you still need the song already exists. to be able to remove if (in_array($SongToAdd, $ExistingSongs)) { them if necessary. There echo "The song you entered already may be other methods of exists!n"; adding songs to the list, echo "Your song was not added to the and the other methods list."; may allow duplicate } entries.
9. Add the following else clause to the preceding if statement. This else clause adds the new song to the song list ?le. 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"; } }
10. 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 ((!?le_exists("SongOrganizer/songs.txt")) || (?lesize("SongOrganizer/songs.txt") == 0)) echo "There are no songs in the list.n"; else { $SongArray = ?le( "SongOrganizer/songs.txt"); echo "n"; foreach ($SongArray as $Song) { echo "n"; echo "" . htmlentities($Song) . ""; echo "n"; } echo "n"; }
11. 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 Shu?e): Sort Song List Remove Duplicate Songs Randomize Song list
12. Next, add the following XHTML code to create a Web form for entering new song names into the song list: Add a New Song Song Name:
13. Save the document as SongOrganizer.php in the Projects directory for Chapter 6 and upload the ?le to the server.
14. Open the SongOrganizer.php ?le in your Web browser by entering the following URL: http:///PHP_ Projects/Chapter.06/Projects/SongOrganizer.php. 15. Close your Web browser window.