Random Flash banner via PHP: Plz help....

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

Post Reply
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

Random Flash banner via PHP: Plz help....

Post by mhulse »

Title: Random Flash banner via PHP: Plz help....

Ack!!! ::Pulling Out Hair:: I can't seem to get the below code to work! What is wrong with me!!! Please help:

Code: Select all

//FILE NAME: random.php

<?php
/* Random flash file 
kvack.inc [[url]http://kvack.euphemize.net[/url]] of euphemize.net (c)MMIII
Please leave this header intact if you wish to use
*/

$directory = 'swf/'; //Directory relative to the file
$flashfiles = list_images($directory);

srand ((float) microtime() * 10000000);
$flash = array_rand($flashfiles);
$flash = $directory.$flash;

function list_images($path)
{
    //declares $flashlist as an array. good practice.
    $flashlist = array();

    //$dh points to the directory object for $path.
    $dh = opendir($path);

    //cycle through the files/directorys in the directory object
    while (false !== ($file = readdir($dh)))
        //for each file/directory, if *.swf, add it to the $flashlist[] array
        if (preg_match('/.(?:swf)$/i', $file))
		{
			$flashlist[] = $file;
		}
	//sort the list
	sort($flashlist, SORT_STRING);
    //returns the array.
    return $flashlist;
}
function echo_result($flash)
{
	echo '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="770" height="128">';
	echo '	<param name="movie" value="'.$flash.'"><param name="quality" value="high">';
	echo '	<embed src="'.$flash.'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="770" height="128">';
	echo '	</embed>';
	echo '</object>';
}
?>
I am including the above PHP script into my main page like so:

Code: Select all

<?php include_once('inc/random.php'); ?>
And then on that same page I am calling this function:

Code: Select all

<?php list_images($directory);?>
Unfortunately, I do not get anything to display on my main page? Can anyone test out the above code, or give me any hints as to what I am doing wrong?

Does anyone know of any other PHP script(s) that will randomly display a .swf on page refresh?

Any code/links/help is appreciated

Thanks in advance,
Cheers!
Micky
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

<?php
$arrFlashFiles=GetFilesOfFolder("ads/head/",".swf");
$SelectedFlashFile=$arrFlashFiles[rand(0,count($arrFlashFiles)-1)];
?>
<TABLE CELLSPACING=0 CELLPADDING=0 BGCOLOR="#ffffff" BORDER=0 WIDTH="100%">
 <TR>
  <TD>
      <OBJECT codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" HEIGHT=60 WIDTH=500 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 BORDER=0>
      <PARAM NAME="movie" VALUE="ads/head/<?php echo $SelectedFlashFile; ?>">
      <PARAM NAME="quality" VALUE="high">
      </OBJECT>
  </TD>
 </TR>
</TABLE>
<?php
function GetFilesOfFolder($dir,$FileExt)
 {
 	$arrFiles=array();
 	if (!($dh = opendir($dir))) die ("Could not open directory");
 	while (($file = readdir($dh)) !== false)
 	 {
 	 	if (strtolower(strrchr($file,"."))==$FileExt)
 	 	 $arrFiles[]=$file;
 	 }
 	 closedir($dh);
 	 return $arrFiles;
 }
?>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Hmmm

Post by neophyte »

Well I think it is because the final function was not called to print the <embed> tags to the browser. Check out your browsers source. Try calling the echo_result($flash).

Code: Select all

<?php
echo_result($flash);
?>
Post Reply