Web Development Exercise 6-1
Question Description
Exercise 6-1 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. 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 ((file_exists("SongOrganizer/songs.txt")) && (filesize("SongOrganizer/songs.txt") != 0)) { $SongArray = file( "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 Shuffle): case 'Remove Duplicates': $SongArray = array_unique( $SongArray); $SongArray = array_values( $SongArray); break; case 'Sort Ascending': sort($SongArray); break; case 'Shuffle': shuffle($SongArray); break; 6. 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\n"; 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 (file_exists("SongOrganizer/songs.txt") 371 && filesize("SongOrganizer/songs.txt") > 0) { $ExistingSongs = file( "SongOrganizer/songs.txt"); } } 8. Add the following if statement immediately after the block Although this where the song file 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 file. 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 ((!file_exists("SongOrganizer/songs.txt")) || (filesize("SongOrganizer/songs.txt") == 0)) echo "There are no songs in the list.\n"; else { $SongArray = file( "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 Shuffle): 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 file to the server.14. Open the SongOrganizer.php file in your Web browser by entering the following URL: http:///PHP_ Projects/Chapter.06/Projects/SongOrganizer.php. 15. Close your Web browser window.
This question has not been answered.
Create a free account to get help with this and any other question!
Brown University
1271 Tutors
California Institute of Technology
2131 Tutors
Carnegie Mellon University
982 Tutors
Columbia University
1256 Tutors
Dartmouth University
2113 Tutors
Emory University
2279 Tutors
Harvard University
599 Tutors
Massachusetts Institute of Technology
2319 Tutors
New York University
1645 Tutors
Notre Dam University
1911 Tutors
Oklahoma University
2122 Tutors
Pennsylvania State University
932 Tutors
Princeton University
1211 Tutors
Stanford University
983 Tutors
University of California
1282 Tutors
Oxford University
123 Tutors
Yale University
2325 Tutors