Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The sample code provided is a Song Organizer script that stores songs in a text

ID: 3716081 • Letter: T

Question

The sample code provided is 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, included is code that will sorts the songs by name, deletes duplicate entries, and randomizes the song list with the shuffle() function.


Add detailed comments into the code ( // Comment or /* Comment */) explaining how the code works. Specifically, add comments to the code where comments are required.




Note: Please type up the whole PHP file, add in your comments and save the file as SongOrganizer.php. Upload SongOrganizer.php to the class web server in your module7a directory for grading.



Khtml lang-"en"> head> Music Sorted Khi>Song Organizer

Explanation / Answer

Line no 11 which explains the functionality of the following if statement:

First of all, we are checking if the action variable in the $_GET associative array(in our case, "action" is the key we are looking for in the $_GET array) is set or not; if it is set then we move on to the next if statement and use the file_exists() and filesize() function to check whether the specified file exists or not, it returns true if the file exists otherwise it returns false and filesize() function which returns filesize in bytes if the specifies file exists otherwise it returns false(when the file does not exist). If the file exists and file size is not zero, then we come across line no 14, we use the file() function which reads the file into an array.

Line no 16 which explains the functionality of the following switch:

Now we are using our $_GET['action'] in the switch case which grabs the value of the variable/parameter passed($_GET['action'] in our case) and compares this value with each of its Case and if there is a match, then it executes the code block of that particular Case

Line 18 gets executed when $_GET['action'] is equal to 'Remove Duplicates', then we use the array_unique function which removes duplicate values from an array and then we use the array_value() function which returns all the unique values in the array(this function returns only values not keys in case of an associative array); break is used for preventing the execution of the following Case(s) code blocks

Line no 22; here we use the sort() function and pass our array as the parameter. This function sorts the array in ascending order; functionality of break keyword is already described above

Line no 25; here we use the shuffle() function and pass our array as the parameter. This function randomizes the order of the elements in the array; functionality of break keyword is already described above

Line no 30

count() function is used to check the length of the passed array and if the array length is greater than zero then the code in the if block gets executed. On line no 32, implode() is used which joins the array elements with a space between them(by default) and returns a string; then we open our file using fopen() and passing the file name and the appropriate mode so that the default content is not erased. Then on line no 34, we check whether the file open was successful or not. If not, then we show an appropriate message to the user otherwise we write to the file using fwrite() and then close the file using fclose(). If the count returns 0, then the else block on line 40 gets executed which used unlink() function which deletes the specified file.

Line no 46; first of all we are checking if the submit is set in the $_POST associative array, if it is set then we grab the file name using the $_POST['SongName'] and strip the slashes in it by using the stripslashes() function and then we add a newline character ( " " ) at its end and on line 49 we create an empty array.

On line no 52, check the file characteristics using the file_exists and filesize function which is already explained under the Line no 11 comment; then on line on 53 we are using the file() function to read in the specified file (which contains songs in our case; file() usage is already described above). On line no 55, in_array() is used to check whether the song that we wish to add is already in the list or not. This function returns true if it is already there so we display this message to the user otherwise we specify the code to add this song to the else code block

Else code block => we open our songs list file using fopen() function by specifying file name and mode in which we want to open it(without deleting its content), then again we check that our file is not false(means it is existing) and we write the name to our list using fwrite() and then we close the file and show the message to the user.

Line no 72

In the if condition we check if the file does not exist or its size is zero, and then we use an appropriate message to the user. If the file exists and its size is not zero, then the else code block gets executed in which we use a foreach loop and loop through each of the song names in the list and print it in a tabular format. htmlentities() is also used which converts some characters to HTML entities.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote