kinney0201 Posted October 14, 2013 Share Posted October 14, 2013 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 Quote Link to comment Share on other sites More sharing options...
kinney0201 Posted October 23, 2013 Author Share Posted October 23, 2013 Anyone? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 23, 2013 Share Posted October 23, 2013 (edited) 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 October 23, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
kinney0201 Posted October 23, 2013 Author Share Posted October 23, 2013 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. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 23, 2013 Share Posted October 23, 2013 (edited) 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 October 23, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Barand Posted October 23, 2013 Share Posted October 23, 2013 You also need to define the path for the images on the first line define('IMAGEPATH', '/images/HomePageRibbon/images/'); Quote Link to comment Share on other sites More sharing options...
kinney0201 Posted October 23, 2013 Author Share Posted October 23, 2013 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" /> ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.