Rotating 125x125 banners

by burtie
18 replies
Hi all,

As you can see at Challenge Oxfordshire - For a healthier, happier Oxfordshire I am trying to configure 6 ad slots that will ramdomly display 30 different ads 6 at a time.

The site is using WordPress and I have tried the WP125 plugin (thats whats on there at the minute), but this doesn't suit me for two reasons. Reason one is that it does't rotate giving each ad an equal amount of displays and reason two is that it shows a default ad which I don't really like as I am going to load all the ads from the start.

I am thinking that it would be easier just to insert a bit of code under the header that allows me to do this.

Would anybody be willing to help me out?
#banners #rotating
  • Profile picture of the author ToneUK
    This code should do what you want. It will load all files from a directory you specify and output the filenames of the 6 random files in that directory:

    <?php

    // loop through directory and get the image names.
    // load the names into an array.

    $dir = dir('images_dir'); // change to directory to load images from

    $images = array();
    while (false !== ($entry = $dir->read())) {
    if (strlen($entry) > 3) {
    $images[] = $entry;
    }
    }
    $dir->close();

    // loas 6 random images into array
    $rand_images = array_rand($images, 6);

    // print the images
    foreach ($rand_images as $img) {
    print $images[$img];
    print '<br />';
    }

    ?>


    You would just need to play about with the print statement in the loop to output the correct img src path.
    Signature
    Free article directory for publishers to download. Free article submission with fast approval times. Submit free articles with back links that are Do follow.
    {{ DiscussionBoard.errors[3887945].message }}
    • Profile picture of the author burtie
      Originally Posted by ToneUK View Post

      This code should do what you want. It will load all files from a directory you specify and output the filenames of the 6 random files in that directory:
      Thank you so much ToneUK.

      I have just tried the script and it just shows the file names and not the files. Am i missing something?
      {{ DiscussionBoard.errors[3888024].message }}
      • Profile picture of the author ToneUK
        Change the line: print $images[$img];

        To this:
        print '<img alt="Your Ad Here" src="http://www.challengeoxfordshire.co.uk/wp-content/plugins/wp125/' . $images[$img] . '">';

        The first script only outputs the image filenames because the html might need worked on abit to get them to display the same way.
        Signature
        Free article directory for publishers to download. Free article submission with fast approval times. Submit free articles with back links that are Do follow.
        {{ DiscussionBoard.errors[3888041].message }}
        • Profile picture of the author burtie
          Originally Posted by ToneUK View Post

          Change the line: print ;

          To this:
          print '<img alt="Your Ad Here" src="http://www.challengeoxfordshire.co.uk/wp-content/plugins/wp125/' . . '">';

          The first script only outputs the image filenames because the html might need worked on abit to get them to display the same way.
          Now I am massively confused - sorry :rolleyes:

          Does this piece of code work with WP125?
          {{ DiscussionBoard.errors[3888068].message }}
          • Profile picture of the author ToneUK
            No, its custom code that should work outside of the plugin. Try the following code:

            <?php

            // loop through directory and get the image names.
            // load the names into an array.

            $path_to_images = 'images_dir'; // path to images directory

            $dir = dir($path_to_images);
            $images = array();
            while (false !== ($entry = $dir->read())) {
            if (strlen($entry) > 3) {
            $images[] = $entry;
            }
            }
            $dir->close();

            // loas 6 random images into array
            $rand_images = array_rand($images, 6);

            // print the images
            foreach ($rand_images as $img) {
            print '<img src="' . $path_to_images . '/' . $images[$img] . '" />';
            }

            ?>


            Change the path to the images directory on the following line:
            $path_to_images = 'images_dir'; // path to images directory

            Once you have done that, let me know and I will take a look at your site.
            Signature
            Free article directory for publishers to download. Free article submission with fast approval times. Submit free articles with back links that are Do follow.
            {{ DiscussionBoard.errors[3888084].message }}
      • Profile picture of the author ToneUK
        This might work better:

        <?php

        // loop through directory and get the image names.
        // load the names into an array.

        $path_to_images = 'images_dir'; // path to images directory

        $dir = dir($path_to_images);
        $images = array();
        while (false !== ($entry = $dir->read())) {
        if (strlen($entry) > 3) {
        $images[] = $entry;
        }
        }
        $dir->close();

        // loas 6 random images into array
        $rand_images = array_rand($images, 6);

        // print the images
        foreach ($rand_images as $img) {
        print '<img src="' . $path_to_images . '/' . $images[$img] . '">';
        }

        ?>
        Signature
        Free article directory for publishers to download. Free article submission with fast approval times. Submit free articles with back links that are Do follow.
        {{ DiscussionBoard.errors[3888062].message }}
        • Profile picture of the author burtie
          Originally Posted by ToneUK View Post

          This might work better:
          Perfect!

          Now I just need to figure out how I can get each ad to link to its relevent website.

          Any ideas?

          I am truely grateful for your help.

          Warmly,

          B
          {{ DiscussionBoard.errors[3888090].message }}
          • Profile picture of the author ToneUK
            One way would be to do away with the first example which scans the directory for images for a hard coded solution. Its not ideal because you would have to alter the code each time you wanted to change an add but it would work. See the following:

            <?php

            $path_to_images = 'images_dir';

            $adverts = array();
            $adverts['01.jpg'] = 'http://example1.com';
            $adverts['02.jpg'] = 'http://example2.com';
            $adverts['03.jpg'] = 'http://example3.com';
            $adverts['04.jpg'] = 'http://example4.com';
            $adverts['05.jpg'] = 'http://example5.com';
            $adverts['06.jpg'] = 'http://example6.com';
            $adverts['07.jpg'] = 'http://example7.com';
            $adverts['08.jpg'] = 'http://example8.com';

            // loas 6 random images into array
            $rand_adverts = array_rand($adverts, 6);

            // print the images
            foreach ($rand_adverts as $advert) {
            print '<a href="' . $adverts[$advert] . '">';
            print '<img src="' . $path_to_images . '/' . $advert . '" />';
            print '</a>';
            }

            ?>

            What you would have to do is add or change any of the lines:
            $adverts['01.jpg'] = 'http://example1.com';

            The image name points to the example link.
            Signature
            Free article directory for publishers to download. Free article submission with fast approval times. Submit free articles with back links that are Do follow.
            {{ DiscussionBoard.errors[3888148].message }}
            • Profile picture of the author burtie
              Originally Posted by ToneUK View Post

              One way would be to do away with the first example which scans the directory for images for a hard coded solution. Its not ideal because you would have to alter the code each time you wanted to change an add but it would work. See the following:
              You THE MAN!!!

              Thank you so much

              Have you any clue how I can include a little padding between the ads? The look fab, but that would make it a little nicer

              Warmly,

              B
              {{ DiscussionBoard.errors[3888890].message }}
              • Profile picture of the author ToneUK
                You could add CSS to the HTML printed out in the code or you could alter your style.css sheet.

                Changing the line:
                print '<img src="' . $path_to_images . '/' . $advert . '" />';

                To:
                print '<img style="padding:3px;" src="' . $path_to_images . '/' . $advert . '" />';

                Will add 3 pixels of padding around the images.
                Signature
                Free article directory for publishers to download. Free article submission with fast approval times. Submit free articles with back links that are Do follow.
                {{ DiscussionBoard.errors[3888938].message }}
  • Profile picture of the author burtie
    Thanks ToneUK
    {{ DiscussionBoard.errors[3895062].message }}
  • Profile picture of the author Taziah
    Hey ToneUK...I have no clue how any of this works. I'm new to all this. I'm trying to figure out how to get my banner ads to rotate. I'm using a hosting service to build my website. I'm trying to rotate 20 or so banner ads. You can send me a private message here, or just email me. Thanks!
    Signature
    The Ultimate Affiliate Marketing Program. Number 2 in the World According to Alexa.com! Watch the short video here: http://www.ibourl.com/egq
    {{ DiscussionBoard.errors[4161662].message }}
    • Profile picture of the author ToneUK
      Originally Posted by Taziah View Post

      Hey ToneUK...I have no clue how any of this works. I'm new to all this. I'm trying to figure out how to get my banner ads to rotate. I'm using a hosting service to build my website. I'm trying to rotate 20 or so banner ads. You can send me a private message here, or just email me. Thanks!
      What hosting service are you using? Is it cPanel and is the site created with PHP? Are you using a CMS like Wordpress or Drupal?

      I can't help unless you provide more information. What is the link to your site?

      If you read post #9 then you should get a good idea of what to do. The script in that post rotates ads. You would just need to add 20 or more elements to the array:

      $adverts['01.jpg'] = 'http://example1.com';
      Signature
      Free article directory for publishers to download. Free article submission with fast approval times. Submit free articles with back links that are Do follow.
      {{ DiscussionBoard.errors[4162489].message }}

Trending Topics