Random Splash Scripts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Sly
Forum Newbie
Posts: 14
Joined: Wed Apr 02, 2003 8:29 am
Contact:

Random Splash Scripts

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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).
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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";
}
?>
Sly
Forum Newbie
Posts: 14
Joined: Wed Apr 02, 2003 8:29 am
Contact:

Post by Sly »

by i got a parse error on this line

function random_background()
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Sly
Forum Newbie
Posts: 14
Joined: Wed Apr 02, 2003 8:29 am
Contact:

Post by Sly »

error sound like this

Parse error: parse error in splash.php on line 3
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Then what's on line 1 and 2?

Mac
Sly
Forum Newbie
Posts: 14
Joined: Wed Apr 02, 2003 8:29 am
Contact:

Post 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 <?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Heh, you gatta love php's way to telling you, "ya screwed up!" :wink:

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!
Sly
Forum Newbie
Posts: 14
Joined: Wed Apr 02, 2003 8:29 am
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Are you still getting the parse error as I couldn't replicate it with that code?

Mac
Sly
Forum Newbie
Posts: 14
Joined: Wed Apr 02, 2003 8:29 am
Contact:

Post by Sly »

when did u replicate it?
errors is still there...... :(
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Sly
Forum Newbie
Posts: 14
Joined: Wed Apr 02, 2003 8:29 am
Contact:

Post 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.........
Post Reply