Jump to content

Recommended Posts

Hey.

 

I have made a code, so that everytime you load the page, it randomly selects wether it sould be an ad or a picture to be there.

 

I think Im allmost finished with the code, But it have some errors. Example, that when it sould show an ad it shows nothing.

 

I will list 2 files in here, The functions.php and the Index.php.

 

Functions.php

<?php

function display_adsense() {

echo "
<script type='text/javascript'><!--
google_ad_client = 'pub-7631840574101908';
/* 300x250, oprettet 21-08-09 */
google_ad_slot = '1626493265';
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type='text/javascript'
src='http://pagead2.googlesyndication.com/pagead/show_ads.js'>
</script>";
}

function random_ad() {
echo "
<script language='JavaScript'>
images = new Array(2);
images[0] = '<?php display_adsense() ?>';
images[1] = '<img src=wp-content/themes/wp-symisun/images/ad_test_1.PNG></img>';
index = Math.floor(Math.random() * images.length);
document.write(images[index]);
</script>";
}
?>

 

Index.php

<?php random_ad() ?>

 

I have not posted the Fully index.php or the fully functions.php, But this is the only thing that haves an error.

 

Can anybody tell me what the problem is?

Link to comment
https://forums.phpfreaks.com/topic/172192-random-image-picture-and-adsense/
Share on other sites

your assignment in the javascript is openning a php block, but you are in the middle of an echo in php.  I think you need to just close the string, call the function, then open the string back up.

 

function random_ad() {
echo "
<script language='JavaScript'>
images = new Array(2);
images[0] = '" . display_adsense() . "';
images[1] = '<img src=wp-content/themes/wp-symisun/images/ad_test_1.PNG></img>';
index = Math.floor(Math.random() * images.length);
document.write(images[index]);
</script>";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.