Reply
How to generate random name from list ?
Old 10-08-2004, 02:00 PM How to generate random name from list ?
Junior Talker

Posts: 1
Hi

we've got a lists of names and we want to automatically randomly display one of the names each time a web page is accessed.

at the moment we cut and paste from the word doc and insert the name in the page for the day but we want to make it automatic so that each time a visitor access that page one of the names is dislayed.

There are not many names in each list - about 50 so I can put them in a spreadsheet or whatever but we do want to add or edit the list in the future.

Any ideas of the easiest way to do this would be appreciated.


thanks
robathandr is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 10-09-2004, 10:21 AM
KeithMcL's Avatar
Super Talker

Posts: 128
Location: Dublin, Ireland
You could store each name in a row in mysql and then use this code to randomly select one row each time:
PHP Code:
SELECT FROM tbl_name ORDER BY RAND() LIMIT 1
KeithMcL is offline
Reply With Quote
View Public Profile Visit KeithMcL's homepage!
 
Old 10-09-2004, 02:07 PM
ejumblesale's Avatar
Extreme Talker

Posts: 240
Location: Kent
You could make a .JS file with a array containing the names ie:

var names = new Array("Henry","Bill","Fred");

function rand(num) {
return Math.floor(Math.random() * num)+ 1; /*returns a random number between 1 and the number it is given*/
}

document.write("<p>"+names[rand(2)]+"</p>");

You could insert the script straight in the page or in a seperate .js file and it will randomly select a name each time the page loads.

This requires no server side scripting and is very simple.

I have made a page simply demonstrating how the script works: http://www.ejumblesale.net/randomnames.htm

reload the page to change the name
__________________
ejumblesale.net - The Internet Jumble Sale,
designanthony - Personal space containing interesting articles and information on downloadable scripts
ejumblesale associates - Free link exchange directory and PPC advertising scheme
ejumblesale forums - The ejumblesale forums

Last edited by ejumblesale : 10-09-2004 at 02:14 PM.
ejumblesale is offline
Reply With Quote
View Public Profile Visit ejumblesale's homepage!
 
Old 10-09-2004, 06:32 PM
HitRaj47's Avatar
Extreme Talker

Posts: 177
Location: GA
put this in a .js file:

function randomisation(){
var randomad_display=new Array()
randomad_display[1]='random name 1'
randomad_display[2]='random name 2'
randomad_display[3]='name 3'
randomad_display[4]='and so on'


var ry=Math.floor(Math.random()*randomad_display.lengt h)
if (ry==0)
ry=1
document.write(randomad_display[ry])
}
randomisation()



replace the text between the single quotes (') ... remember to increment the values (the numbers between the [ ] each time u add a new one

then put the followig html in your page u want it to appear on:

<script language="javascript" type="text/javascript" src="random.js"></script>
HitRaj47 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to generate random name from list ?
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12857 seconds with 12 queries