Lab #2 Exercise #4. Create a Song Organizer script that stores songs in a text file. Print a summary that includes the number of lines in the file and the file's size. Append another song in the text file. Include functionality that allows users to view the song lists and prevents the same song name from being entered twice. Also, include code that sorts the songs by name and randomizes the song list with the shuffle( ) function.
**************************************************************
CODES:
<?php
// Create a Song Organizer script that stores songs in a text file.
$songFile = array("song1", "song2", "song3", "song4");
// Print a summary that includes the number of lines in the file and the file's size.
echo "Song summary:<br />";
echo "Size of Song File: ".sizeof($songFile);
echo "<br />";
// ADAPTED FROM: http://www.totallyphp.co.uk/code/count_the_number_of_lines_in_a_text_file.htm
$file = "song_file.txt";
$lines = count(file($file));
echo "There are $lines lines in $file<br />";
// ADAPTED FROM: http://www.tizag.com/phpT/fileappend.php
$myFile = "song_file.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$song3 = "<pre>
Au clair de la lune
Chanson enfantine
(French)
Au clair de la lune
Mon ami Pierrot
Prête-moi ta plume*
Pour écrire un mot
Ma chandelle est morte
Je n'ai plus de feu
Ouvre-moi ta porte
Pour l'amour de Dieu
Au clair de la lune
Pierrot répondit
Je n'ai pas de plume
Je suis dans mon lit
Va chez la voisine
Je crois qu'elle y est
Car dans sa cuisine
On bat le briquet
Au clair de la lune
L'aimable Lubin
Frappe chez la brune
Qui répond soudain
Qui frapp' de la sorte
Il dit à son tour
Ouvrez votre porte
Au dieu de l'amour
Au clair de la lune
On n'y voit qu'un peu
On chercha la plume
On chercha du feu
En cherchant d' la sorte
Je n' sais c' qu'on trouva
Mais je sais qu' la porte
Sur eux se ferma</pre>";
// HELP! This overwrote/removed the other song lyrics...
fwrite($fh, $song3);
fclose($fh);
$lines = count(file($file));
echo "There are now $lines lines in $file<br />";
var_dump($songFile);
// Append another song in the text file.
$songFile[] = "song5";
echo "<br />After new song added:<br />";
var_dump($songFile);
//Include functionality that allows users to view the song lists and
// each($songFile);
echo "<br />Print of songFile keys and values:<br />";
// prevents the same song name from being entered twice.
foreach($songFile as $key => $value) {
print_r(each($songFile));
}
// Also, include code that sorts the songs by name
sort($songFile);
echo "<br />Song file sorted: <br />";
print_r($songFile);
echo "<br />";
// and randomizes the song list with the shuffle( ) function.
shuffle($songFile);
echo "Song file shuffled<br />";
print_r($songFile);
?>
**************************************************************
ANSWER:
Song summary: