Web Development Exercise 6-1

 Exercise 6-1_x000D_
Create a Song Organizer script that stores songs in a text file. Include_x000D_
functionality that allows users to view the song list and prevents the_x000D_
same song name from being entered twice. Also, include code that_x000D_
sorts the songs by name, deletes duplicate entries, and randomizes_x000D_
the song list with the shuffle() function._x000D_
   1.   Create a new document in your text editor._x000D_
   2.   Type the  declaration,  element, header_x000D_
        information, and  element. Use the strict DTD and_x000D_
        “Song Organizer” as the content of the  element._x000D_
   3.   Add the following XHTML code and script section to the_x000D_
        document body:_x000D_
        Song Organizer_x000D_
        
4.   Add the following code to the script section to handle any_x000D_
                        parameters in the URL:_x000D_
                        if (isset($_GET['action'])) {_x000D_
                             if ((file_exists("SongOrganizer/songs.txt"))_x000D_
                                  && (filesize("SongOrganizer/songs.txt")_x000D_
                                  != 0)) {_x000D_
                              $SongArray = file(_x000D_
                                       "SongOrganizer/songs.txt");_x000D_
                                  switch ($_GET['action']) {_x000D_
                                  } // End of the switch statement_x000D_
                             }_x000D_
                        }
5.   Add the following code to the body of the switch statement_x000D_
                        to handle the three options (Sort Ascending, Remove_x000D_
                        Duplicates, and Shuffle):_x000D_
                        case 'Remove Duplicates':_x000D_
                             $SongArray = array_unique(_x000D_
                                  $SongArray);_x000D_
                             $SongArray = array_values(_x000D_
                                  $SongArray);_x000D_
                             break;_x000D_
                        case 'Sort Ascending':_x000D_
                             sort($SongArray);_x000D_
                             break;_x000D_
                        case 'Shuffle':_x000D_
                             shuffle($SongArray);_x000D_
                             break;
6.   Add the following code immediately after the switch state-_x000D_
                        ment to save the song list after it has been modified:_x000D_
                        if (count($SongArray)>0) {_x000D_
                             $NewSongs = implode($SongArray);_x000D_
                             $SongStore = fopen(_x000D_
                                  "SongOrganizer/songs.txt",_x000D_
                                  "wb");_x000D_
                             if ($SongStore === false)_x000D_
                                  echo "There was an error_x000D_
                                       updating the song filen";_x000D_
                             else {_x000D_
                                  fwrite($SongStore, $NewSongs);_x000D_
                                  fclose($SongStore);_x000D_
                             }_x000D_
                        }_x000D_
                        else_x000D_
                             unlink("SongOrganizer/songs.txt");
7.   Add the following code to the end of the script section to_x000D_
     handle any data submitted from the Web form:_x000D_
     if (isset($_POST['submit'])) {_x000D_
          $SongToAdd = stripslashes(_x000D_
               $_POST['SongName']) . "n";_x000D_
          $ExistingSongs = array();_x000D_
          if (file_exists("SongOrganizer/songs.txt")                                                  _x000D_
               && filesize("SongOrganizer/songs.txt")_x000D_
               > 0) {_x000D_
               $ExistingSongs = file(_x000D_
                    "SongOrganizer/songs.txt");_x000D_
          }_x000D_
     }
8.   Add the following if statement immediately after the block                   Although this_x000D_
     where the song file data was read into the $ExistingSongs                     form does not_x000D_
                                                                                  allow for dupli-_x000D_
     array. This if statement checks to see if the song name_x000D_
                                                                                  cate entries,_x000D_
     entered is already in the song list, and displays a message if               you still need_x000D_
     the song already exists.                                         to be able to remove_x000D_
     if (in_array($SongToAdd, $ExistingSongs)) {                      them if necessary. There_x000D_
          echo "The song you entered already                       may be other methods of_x000D_
               exists!n";                                      adding songs to the list,_x000D_
          echo "Your song was not added to the                        and the other methods_x000D_
               list.";                                            may allow duplicate_x000D_
     }                                                                entries.
9.   Add the following else clause to the preceding if statement._x000D_
     This else clause adds the new song to the song list file._x000D_
     else {_x000D_
          $SongFile = fopen(_x000D_
               "SongOrganizer/songs.txt", "ab");_x000D_
          if ($SongFile === false)_x000D_
               echo "There was an error saving_x000D_
                    your message!n";_x000D_
          else {_x000D_
               fwrite($SongFile, $SongToAdd);_x000D_
               fclose($SongFile);_x000D_
               echo "Your song has been added to_x000D_
                    the list.n";_x000D_
          }_x000D_
     }_x000D_
                    
10. Add the following code to the end of the script section to dis-_x000D_
                      play the song list, or a message that there are no songs in the_x000D_
                      list if the list is empty:_x000D_
                       if ((!file_exists("SongOrganizer/songs.txt"))_x000D_
                            || (filesize("SongOrganizer/songs.txt")_x000D_
                            == 0))_x000D_
                       echo "There are no songs in the_x000D_
                                 list.n";_x000D_
                       else {_x000D_
                            $SongArray = file(_x000D_
                                 "SongOrganizer/songs.txt");_x000D_
                            echo "n";_x000D_
                            foreach ($SongArray as $Song) {_x000D_
                                 echo "n";_x000D_
                                 echo "" . htmlentities($Song) ._x000D_
                                      "";_x000D_
                                 echo "n";_x000D_
                            }_x000D_
                            echo "n";_x000D_
                       }
11. Add the following XHTML code immediately after the PHP_x000D_
                      script section to display hyperlinks for the three functions in_x000D_
                      the switch statement (Sort Ascending, Remove Duplicates,_x000D_
                      and Shuffle):_x000D_
                       _x000D_
                       _x000D_
                            Sort Song List_x000D_
                       _x000D_
                            Remove Duplicate Songs_x000D_
                       _x000D_
                            Randomize Song list
12. Next, add the following XHTML code to create a Web form_x000D_
                      for entering new song names into the song list:_x000D_
                       _x000D_
                       Add a New Song_x000D_
                       Song Name: 
13. Save the document as SongOrganizer.php in the Projects_x000D_
                      directory for Chapter 6 and upload the file to the server.
14. Open the SongOrganizer.php file in your Web browser by_x000D_
      entering the following URL: http:///PHP__x000D_
      Projects/Chapter.06/Projects/SongOrganizer.php._x000D_
  15. Close your Web browser window.

"Is this question part of your assignment? We can help"

ORDER NOW