364,111 Members | 2109 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Random HTML snippets with PHP include?

Kevin G.
P: n/a
Kevin G.
Hello all,

I have a flat HTML page with 3 distinct HTML tables, and I'm trying to
randomly display the tables when the page loads, without repeating.
My idea is to use the PHP include() function to pull in the page
content.

So what I've done so far was broke the full page down into 3 static
HTML files, each containing one of the tables. Then in the main index
page where I want to display the code randomly, I created a variable
using the rand() function -- and then within my include statement I'm
using the $rand variable to build the html filename to include.

The problem is that while the page does *work*, the rand function only
returns one random number and hence all three tables on the page turn
out to be duplicates. What I'm looking to do is make sure that all 3
HTML files are included into the page, but with no repeating, and each
of the 3 will show randomly on page load.

Here's an example of what I have currently: http://www.mollymaguires.net/rotating_html/

Not exactly a PHP guru, so any replies back regarding setting up an
array or custom functions to handle this situation need to be
explained pretty clearly :) Thanks in advance for any help!

KG
Feb 1 '08 #1
Share this Question
Share on Google+
4 Replies


Rik Wasmus
P: n/a
Rik Wasmus
On Sat, 02 Feb 2008 01:13:57 +0100, Kevin G. <kgibbons04@gmail.comwrote:
Hello all,
>
I have a flat HTML page with 3 distinct HTML tables, and I'm trying to
randomly display the tables when the page loads, without repeating.
My idea is to use the PHP include() function to pull in the page
content.
>
So what I've done so far was broke the full page down into 3 static
HTML files, each containing one of the tables. Then in the main index
page where I want to display the code randomly, I created a variable
using the rand() function -- and then within my include statement I'm
using the $rand variable to build the html filename to include.
>
The problem is that while the page does *work*, the rand function only
returns one random number and hence all three tables on the page turn
out to be duplicates. What I'm looking to do is make sure that all 3
HTML files are included into the page, but with no repeating, and each
of the 3 will show randomly on page load.
>
Here's an example of what I have currently:
http://www.mollymaguires.net/rotating_html/
>
Not exactly a PHP guru, so any replies back regarding setting up an
array or custom functions to handle this situation need to be
explained pretty clearly :) Thanks in advance for any help!
Either just use 1 include (if they're numbered), if they have different
names, use a switch statement:

$chosen = mt_rand(1,3);
switch($chosen){
case 1:
include 'first.php';
break;
case 2:
include 'second.php';
break;
case 3:
include 'third.php';
break;
default:
echo 'something went horribly wrong';
}


--
Rik Wasmus
Feb 2 '08 #2

ZeldorBlat
P: n/a
ZeldorBlat
On Feb 1, 7:13 pm, "Kevin G." <kgibbon...@gmail.comwrote:
Hello all,
>
I have a flat HTML page with 3 distinct HTML tables, and I'm trying to
randomly display the tables when the page loads, without repeating.
My idea is to use the PHP include() function to pull in the page
content.
>
So what I've done so far was broke the full page down into 3 static
HTML files, each containing one of the tables. Then in the main index
page where I want to display the code randomly, I created a variable
using the rand() function -- and then within my include statement I'm
using the $rand variable to build the html filename to include.
>
The problem is that while the page does *work*, the rand function only
returns one random number and hence all three tables on the page turn
out to be duplicates. What I'm looking to do is make sure that all 3
HTML files are included into the page, but with no repeating, and each
of the 3 will show randomly on page load.
>
Here's an example of what I have currently: http://www.mollymaguires.net/rotating_html/
>
Not exactly a PHP guru, so any replies back regarding setting up an
array or custom functions to handle this situation need to be
explained pretty clearly :) Thanks in advance for any help!
>
KG
Suppose your HTML files are named table1.php, table2.php, table3.php,
etc.

You can then do something like this:

$numTables = 3;
$a = range(1, $numTables);
shuffle($a);

foreach($a as $i)
include('table' . $i . '.php');
Feb 2 '08 #3

Lars Eighner
P: n/a
Lars Eighner
In our last episode,
<41e68752-42eb-4937-97c3-65f365e1da29@e23g2000prf.googlegroups.com>, the
lovely and talented Kevin G. broadcast on comp.lang.php:
Hello all,
I have a flat HTML page with 3 distinct HTML tables, and I'm trying to
randomly display the tables when the page loads, without repeating.
My idea is to use the PHP include() function to pull in the page
content.
So what I've done so far was broke the full page down into 3 static
HTML files, each containing one of the tables. Then in the main index
page where I want to display the code randomly, I created a variable
using the rand() function -- and then within my include statement I'm
using the $rand variable to build the html filename to include.
The problem is that while the page does *work*, the rand function only
returns one random number and hence all three tables on the page turn
out to be duplicates. What I'm looking to do is make sure that all 3
HTML files are included into the page, but with no repeating, and each
of the 3 will show randomly on page load.
Here's an example of what I have currently:
http://www.mollymaguires.net/rotating_html/
Not exactly a PHP guru, so any replies back regarding setting up an
You don't have a php problem. You have a thinking problem. There are only
six different ways to display all three tables exactly once. Use your
random variable to select one of the six.
array or custom functions to handle this situation need to be
explained pretty clearly :) Thanks in advance for any help!
KG
--
Lars Eighner <http://larseighner.com/usenet@larseighner.com
Countdown: 352 days to go.
Feb 3 '08 #4

Kevin G.
P: n/a
Kevin G.
You don't have a php problem. You have a thinking problem. There are only
six different ways to display all three tables exactly once. Use your
random variable to select one of the six.
Never said I had a PHP problem, I simply said the "problem" is that
the $rand variable gets set only once so then the table is repeated
3x. And while I appreciate any/all replies, I can't help but think
this one was a bit condescending in tone with my "thinking problem"
and all.
Feb 3 '08 #5

Post your reply

Help answer this question



Didn't find the answer to your PHP question?

You can also browse similar questions: PHP