Log in

View Full Version : Can I do this with PHP?



llitos
03-29-2007, 05:24 AM
Hi, :)
I have a website which has over 150 pages, now presently I currently go through and change every single page:confused: I just learned something about PHP... include() and I think this helps me change the whole layout of my page editing only a some few files. However, I have a "Lyrics" site :cool: , but it's getting really hard to work on it because everytime I have a new lyric, I have to create a whole new page for it.

Is there a way that I can have a something like a template lyric page and then when I click on any lyric this page changes the lyric and shows the new lyric I clicked??

I cannot find a total newbie tutorial on how to do this. I have bits and pieces that I've put together but it's all getting confusing. I've tried posting all over the web but I don't get the right answers back.

I would really, really appreciate if someone could point me in the right direction. I am solid with html but nothing else. PLEASE HELP

codeexploiter
03-29-2007, 05:40 AM
You can do a more simple site maintenance using a template based layout system. If you are willing you can use any freely available php based CMS packages like Joomla, Mambo, etc for this purpose.

check this page (http://www.ibdhost.com/help/templates/).

kosi
03-29-2007, 06:10 AM
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

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

$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

$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:


include("$dir/$lyrics");

with this


$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:


$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.

llitos
03-29-2007, 06:39 AM
To Codexploiter: Thanks for Joomla, I think I'll give it a try... It's late now, so I guess I'll try it tomorrow.


To Kosi: Thank you very very much for all this information, I really appreciate all you did for me :cool: !
I'm new to all this PHP, so I'll study your code and try to use it. I guess I'll go as you said, with the step 3 looks better for me. check out my site www.arklyrics.110mb.com I started on Feb I did something for somedays but then I stopped because it was pointless to make Thousands of new pages for every lyric.

I'll try all your code and see if I make it work! :D ThanKs!