Php Random Titles - Learn to create neat-o random titles!
KansukeKojima
Dec 15 2007, 08:52 AM
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!";
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.
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.
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.)
<?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!";
$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!";
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...
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.
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 ? 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.
<!-- 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.
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.
PHP - Randomize Web Title with the rand function... Define the variables numbers we start at number
0 CODE <?php $title[0] = "Web title 0"; // Title 0
$title[1] = "Web title 1"; // Title 1 $title[2] = "Web title
2"; // Title 2 $title[3] = "Web title 3"; // Title 3
$title[4] = "Web title 4"; // Title 4 ?> now we use the rand()
function to get a randomize the web title. rand(0, 4) means we start at 0 and end at 4 i can get
0,1,2,3,4 one of those. ....
Here's a little random quote script that you can use to randomly choose from an array of quotes
you provide. It's very easy to use on any page as it's an external script, you just call it
from your page. I wouldn't use it for any more than about 50 quotes though, it would probably
slow your site down too much. Here's the code: var ar = new Array(44) ar = "Do not meddle in
the affairs of wizards, for they are subtle and quick to anger." ar = "Faithless is he that says
farewell when the road darkens." ar = "I cordially dislike allegory in all its ....
Looking for php, random, titles, learn, create, neat, o, random, titles
Searching Video's for php, random, titles, learn, create, neat, o, random, titles
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.