Jump to content
xisto Community
Sign in to follow this  
KansukeKojima

Php Random Titles Learn to create neat-o random titles

Recommended Posts

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

<?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

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

 

Notice from rvalkass:

Remember to place code inside code tags.

Share this post


Link to post
Share on other sites

I would like to suggest a modification to the posted 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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

I imagine it would look like this.

<?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.)

Share this post


Link to post
Share on other sites

This is how I would do it:

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

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

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

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.

Share this post


Link to post
Share on other sites

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


<?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?>

Share this post


Link to post
Share on other sites

Thanks for sharing your knowledge. This is a really good discussion. I would like to edit some parts as following.

<?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, "https://www.1and1.com/website-builder; 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 "https://www.1and1.com/website-builder; from the array and put in the HTML coding.

<?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; ?>

Share this post


Link to post
Share on other sites

Hi All,

KansukeKojima way is good for pages that only have one view at a time. Imagine that you provide some user registration form that consist of 4 steps. And you don't want to reload your page every time user has finished some step but you want to quickly show next form just after user click the "next" button.

Server side scripting like PHP is not the solution, so :D ? the solution is client side scripting => JAVASCRIPT.
we can use document.title="STRING TITLE" to change the title of the page.

on scenario above we just need HTML element like div that have "display:none" in it style to hide.

Here the Example:
============

<div id="frm1"><!-- Form 1 --><input type="button" onclick="toggleShow('frm1')" value="Next" /></div><div id="frm2" style="display:none"><!-- Form 2 --><input type="button" onclick="toggleShow('frm2')" value="Next" /></div><div id="frm3" style="display:none"><!-- Form 3 --><input type="button" onclick="toggleShow('frm3')" value="Next" /></div><div id="frm4" style="display:none"><!-- Form 4 --><input type="button" onclick="toggleShow('frm4')" value="Finish" /></div><!-- the JAVSCRIPT --><script language="javascript"> var divs = new Array("frm1", "frm2", "frm3", "frm4"); var title = new Array("Registration Step 1", "Registration Step2", "Registration Step 3", "The Last"); function toggleShow(frm) {  for (var i=0; i<4; i++) {   // show frm and hide others   if (frm == divs[i]) {	document.getElementById(frm).style.display='block'; // this makes the div appear	document.title=title[i]; // changes the page title   } else	document.getElementById(div[i]).style.display='none'; // this will hide the div  } }</script>

Not tested but it should be work. you can use AJAX if you want, so the data can be directly processed by PHP script every step user done.

It's only examle, in real life we should use CSS to manipulate the element style. And for boosting the development i suggest to use javascipt frameword like Prototype, Mootools, etc.

Sorry for my english :P

Share this post


Link to post
Share on other sites

Oh yeah.... I just remembered..... you can also do this with switches... or cases whatever they're called.


$random_message = rand(1, 6);switch ($random_message) {	case 1;		$rand = 'BLA BLA BLA BLA BLA.... ';		break;	case 2;	   $rand = 'BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... ';		break;	case 3;	   $rand = 'BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... ';		break;	case 4;		$rand = 'BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... ';		break;	case 5;		$rand = 'BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... ';		break;	case 6;		$rand = 'BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... BLA BLA BLA BLA BLA.... ';		break;}echo $rand;

Share this post


Link to post
Share on other sites

There are a lot of tutorials on how to randomize things with php.Why do they need so many? Why couldn't someone just post a couple of the functions like random(), and a tutorial on arrays, and let people figure out, "Hey, maybe if I put a random number in an array of strings, I could have it use that as the title, then I would have a random title!"There have been tutorials on having random images, and random titles, and many others, and they're all pretty much exactly the same.To me, it just seems like wasted space, and when you're trying to actually figure something out, you have a lot more code to look through because of all the ways people find to do the same thing.They should only make a new post if it's actually new code, or it's really innovative, and not something someone could easily figure out.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.