Page 1 of 2
Random Splash Scripts
Posted: Wed Apr 02, 2003 8:29 am
by Sly
i am in need to create a random splash (everytime logging into the website the page changes), a good example is
http://www.teamphotoshop.com
and i got this script from some of the members there it sounds like this:
<?php
function random_background() {
global $img_array;
$img_array[1] = "splashes/1/index.php";
$img_array[2] = "splashes/2/index.php";
$img_array[3] = "splashes/3/index.php";
srand ((double) microtime() * 1000000);
if (!isset($random)) {
global $random;
$random = rand(1, count($img_array));
} // endif
return $img_array[$random];
} // end function
if($bg_image == "") {
$bg_image = random_background();
}
include "$bg_image";
// $prev = $random - 1;
// if ($prev == 0) { $prev = count($img_array);
// }
// $next = $random + 1;
// if ($next > count($img_array)) { $next = 1;
// }
$total = count($img_array);
echo "
<table align=\"right\" cellpadding=\"0\" cellspacing-\"0\">
<tr>
<td>
randomsplash ( $random / $total ) .. | | <a href=\"/index.php\"><b>view another</b></a> | | ..
</td>
</tr>
<tr>
<td align=\"right\">
script from <a href=\"
http://www.somethingleet.com\" target=\"_blank\">something leet</a>
</td>
</tr>
</table>
</body>
</html>";
?>
but i got a problem on line 3
"function()" line
i am quite a newbie,
can you help me?
i also not quite understand where should my files i should be
if i name the script splash.php in a folder name splash
where should my index page be? and the images?
thanks
Posted: Wed Apr 02, 2003 9:24 am
by volka
maybe some comments will help
Code: Select all
<?php
/**
creates an array img_array in global scope containing
the paths to all available splash-documents
returns a random doc-path once a request (if called again within the same request returns the same doc)
*/
function random_background() {
global $img_array;
// adding all available documents
$img_array[1] = "splashes/1/index.php";
$img_array[2] = "splashes/2/index.php";
$img_array[3] = "splashes/3/index.php";
global $random;
// if $random hasn't been defined by a prior call to this function
if (!isset($random)) {
// initialize the random-generator (obsolete for php 4.2+
srand ((double) microtime() * 1000000);
// get a random number that can be used as index for img_array
$random = rand(1, count($img_array));
} // endif
// return the document-path that applies to $random in $img_array
return $img_array[$random];
} // end function
// if $bg_image has not been set otherwise yet
if($bg_image == "") {
// get a random path to a script that will create the splash.
$bg_image = random_background();
}
// include (execute) that script
include "$bg_image";
$total = count($img_array);
echo "
<table align="right" cellpadding="0" cellspacing-"0">
<tr>
<td>
randomsplash ( $random / $total ) .. | | <a href="/index.php"><b>view another</b></a> | | ..
</td>
</tr>
<tr>
<td align="right">
script from <a href="http://www.somethingleet.com" target="_blank">something leet</a>
</td>
</tr>
</table>
</body>
</html>
";
?>
there should be a sub-directory
splashes where this php-script resides. And in it there are (at least) three new sub-directories supposed to be (1,2,3), each containing a file index.php. These scripts (but it might also be a static html-file as well) will create the actual splashimage-document. Of course you can change the entries in $img_array (or add new ones).
Posted: Wed Apr 02, 2003 11:23 am
by m3mn0n
My suggestion is use a simple, and good, randomizer script and make
switch() statement that would require a different page depending on the different random number selected. I think this way is much less complicated than what your doing now.
example:
Code: Select all
<?php
switch ($i) {
case 0:
require "index0.php";
case 1:
require "index1.php";
case 2:
require "index2.php";
}
?>
Posted: Thu Apr 03, 2003 3:05 am
by Sly
by i got a parse error on this line
function random_background()
Posted: Thu Apr 03, 2003 3:08 am
by twigletmac
What was the exact error message? In most cases, parse errors are caused by problems in the code above the line it's reported on.
Mac
Posted: Thu Apr 03, 2003 6:04 am
by Sly
error sound like this
Parse error: parse error in splash.php on line 3
Posted: Thu Apr 03, 2003 6:34 am
by twigletmac
Then what's on line 1 and 2?
Mac
Posted: Thu Apr 03, 2003 8:38 am
by Sly
okay
line 2 was a blank line
and line one was "<?" - the php tag
my code is posted up there
now,
i have edited the code, leaving no blanks and it appears to be
parse error on line 2
and line 1 was <?
Posted: Thu Apr 03, 2003 8:56 am
by twigletmac
Could you cut and paste your exact code (the first 5 lines or so), just so we can see exactly what you've got?
Mac
Posted: Thu Apr 03, 2003 10:03 pm
by m3mn0n
Heh, you gatta love php's way to telling you, "ya screwed up!"
The line it usualy states is the line in which php itself had an error executing, it doesn't always tell you which line of code the error happened on. In the case the line looks ok and it's telling you that it's run an error, check every line that has to do with that particular section of the code, including required/included files.
Hope it helps!
Posted: Fri Apr 04, 2003 6:50 am
by Sly
twigletmac wrote:Could you cut and paste your exact code (the first 5 lines or so), just so we can see exactly what you've got?
okay,
the five lines:
<?php
function random_background() {
global $img_array;
$img_array[1] = "splashes/1/index.php";
$img_array[2] = "splashes/2/index.php";
$img_array[3] = "splashes/3/index.php";
check every line that has to do with that particular section of the code, including required/included files.
the code is:
function random_background() {
global $img_array;
$img_array[1] = "splashes/1/index.php";
$img_array[2] = "splashes/2/index.php";
$img_array[3] = "splashes/3/index.php";
srand ((double) microtime() * 1000000);
if (!isset($random)) {
global $random;
$random = rand(1, count($img_array));
} // endif
return $img_array[$random];
} // end function
Posted: Fri Apr 04, 2003 6:56 am
by twigletmac
Are you still getting the parse error as I couldn't replicate it with that code?
Mac
Posted: Fri Apr 04, 2003 8:11 am
by Sly
when did u replicate it?
errors is still there......

Posted: Fri Apr 04, 2003 8:17 am
by twigletmac
I copied and pasted your code but didn't get any parse errors, have you tried deleting the first two lines completely and retyping them?
Mac
Posted: Fri Apr 04, 2003 10:06 am
by Sly
oh no!!!
thants very strange i completely retype the whole thing
even tried changing to <? opening tag but still din work
hrm....
in the code i figure out that it contains </html> but no <html>
can tat be the problem?
i just can't get it.........