Hi all. I'm trying to implement a rotating image system written in Javascript into a PHP website. The PHP page and the Javascript both work fine independently, but integrating the JS is proving tough and giving me errors.

Here is the top part of the PHP page:

<?
include ("./application.php");
include ("./functions.php");
include ("./database.php");

After line 4 I would like to implement the following Javascript:

<!--webbot bot="HTMLMarkup" startspan --><script language="JavaScript"><!--

//store the quotations in arrays

images = new Array(2);

images[0] = "<a href = 'http://www.niburu.nl/index.php?articleID=22481'><img src='http://www.niburu.nl/img/blooms2.jpg' border=0></a>";

images[1] = "<a href = 'http://www.flexwebhosting.nl/'><img src='http://www.niburu.nl/img/flexweb2.jpg' border=0></a>";

index = Math.floor(Math.random() * images.length);

document.write(images[index]);

//done

// --></script><!--webbot bot="HTMLMarkup" endspan -->&nbsp;&nbsp;<!--webbot bot="HTMLMarkup" startspan --><script language="JavaScript"><!--

//store the quotations in arrays

images = new Array(1);

images[0] = "<a href = 'http://www.niburu.nl/index.php?articleID=22449'><img src='http://www.niburu.nl/img/spieghel2.jpg' border=0></a>";

index = Math.floor(Math.random() * images.length);

document.write(images[index]);

//done

// --></script><!--webbot bot="HTMLMarkup" endspan -->&nbsp;&nbsp;<!--webbot bot="HTMLMarkup" startspan --><script language="JavaScript"><!--

//store the quotations in arrays

images = new Array(1);

images[0] = "<a href = 'http://www.niburu.nl/index.php?articleID=22551'><img src='http://www.niburu.nl/img/letsclean.jpg' border=0></a>";

index = Math.floor(Math.random() * images.length);

document.write(images[index]);

//done

// --></script><!--webbot bot="HTMLMarkup" endspan -->

Any ideas how I can get this to display properly? Simply pasting the JS isn't working, and I'm not too good at the whole coding thing. Thanks for any help, I really appreciate it.

dont use javascript,
try php to randomize the images as well, there is no user input, there need be nothing clientside

array() and rand() in php

has the bonus there is less to download, cant be blocked if javascript is disabled

almostbob's idea is probably better. But, if you have to use JavaScript, you will have to close the PHP block before you include it then re-open it after the JS. Otherwise, your JS will get eaten by the PHP parser and cause issues.

<?php

echo $images[rand(0,count($images)-1)];  

?>

Fbody is right,

after your include statements, close it off

?>
put JS here
<?

Thanks guys, I fiddled around with the suggestions and got it to display more or less correctly, it'll just take a little additional tweaking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.