PDA

View Full Version : Advertisement Switching Script



Paintball24
08-09-2006, 01:26 AM
Alright, I need some help with this.
I'm looking for a script that and switch between advertisements I have on my site. Now, this is the confusing part. Each ad has its own "script", so in order to make this work....ugh....I think this new script will have to render selected code at random. Hmm...

Did I explain this right?
Can anyone help?

Twey, please? (lol)

blm126
08-09-2006, 03:26 AM
Hmm... what do you mean 'their own script'

Paintball24
08-09-2006, 05:47 PM
I mean like the google ads. They have their own script that you insert into the website and volia, the ad appears.

blm126
08-09-2006, 06:26 PM
This could be done in javascript but would be much easier server side. Do you have access to PHP?

alexjewell
08-09-2006, 06:28 PM
I would love to learn how to do this too!
I have access to PHP.
Thanks.

alexjewell
08-09-2006, 06:36 PM
found the answer! check out:
http://www.firethrottle.com/viewtut.php?id=42

Paintball24
08-09-2006, 11:43 PM
Ah, see that's an image switcher. I need an advertisement switcher. Here's why I can't use this: I'm using Google Adsense. Google gives me a Javascript script to slip into my website for an advertisement to appear. This is the code for one:

<script type="text/javascript"><!--
google_ad_client = "pub-5686021945884113";
google_alternate_color = "191919";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "404040";
google_color_bg = "191919";
google_color_link = "2370B6";
google_color_url = "2370B6";
google_color_text = "FFFFFF";
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>



This could be done in javascript but would be much easier server side. Do you have access to PHP?
Yes, I do.

blm126
08-10-2006, 01:10 AM
Ok, then this can be pretty easy.


<?php
$code_list[] = 'Some code';
$code_list[] = 'Some other code';
$code_list[] = 'Some more random code';
shuffle($code_list);
echo $code_list[0];
?>

Yes this will work javascript to

Paintball24
08-10-2006, 01:13 AM
Wow. Thanks man.
So how does this work? Does it have to be a .php file or can I work in HTML?

Twey
08-10-2006, 01:15 AM
It has to be .php, and you have to have PHP enabled on your server.

Paintball24
08-10-2006, 01:17 AM
Ah. Thanks, but there is a Javascript way, right?
Can I see that? Please?

Twey
08-10-2006, 01:20 AM
<script type="text/javascript">
var adverts = [
'Some code',
'Some more code',
'Some other random code'
];
document.write(adverts[Math.floor(Math.random() * adverts.length)]);
</script>This will slow your page-loading time down a bit, and whatever you do don't use it for important content.
You're probably going to lose a bit of revenue if you use that code, but since it's only an advert it shouldn't be too bad for your page as a whole.

Paintball24
08-10-2006, 02:40 AM
ok, thanks man! :)

Paintball24
08-10-2006, 07:40 PM
Hmm....this isn't quite working right...

I'm trying to insert this:

<script type="text/javascript"><!--
google_ad_client = "pub-5686021945884113";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text";
google_ad_channel ="8672832191";
google_color_border = "404040";
google_color_bg = "191919";
google_color_link = "0099FF";
google_color_text = "FFFFFF";
google_color_url = "0099FF";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Creating a total script of:

<script type="text/javascript">
var adverts = [

'<script type="text/javascript"><!--
google_ad_client = "pub-5686021945884113";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text";
google_ad_channel ="8672832191";
google_color_border = "404040";
google_color_bg = "191919";
google_color_link = "0099FF";
google_color_text = "FFFFFF";
google_color_url = "0099FF";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>',

'test',

];
document.write(adverts[Math.floor(Math.random() * adverts.length)]);
</script>

Is this correct?
It's not working...this is what appears:

', 'test', ]; document.write(adverts[Math.floor(Math.random() * adverts.length)]);

Can someone please help?

blm126
08-10-2006, 07:51 PM
Appears where?
Could we see a demo? Also I would still recommend the PHP version if at possible.

Paintball24
08-10-2006, 07:53 PM
You can see it at the footer of this page:
http://mnightfans.com/betaforums/

mburt
08-10-2006, 07:57 PM
Whoa.. That.. was a gigantic mess!! (no offense). Remember, the variable adverts is an array, so you have to use commas after each segment. This is what it should look like:


<script type="text/javascript">
var adverts = [

'<script type="text/javascript"><!--',
google_ad_client = "pub-5686021945884113",
google_ad_width = 728,
google_ad_height = 90,
google_ad_format = "728x90_as",
google_ad_type = "text",
google_ad_channel ="8672832191",
google_color_border = "404040",
google_color_bg = "191919",
google_color_link = "0099FF",
google_color_text = "FFFFFF",
google_color_url = "0099FF",
'//--></\script>',

'test',

];
document.write(adverts[Math.floor(Math.random() * adverts.length)]);
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Hopefully this helps

mburt
08-10-2006, 07:58 PM
Hmm.. there's still something wrong though..

Paintball24
08-10-2006, 08:00 PM
Yea, //--> shows up and then random numbers from the code.

blm126
08-10-2006, 08:06 PM
Remember, the variable adverts is an array, so you have to use commas after each segment.

I don't want this to sound mean, but that is just wrong. He wants all of that code to appear together. Not individually.

Paintball24: I see the google ads in the footer.Also I am slightly worried that you are using a rebranded version of phpBB. It appears to be legal, but you should just go get phpBB.

