NameSilo

Random Rotation Script

Spaceship Spaceship
Watch

John84

Established Member
Impact
2
Hello,

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

HTML:
<?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]<?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?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Why not try something like this ;)

PHP:
$number = mt_rand(1, 6);

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.

You cuold also use switch etc but this way is best :imho:.
 
0
•••
Figured it out (not on my own as I suck at PHP)....

HTML:
<?php

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

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

echo $random;

?>

... however, thanks for the suggestion
 
0
•••
Programing can be tough! Good Job.

I always suggest searching google for a few minutes if you run accross a problem.
 
0
•••
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
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back