HOME  |  ABOUT US  |  PRODUCTS  | DOWNLOAD  |  HOSTING COUPON  |  TEMPLATE  |  REGISTER | FORUM  | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Turtotal :::
  Basics & Beginner (3)
  Date & Time (1)
  Sessions & Cookies (1)
  Counters & Logging (2)
  Databases (0)
  Email & Mailing Lists (0)
  Membership Systems (0)
  User Authentication (0)
  Security (1)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (2)
  Date & Time (5)
  E-Mail (3)
  Forms (5)
  Guestbooks (1)
  Logging (1)
  Miscellaneous (2)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (4)
  User Authentication (2)
::: Download Center :::
  PHP Version
  Free Ebook
  Online Project
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 23
  PHP Example : 35
  PHP Tutorials : 8
  PHP News : 8
  Total Coupon : 22
  Other Tutorials :
  Member : 34
  Visits : 16469
  Member Online : 47
Home Today :

Random Title Generator using Arrays

I have always wanted to have a simple Random Title Generator for some of my sites because it would be something rather fun. I know of many Javascript-based ones, but it would 1) Give away how I did it and 2) It would not work on some browsers. So, That has always persuaded me not to do it. Until I saw a nameless site touting it and would not give away how it worked. Well, Now I know how it works and now it is my time to share.
First things first, you need to create some titles (about 7 to start, but you can add later on) and they all have to be different. Got them? Good.
Putting Titles into an Array

Now, you have to learn about arrays. Using arrays allows one to list something or create something that would use the arrays to show data using different switches. If already know what arrays are, then you can continue to read, however, if you do not know what arrays are, then read This first.

For this script, I will be using an array called title. This Array is just like an variable, but it has ordered data. So, now that we are ready, lets create our array.

PHP Tutorials :
Please login before you come here

Now, as you can see, I made an array with 7 titles and they are separated by commas. The use of arrays cut down on size, so, instead of having 7 different variables, I have 1 array and less space.
Randomizing it and printing it

Now, we have to use rand() to do the whole random part of the script.

PHP Tutorials :
Please login before you come here

This will now enable the script to choose between 1 and 7 and it will continue to do so every time it is accessed. Sweet huh?

Ok, all we have to do is print the nice new title.

PHP Tutorials :
Please login before you come here

End your PHP page and finish up
Finishing it up
In the title of your page that you want to randomize the title for, put the following code:

PHP Tutorials :
Please login before you come here

And you have a random title. For those of you who want to know more ... the array is a bunch of static info (unless you were to grab some of the info from an SQL database) and the array itself is not dynamic. What makes it dynamic is rand(). It dynamically chooses what number should be printed. From there it is rather easy and it can be finished/printed and it is done.

Thanks for DuaneJeffers



Other Tutorials
PHP Includes
Using If Else Statements

::: PHP Scripts :::
Auctions (1)
Banner Exchange (1)
Calendars (1)
Chat Related (0)
Classified Ads (6)
Content Manage (1)
Counters & Trackers (0)
Customer Support (1)
Games (2)
E-Commerce (2)
Email Related (0)
Knowledgebase (0)
Forum / Boards (2)
Guestbooks (2)
Image Galleries (1)
Miscellaneous (0)
Music Libraries (2)
News Publishing (1)
Polls and Voting (0)
User Authentication (0)
::: Other Tutorials :::

  Powered By T4VN.NET - Version 2.0 - CopyRight @ T4VN.NET 2005-2006