Martha's CIS 52 Lab #2 Number Exercise #4

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:
Size of Song File: 4
There are 73812 lines in song_file.txt
There are now 73851 lines in song_file.txt
array(4) { [0]=> string(5) "song1" [1]=> string(5) "song2" [2]=> string(5) "song3" [3]=> string(5) "song4" }
After new song added:
array(5) { [0]=> string(5) "song1" [1]=> string(5) "song2" [2]=> string(5) "song3" [3]=> string(5) "song4" [4]=> string(5) "song5" }
Print of songFile keys and values:
Array ( [1] => song1 [value] => song1 [0] => 0 [key] => 0 ) Array ( [1] => song2 [value] => song2 [0] => 1 [key] => 1 ) Array ( [1] => song3 [value] => song3 [0] => 2 [key] => 2 ) Array ( [1] => song4 [value] => song4 [0] => 3 [key] => 3 ) Array ( [1] => song5 [value] => song5 [0] => 4 [key] => 4 )
Song file sorted:
Array ( [0] => song1 [1] => song2 [2] => song3 [3] => song4 [4] => song5 )
Song file shuffled
Array ( [0] => song4 [1] => song3 [2] => song2 [3] => song5 [4] => song1 )