NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Random Rotation Script

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 02-12-2007, 02:58 PM THREAD STARTER               #1 (permalink)
NamePros Regular
 
John84's Avatar
Join Date: Apr 2006
Posts: 267
John84 is an unknown quantity at this point
 



Random Rotation Script


Hello,
????: NamePros.com http://www.namepros.com/programming/293444-random-rotation-script.html

I have used the following php script to get random text in my website...

HTML Code:
<?php
$number=mt_rand(1, 6);

if ($number=="1") {

include "navrandom1.php";

}

if ($number=="2") {

include "navrandom2.php";

}

if ($number=="3") {

include "navrandom3.php";

}

if ($number=="4") {

include "navrandom4.php";

}

if ($number=="5") {

include "navrandom5.php";

}

if ($number=="6") {

include "navrandom6.php";

}
?>
... each .php page is simply a paragraph and it works just fine, however, I was hoping to add about 20 different items as oppose to the current 6. Is there a shortcut in doing this or do I have to put...

[HTML]
HTML Code:
<?php
if ($number=="?") {

include "navrandom?.php";

}
for each single item. Also, is there a way to put all the paragraphs in one page and have the script randomly choose one paragraph to display?
John84 is offline  
Old 02-12-2007, 03:24 PM   #2 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,568
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship
Why not try something like this

PHP Code:
$number mt_rand(16);

include 
"navrandom{$number}.php"
The way you are doing it is inefficient in two ways really, the first is using the conditionals and the second if repeatidly using the if without elseif etc so it will check every condition instead of stopping when one is matched.
????: NamePros.com http://www.namepros.com/showthread.php?t=293444

You cuold also use switch etc but this way is best .
Matthew. is offline  
Old 02-12-2007, 08:36 PM THREAD STARTER               #3 (permalink)
NamePros Regular
 
John84's Avatar
Join Date: Apr 2006
Posts: 267
John84 is an unknown quantity at this point
 



Figured it out (not on my own as I suck at PHP)....

HTML Code:
<?php

$paragraphs = array(
'Paragraph 1',
'Paragraph 2',
'Paragraph 3',
'Paragraph 4',
);

$random = $paragraphs[array_rand($paragraphs)];

echo $random;

?>
... however, thanks for the suggestion
John84 is offline  
Old 02-21-2007, 12:30 AM   #4 (permalink)
NamePros Regular
Join Date: Jul 2005
Posts: 274
TrafficSecret is just really niceTrafficSecret is just really niceTrafficSecret is just really niceTrafficSecret is just really niceTrafficSecret is just really niceTrafficSecret is just really niceTrafficSecret is just really niceTrafficSecret is just really nice
 



Programing can be tough! Good Job.

I always suggest searching google for a few minutes if you run accross a problem.
__________________
~TrafficSecret~
KickAssMovies.com - Funny Movies and Hot Babes
Unsober.com - Drunken Media
OvtCheck.com - Domain Tools
HumorTrade.com - Humor Site Banner Exchange
TrafficSecret is offline  
Old 02-21-2007, 07:49 PM   #5 (permalink)
NamePros Regular
 
vinodkv's Avatar
Join Date: Feb 2007
Posts: 403
vinodkv has a spectacular aura aboutvinodkv has a spectacular aura about
 



already problem solved john84 and matthew, you are using "if" statement so that you can use elseif statement also...but thats not needed because john and matthew solved the problem with good technique
vinodkv is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 05:40 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger