473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Random HTML snippets with PHP include?

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 2 '08 #1
4 7109
On Sat, 02 Feb 2008 01:13:57 +0100, Kevin G. <kg********@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
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 3 '08 #3
In our last episode,
<41**********************************@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/us****@larseighner.com
Countdown: 352 days to go.
Feb 3 '08 #4
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Acacia | last post by:
How would you generate a random number in C++?
19
by: Moritz Beller | last post by:
Dear programmers, Have a look at this function to generate random numbers from 1 to count. It simply does not work. Well, actually it compiles silently, but then returns "1" (Compiler: gcc/++...
6
by: Olly | last post by:
I've found a basic script, however I also need to add alt and title attributes as well, how would I go about doing this? Here's the script I found: Thanks <script language="JavaScript"> <!--...
4
by: cjm | last post by:
I have two problems that I suspect will be bread-and-butter problems for the more experienced guys on here, but I'm not the greatest with js. NB: Code snippets at the bottom The first problem...
4
by: tao_benz | last post by:
Hi: My system generates a bunch of integers, about 1000. There is no any relationship between those integers; the smallest one might only contain 1 digit and the biggest one might contain 6...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
3
by: Roger | last post by:
Is there a method I can call to generate a random number?
9
by: L33VaNcL33F | last post by:
I like to make a javascript that generate random number from the range number within : (10016486 and 99999985). the number always + 22423 that begin from 10016486 and end at 99999985 Example...
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.