Paintball24
08-10-2006, 08:13 PM
From First Post:


I think this new script will have to render selected code at random.

All of the code will have to be there, but it shouldn't all appear at once like you are saying. If that were the case...we wouldn't be spending time thinking about this...

blm126
08-10-2006, 08:16 PM
No you misunderstood me, the code mburt posted would display chunks of the google code at random(instead of the whole code at once)

mburt
08-10-2006, 08:18 PM
If your going to string content on different lines, you have to you + sign after each sentence. There was no indication that this was his idea. I wouldn't see the point in using an array if you were to have all the content joined.

Twey
08-10-2006, 08:31 PM
No no no. It's to do with the linebreaks.
<script type="text/javascript">
var adverts = [

'<script type="text/javascript">google_ad_client = "pub-5686021945884113";google_ad_width = 728;google_ad_height = 90;google_ad_format = "728x90_as";google_ad_type = "text";google_ad_channel ="8672832191";google_color_border = "404040";google_color_bg = "191919";google_color_link = "0099FF";google_color_text = "FFFFFF";google_color_url = "0099FF";</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>',

'test',

];
document.write(adverts[Math.floor(Math.random() * adverts.length)]);
</script>Not particularly easy to read, but it should work just as well.

blm126
08-10-2006, 08:34 PM
See I knew if we waited ling enough Twey would have the answer. :)

Paintball24
08-10-2006, 08:40 PM
Well, it doesn't work. Check it out.
http://mnightfans.com/betaforums/

blm126
08-10-2006, 08:45 PM
Shouldn't


document.write(adverts[Math.floor(Math.random() * adverts.length)]);

be


document.write(adverts[Math.floor(Math.random() * (adverts.length -1))]);

Paintball24
08-10-2006, 09:15 PM
Thanks again, but nope it didn't work.

Same Thing.
http://mnightfans.com/betaforums/

Twey
08-10-2006, 09:24 PM
blm126: No.
Math.random() never returns a number greater than or equal to one. Therefore, Math.floor(Math.random() * x) returns a whole number between 0 and x - 1, since for the result to be x Math.random() would have to return 1.

Paintball24: Sorry, there's a trailing comma after "test" that shouldn't be there as well. Missed that.

Paintball24
08-10-2006, 09:30 PM
blm126: No.
Math.random() never returns a number greater than or equal to one. Therefore, Math.floor(Math.random() * x) returns a whole number between 0 and x - 1, since for the result to be x Math.random() would have to return 1.

Paintball24: Sorry, there's a trailing comma after "test" that shouldn't be there as well. Missed that.

Even well both of those changes combined....it still doesn't work...

blm126
08-10-2006, 09:48 PM
Twey: Thanks :)

Paintball24: As your page is beng served by PHP, why don't you just the PHP code I posted?

Twey
08-10-2006, 09:49 PM
<script type="text/javascript">
var adverts = [
'<script type="text/javascript">google_ad_client = "pub-5686021945884113";google_ad_width = 728;google_ad_height = 90;google_ad_format = "728x90_as";google_ad_type = "text";google_ad_channel ="8672832191";google_color_border = "404040";google_color_bg = "191919";google_color_link = "0099FF";google_color_text = "FFFFFF";google_color_url = "0099FF";<\/script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"><\/script>',
'test'
];
document.write(adverts[Math.floor(Math.random() * adverts.length)]);
</script>We also have to escape the </script> tags to prevent dropping out of script-parsing mode. I forgot about that.

Paintball24
08-10-2006, 10:13 PM
Great. This works perfectly.

I'm also trying to insert this:

<center><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" height="60" width="468">
<param name="movie" value="http://www.soundclick.com/ads/FlashDB/Movies/sc17.swf?bandname=Patrick%20Swanson&amp;bandpic=http%3A%2F%2Fwww%2Esoundclick%2Ecom%2Fimagesflash%2Fpatrickswanson%2Bablindmansvision%2Eswf&amp;songname=Classical%20%3A %20Film%20Music&amp;actionURL=http%3A%2F%2Fwww%2Esoundclick%2Ecom%2Fpro%2F%3FBandID%3D106364"><param name="quality" value="high"><param name="bgcolor" value="#000000">
<embed src="http://www.soundclick.com/ads/FlashDB/Movies/sc17.swf?bandname=Patrick%20Swanson&amp;bandpic=http%3A%2F%2Fwww%2Esoundclick%2Ecom%2Fimagesflash%2Fpatrickswanson%2Bablindmansvision%2Eswf&amp;songname=Classical%20%3A %20Film%20Music&amp;actionURL=http%3A%2F%2Fwww%2Esoundclick%2Ecom%2Fpro%2F%3FBandID%3D106364" quality="high" bgcolor="#000000" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" height="60" width="468">
</object></center>

And it too doesn't seem to be working right.
Can someone help me with this? I can't figure it out. This isn't a "<script>" problem. Hmm...

blm126
08-10-2006, 11:15 PM
Same rules apply(all on one line & escape /)

Paintball24
08-10-2006, 11:48 PM
Ok, lemme try that. Hold on.

Paintball24
08-10-2006, 11:50 PM
Perfecto.

Thanks a bunch to guys! :D

blm126
08-11-2006, 01:30 AM
your welcome(though I'm not sure how much a really did) :)

Paintball24
08-13-2006, 04:31 AM
Yea, I know it seems confusing.
Read the above posts again ;)