Connect with Expertise | Find Experts, Get Answers, Share Insights

Random HTML snippets with PHP include?

Kevin G.
 
Posts: n/a
#1: Feb 1 '08
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

Rik Wasmus
 
Posts: n/a
#2: Feb 2 '08

re: Random HTML snippets with PHP include?


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
ZeldorBlat
 
Posts: n/a
#3: Feb 2 '08

re: Random HTML snippets with PHP include?


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');
Lars Eighner
 
Posts: n/a
#4: Feb 3 '08

re: Random HTML snippets with PHP include?


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.
Kevin G.
 
Posts: n/a
#5: Feb 3 '08

re: Random HTML snippets with PHP include?


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.
Closed Thread