Random Splash Scripts
Moderator: General Moderators
Random Splash Scripts
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
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
maybe some comments will helpthere 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).
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>
";
?>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:
example:
Code: Select all
<?php
switch ($i) {
case 0:
require "index0.php";
case 1:
require "index1.php";
case 2:
require "index2.php";
}
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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!
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!
okay,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?
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";
the code is:check every line that has to do with that particular section of the code, including required/included files.
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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK