Jump to content
OtakuBoards

Java Help


Morpheus
 Share

Recommended Posts

I've been using this code to show all of my websites banners but it only shows 4 of them. Can someone figure out what's wrong? I'm not very good with Java.


[php][/php]
Link to comment
Share on other sites

If you just want random banners, that's something rather easy to accomplish.

[CODE]
var pos=Math.floor(Math.random()*(A-1))
var B=new Array(A)
B[0] = "C"
B[1] = "D"
.
.
.
B[A-1] = "E"

function getbanner(){
document.write(< IMG SRC=" +B[pos]+">")
}
[/CODE]

A = number of different banners you have.
B = name of the array of banners.
C...E = the URLs for your banners.

Basically this code just picks a random number and creates an array of URLs. When you call the function getbanner() it just writes the code for an image based on the random number it picked earlier.

Remember you have to call the function somewhere in your code. This particular script should go somewhere up towards the top. Then when you want your banner inserted, write something like [CODE]getbanner()[/CODE] and you're done.

And also note all the code needs to be wrapped with < script > and < /script > tags.
Link to comment
Share on other sites

I use Surpass. Very recommended on my part.

If you use PHP in the future, here is the code:

[PHP]
//Edit this line to the url of your image folder to randomize
//Must be a relative URL in your server, do not put in a forward
//or trailing slash for it to work.
$url='images/banners';
//Stop editing, that is all. Call the image with That's all, enjoy. :D

$files=array();
if ($handle=opendir("$url")) {
while(false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(substr($file,-3)=='gif' || substr($file,-3)=='jpg' || substr($file,-3)=='png' || substr($file,-3)=='bmp') $files[count($files)] = $file;
}
}
}
closedir($handle);

$random=rand(0,count($files)-1);
if(substr($files[$random],-3)=='gif') header("Content-type: image/gif");
elseif(substr($files[$random],-3)=='jpg') header("Content-type: image/jpeg");
elseif(substr($files[$random],-3)=='png') header("Content-type: image/png");
elseif(substr($files[$random],-3)=='bmp') header("Content-type: image/bmp");
readfile("$url/$files[$random]");

?>[/PHP]

I don't remember where I got it. The problem with most pre-made scripts you'll find online that do this is that they require you to put in the name of every single last image in there. It's time consuming and obviously increases the file size needlessly.

With this one, you just specify the folder and that's the end of it. Just take this code, edit it and put it in "randomimage.php" or whatever you want. Then when you want to use it, just use that php file as the image source.
Link to comment
Share on other sites

What did I do wrong?

[PHP]








[/PHP]
Link to comment
Share on other sites

I've got it working now.

[PHP]

[/PHP]

And then, somewhere in the body of your document, just put:

[PHP]

[/PHP]

I tested this and it worked. Part of the problem was that you had a space in a variable name (Weekly Links I changed to WeeklyLinks), and part of it was my fault.
Link to comment
Share on other sites

[color=blue]You're going to want to include the link tag [url="http://www.otakuboards.com/..."][/url]in the document.write part of the function though. Otherwise, you won't be able to include different sites ads and they will always link to the same place.[/color]
Link to comment
Share on other sites

[quote name='Petie][color=blue]You're going to want to include the link tag [url="http://www.otakuboards.com/..."][/url']in the document.write part of the function though. Otherwise, you won't be able to include different sites ads and they will always link to the same place.[/color][/quote]


That's true. However I believe in this case they're all linking to the same place (all the banners are for the same site).

But if they do need to go to different places, I could help there too.
Link to comment
Share on other sites

What's wrong?
[PHP]





[/PHP]

It's all OK until the "getbanner()" function. While it will work, you'll get a border. So REPLACE the getbanner() function above with this one:

[PHP]
function getbanner(){
document.write('')
}
[/PHP]

And that part is good.

However, this second part is wrong:

[PHP]

document.write('')
}
[/PHP]

Here's all you need to put:

[PHP]

[/PHP]

And you'll have a borderless banner that links somewhere on your site. Remember, this second part can go anywhere in your page. It doesn't have to follow the first part of the script at all.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...