There are a number of ways you could do this, actually.
1. You could use a database . . . or at least i think you can. I'm not very good with databasing, and so i'm going to leave this for one of the more experienced php coders to fill in.
2. You could have a very big php file containing all the lyrics you need and call them individually using the if construct:
PHP Code:
<?php
if ($lyrics == "maryhadalittlelamb") {
echo("Mary had a little lamb . . .");
} else if ($lyrics == "baabaablacksheep") {
echo("Baa baa black sheep, have you any wool? . . . ");
} else if ($lyrics == "georgyporgie") {
echo("Georgy Porgie pudding and pie . . . ");
} else {
echo("<a href='".$_SERVER["PHP_SELF"]."?lyrics=maryhadalittlelamb'>Mary Had A Little Lamb</a><br/>");
echo("<a href='".$_SERVER["PHP_SELF"]."?lyrics=baabaablacksheep'>Baa Baa Black Sheep</a><br/>");
echo("<a href='".$_SERVER["PHP_SELF"]."?lyrics=georgyporgie'>Georgy Porgie</a><br/>");
}
?>
3. You can have each of your lyrics stored in a text file and call them individually with the include() function when you need them:
PHP Code:
<?php
$dir = "/path_to_where_all_my_lyrics_are";
if ($lyrics != "") {
include("$dir/$lyrics.txt");
}
/*
The assumption here is that you have some files saved in the directory called
"path_to_where_all_my_lyrics_are" and that the files are named
"maryhadalittlelamb.txt", "baabaablacksheep.txt", and "georgyporgie.txt" and
that these will contain the lyrics to their respective songs.
*/
echo("<a href='".$_SERVER["PHP_SELF"]."?lyrics=maryhadalittlelamb'>Mary Had A Little Lamb</a><br/>");
echo("<a href='".$_SERVER["PHP_SELF"]."?lyrics=baabaablacksheep'>Baa Baa Black Sheep</a><br/>");
echo("<a href='".$_SERVER["PHP_SELF"]."?lyrics=georgyporgie'>Georgy Porgie</a><br/>");
?>
Personally, this would be my method of choice, if only because you could then make life even easier on yourself by reading the text files right out of the directory:
PHP Code:
<?php
$dir = "/path_to_where_all_my_lyrics_are";
if ($lyrics != "") {
include("$dir/$lyrics"); //note the difference: I didn't include the .txt part.
//you'll see why a little later.
}
/* use this if you have php 4 . . . */
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
/* . . . or this if you have php 5 */
$files = scandir($dir);
/* Now I'm going to use the foreach construct to create the list of links */
foreach ($files as $file) {
// This will make sure we only use the .txt files in the directory
if (substr($file, -4) == ".txt") {
// notice that the $file variable i'm including already ends in .txt, which is why i didn't include it earlier.
echo("<a href='".$_SERVER["PHP_SELF"]."?lyrics=$file'>$file</a>");
}
}
?>
Now, to add more lyrics links to the page, you only have to add them to the directory, and they will automatically appear on the page.
If you want, you can even replace this:
PHP Code:
include("$dir/$lyrics");
with this
PHP Code:
$text = file_get_contents("$dir/$lyrics");
echo($text);
because that way you can perform any function you want (such as html_entity_decode() or nl2br() etc) to the text before you print it to the page:
PHP Code:
$text = file_get_contents("$dir/$lyrics");
echo(nl2br($text));
I know that was a lot of information, but i hope it was helpful.
/*********** EDIT *************/
You can include html or htm files that you do with txt files, if you use the include() function. That way, if you already have html files you don't have to change them to txt files. You may have to take out the <html></html>, <head></head> and <body></body> tags though.
Bookmarks