Jul 26, 2008

Php Random Titles - Learn to create neat-o random titles!

Free Web Hosting, No Ads > CONTRIBUTE > Tutorials
Pages: 1, 2

free web hosting

Php Random Titles - Learn to create neat-o random titles!

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

Notice from rvalkass:

Remember to place code inside code tags.

 

 

 


Reply

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

Reply

suberatu
Always nice to learn little tricks like this.

QUOTE(truefusion @ Dec 15 2007, 04:56 AM) *
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).

Reply

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

Reply

delivi
cool code snippet , very useful for site homepages

Reply

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

 

 

 


Reply

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

Reply

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

?>



Reply

suberatu
Thank you guys for the responses. It's nice to have little neat scripts like these on hand.

Reply

c0kr3x
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 huh.gif ? 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:
============

CODE
<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 -->
&lt;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 smile.gif

Reply

Latest Entries

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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Pages: 1, 2

Searching Video's for php, random, titles, learn, create, neat, o, random, titles
advertisement



Php Random Titles - Learn to create neat-o random titles!



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE