|
|
|
|
![]() ![]() |
Dec 15 2007, 08:52 AM
Post
#1
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 525 Joined: 13-October 06 From: Alberta, Canada Member No.: 31,584 |
PHP Random Titles
Description Ever wondered how some websites (ex:spoono.com) have those cool-o random titles appearing in the title bar? Now you get to learn how to do it using PHP. We will be using our knowledge of Variables and Echos in this tutorial. Try It Out Creating the code itself is fairly easy. To start, lets create some of our different titles. Code CODE <?php $title[1] = "This will be more fun that that time - never mind..."; $title[2] = "Another random title... neat eh?"; $title[3] = "I've got wood... in my fire place..."; $title[4] = "More ranom titles? Cool!"; ?> Great, you just created all of your titles. However, this alone does absolutely nothing at the moment. Next, lets create the whole random part of it now. Code CODE <?php $title[1] = "This will be more fun that that time - never mind..."; $title[2] = "Another random title... neat eh?"; $title[3] = "I've got wood... in my fire place..."; $title[4] = "More ranom titles? Cool!"; $random_select = rand(1, 4); echo $title[$random_select]; ?> See how it works? The variable $random_select selects a random number. In the echo, you type the $title variable and then the $random_select variable in the brackets. In short, every time the page loads, it is told to randomly output $title[1], or $title[2], etc. Don't get it yet? Read it over a few times, you'll eventually get the hang of it. |
|
|
|
Dec 15 2007, 09:56 AM
Post
#2
|
|
|
Ephesians 6:10-17 ![]() Group: [MODERATOR] Posts: 1,868 Joined: 22-June 05 From: The World of Gentoo Member No.: 8,528 ![]() |
I would like to suggest a modification to the posted code:
CODE <?php $title[] = "This will be more fun that that time - never mind..."; $title[] = "Another random title... neat eh?"; $title[] = "I've got wood... in my fire place..."; $title[] = "More ranom titles? Cool!"; echo $title[array_rand($title)]; ?> Also, it is best if this is not used for the TITLE element in HTML. I hear random page titles decrease your ranking with search engines. |
|
|
|
Dec 18 2007, 01:51 AM
Post
#3
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 132 Joined: 23-September 07 Member No.: 50,511 |
Always nice to learn little tricks like this.
Also, it is best if this is not used for the TITLE element in HTML. I hear random page titles decrease your ranking with search engines. Wow, I never knew that. Thanks for the heads-up. Question: How could you modify this to display random images instead of text (obviously inside page, not title). |
|
|
|
Dec 18 2007, 11:40 PM
Post
#4
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 525 Joined: 13-October 06 From: Alberta, Canada Member No.: 31,584 |
I imagine it would look like this.
CODE <?php $title[1] = "<img src="folder1/folder2/image1">"; $title[2] = "<img src="folder1/folder2/image2">"; $title[3] = "<img src="folder1/folder2/image3">"; $random_select = rand(1, 3); $content = <<<html <html> <body> $title[$random_select] </body> </html> html; echo $content ?> At least thats how I would do it... and if it would work... (I tend to code my entire page into one html tag.... but the basic theories there.... make your own modification to suit your needs.) |
|
|
|
Dec 19 2007, 02:43 PM
Post
#5
|
|
|
Trap Grand Marshal Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 1,300 Joined: 11-January 06 From: Chennai, India Member No.: 16,932 |
cool code snippet , very useful for site homepages
|
|
|
|
Dec 19 2007, 03:27 PM
Post
#6
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 111 Joined: 29-September 07 From: United Kingdom Member No.: 50,853 |
This is how I would do it:
CODE <?php $title[1] = "This will be more fun that that time - never mind..."; $title[2] = "Another random title... neat eh?"; $title[3] = "I've got wood... in my fire place..."; $title[4] = "More ranom titles? Cool!"; $random_select = rand(1, 4); $randomtitle = $title[$random_select]; print("<html><head><title>"); print($randomtitle); print("</title></head><body>content</body></html>"); ?> Broken down: CODE $title[1] = "This will be more fun that that time - never mind..."; $title[2] = "Another random title... neat eh?"; $title[3] = "I've got wood... in my fire place..."; $title[4] = "More ranom titles? Cool!"; The variables. In this case, the random titles. CODE $random_select = rand(1, 4); $randomtitle = $title[$random_select]; The random_select gets the random title. The randomtitle also generates the random title but is simplified to use with one variable. CODE print("<html><head><title>"); print($randomtitle); print("</title></head><body>content</body></html>"); I chose to use the print function but I guess echo would work. The rest is pretty self-explanatory. |
|
|
|
Dec 19 2007, 11:24 PM
Post
#7
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 525 Joined: 13-October 06 From: Alberta, Canada Member No.: 31,584 |
Oops... in the random image part. I tested it, doesn't work, here is the fix. The error was that I use the " character for the variables when I should have used the ' character...
CODE <?php
$title[1] = '<img src="folder1/folder2/image1">'; $title[2] = '<img src="folder1/folder2/image2">'; $title[3] = '<img src="folder1/folder2/image3">'; $random_select = rand(1, 3); $content = <<<html <html> <body> $title[$random_select] </body> </html> html; echo $content ?> |
|
|
|
Dec 20 2007, 05:59 AM
Post
#8
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 23 Joined: 7-November 07 Member No.: 52,597 |
Thanks for sharing your knowledge. This is a really good discussion. I would like to edit some parts as following.
CODE <?php $image[1] = 'http://mywebsite.com/pic/1.JPG'; $image[2] = 'http://mywebsite.com/pic/2.JPG'; $image[3] = 'http://mywebsite.com/pic/3.JPG'; $random_select = rand(1, 3); $content = <<<html <html> <body> <img src="$image[$random_select]"/> </body> </html> html; echo $content; ?> I do not put <img.....> coding in the array because I think it is kind of repeating. I think line of code and words are count for a total file size. Moreover, one of my teachers taught me to use One and Only One principle. For example, it is going to use repeatedly, we should put it in the function and call that function instead of repeating those same coding again and again. In this example, "http://mywebsite.com/pic/" is repeatedly used in array again. If you are going to create this random of pictures from the same source like above. As long as you are ensure you can remove "http://mywebsite.com/pic/" from the array and put in the HTML coding. CODE <?php $image[1] = '1.JPG'; $image[2] = '2.GIF'; $image[3] = '3.JPG'; $random_select = rand(1, 3); $content = <<<html <html> <body> <img src="http://mywebsite.com/pic/$image[$random_select]"/> </body> </html> html; echo $content; ?> |
|
|
|
|
|