Log in

View Full Version : Layering a PHP?



Diversions
02-06-2009, 03:13 PM
The goal here is to have a new group of featured products change when either a page is refreshed or a visitor returns to the site. What I have done is created six product galleries and then placed them in a random call as follows:

<?php
srand((float) microtime() * 10000000);
$input = array(
'<?php include ("gallery-insert.php">; ?>',
'<?php include ("gallery-insert2.php">; ?>',
'<?php include ("gallery-insert3.php">; ?>',
'< php include ("gallery-insert4.php">; ?>',
'<?php include ("gallery-insert5.php">; ?>',
'<?php include ("gallery-insert6.php">; ?>',
);
$rand_key = array_rand($input);
echo $input[$rand_key] . "\n";
?>When I call the php files individually they work fine, but when I call from the "galleryrotator.php" file I simply get a ( ; ?>) where the file should appear.
Is it not possible to achieve this with php or is there something I am missing in the code which is clearly possible since I am just beginning to understand php a wee bit better.
Thanks for your help.
DM

Schmoopy
02-06-2009, 04:31 PM
Ah, at the moment you're not actually including them, just echoing them - that's why it doesn't work. Rewrite the code like this:



<?php
srand((float) microtime() * 10000000);
$input = array(
"gallery-insert.php",
"gallery-insert2.php",
"gallery-insert3.php",
"gallery-insert4.php",
"gallery-insert5.php",
"gallery-insert6.php",
);
$rand_key = array_rand($input);
include $input[$rand_key]; echo "\n";
?>


I've tested this and know it works, good luck :)

Remember that you're already within a <?php ?> block and so you don't need to type this for every item in the array, you just need the file names and then use include instead of echo.

Diversions
02-06-2009, 05:07 PM
Just when I thought I was getting the hang of php, I find I am not even close. It works a charm to be sure! Thanks so much Schmoopy.

My regards