Jump to content

New to PHP....trying to figure out how to rotate two images with different hyperlinks


Recommended Posts

Hello,

 

I'm new to PHP so I apologize in advance if I am posting to the wrong category.

 

Simply, I'm trying to use PHP to A/B test two ads.

 

Basically the PHP file that I found successfully rotates the two ads the way I would like, however, I can't seem to figure out how to make them clickable to different links.

 

Example:

 

Ad "A" should be hyperlinked to www.google.com

 

Ad "B" should be hyperlinked to www.msn.com

 

The rotate.php (attached) file is located in the same folder as the two ad images.

 

If anyone has the knowledge and would be willing to assist me I'd be very grateful.

 

Kind regards,

JK

rotate.php

  • 2 weeks later...

The link location goes in your HTML outside the image therefore that code can have no control over the link.

 

You could do something simple like

<?php
define('IMAGEPATH', './images/');
$images = array (
            array (
                'name' => 'im1.png',
                'link' => 'http://www.google.com'
                ),
            array (
                'name' => 'im2.png',
                'link' => 'http://www.msn.com'
                )
        );

function getImage(&$images)
{
    $upper = count($images)-1;
    $choice = rand(0, $upper);
    return sprintf('<a href="%s"><img src="%s" alt="advert" /></a>',
        $images[$choice]['link'], IMAGEPATH.$images[$choice]['name']);
}

?>
<html>
<body>

    <?php echo getImage($images)?>

</body>
</html>

Edited by Barand

 

The link location goes in your HTML outside the image therefore that code can have no control over the link.

 

You could do something simple like

<?php
define('IMAGEPATH', './images/');
$images = array (
            array (
                'name' => 'im1.png',
                'link' => 'http://www.google.com'
                ),
            array (
                'name' => 'im2.png',
                'link' => 'http://www.msn.com'
                )
        );

function getImage(&$images)
{
    $upper = count($images)-1;
    $choice = rand(0, $upper);
    return sprintf('<a href="%s"><img src="%s" alt="advert" /></a>',
        $images[$choice]['link'], IMAGEPATH.$images[$choice]['name']);
}

?>
<html>
<body>

    <?php echo getImage($images)?>

</body>
</html>

 

Hi Barand,

 

I tried using the code that you supplied but was unsuccessful.  Since this is my first time really using PHP, that could be the issue.

 

Could you possible provide specifics about what I am to do with the code that you provided?

 

With my previous code, I was calling out to the PHP file itself.  Like this:

 

<img style="width: 210px; height: 244px;" src="/images/HomePageRibbon/images/rotate.php" alt="" align="left" />

 

I hope this makes sense.  I apologize again, but I'm a true newbie to PHP and could use all the help that I can get.

 

Thanks in advance for the clarification.

To get barands code to work you need to define each advert image and the link for that advert in the $images array. (The image name and link go between the quotes)

$images = array (
            array (
                'name' => 'im1.png',               // image for advert 1
                'link' => 'http://www.google.com'  // link  for advert 1
                ),
            array (
                'name' => 'im2.png',              // image for advert 2
                'link' => 'http://www.msn.com'    // link  for advert 2
                ),
            array (
                // advert 3
                ),
            array (
               // advert 4
                ),
            // etc...
        );

Then where you want the adverts to show you'll call the getImage() function, using

<?php echo getImage($images)?>

That will then randomly choose an advert, display the advert with the link that corresponds to it.

 

 

You current script rotate.php is very limited. You can only display the advert with it. It can't output the link and the advert together. It will need to be recoded. Barands code is a lot easier to use/configure for what you're wanting to do.

Edited by Ch0cu3r

To get barands code to work you need to define each advert image and the link for that advert in the $images array. (The image name and link go between the quotes)

$images = array (
            array (
                'name' => 'im1.png',               // image for advert 1
                'link' => 'http://www.google.com'  // link  for advert 1
                ),
            array (
                'name' => 'im2.png',              // image for advert 2
                'link' => 'http://www.msn.com'    // link  for advert 2
                ),
            array (
                // advert 3
                ),
            array (
               // advert 4
                ),
            // etc...
        );

Then where you want the adverts to show you'll call the getImage() function, using

<?php echo getImage($images)?>

That will then randomly choose an advert, display the advert with the link that corresponds to it.

 

 

You current script rotate.php is very limited. You can only display the advert with it. It can't output the link and the advert together. It will need to be recoded. Barands code is a lot easier to use/configure for what you're wanting to do.

 

Don't think it will work for me.  I have to work within the CMS (Umbraco) and it doesn't like the "<?php echo getImage($images)?>".

 

That's why I asked if I could reference the .php file like I did originally, like so:

 

<img style="width: 210px; height: 244px;" src="/images/HomePageRibbon/images/rotate.php" alt="" align="left" />

 

?

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.