问题
I have a Magento store and I would like to replace the src
value of an img based on whether the referring URL contains a specific querystring or querystring value (adnetwork=as) and if not simply leave the image as it is.
Is this simple enough to do, I've searched everywhere for the answer, and even asked on various forums to no avail.
Here's what I have so far, seems like the correct syntax, just Firebug reports that it is unable to load the image URL, and it simply shows "". The paths are correct and have been tested.
<?php
// Path for default image source
$logo = $this->getSkinUrl('images/logo.gif');
// Get the referrer url from the $_SERVER array, or false if it's empty
$referrer = empty($_SERVER['HTTP_REFERER']) ? false : $_SERVER['HTTP_REFERER'];
// If the referrer exists
if($referrer) {
// parse the url for the query
$query_str = parse_url($referrer, PHP_URL_QUERY);
// If there's a query string
if(!empty($query_str)) {
// Parse it and put the array into $components
parse_str($query_str, $components);
// If the "adnetwork" component is set
if(!empty($components['adnetwork'])) {
// Set the $logo var to the adnetwork image source
$logo = $this->getSkinUrl('images/logo-no-number.gif');
}
}
}
?>
<div class="header">
<img class="shop24" src="<?php echo $this->getSkinUrl('images/shop-24.gif') ?>" alt="Shop Online 24 Hours a Day"/>
<div class="shopping-bag"><?php echo $this->getChildHtml('topcart')?></div>
<div id="fplogo"><a href="<?php echo $this->getUrl('') ?>" title="Sale Basins - Clickbasin.co.uk"><img src="<?php echo $logo; ?>" alt="Sale Basins - Clickbasin.co.uk" /></a></div>
<img src="<?php echo $this->getSkinUrl('images/side_logo_promo.gif') ?>" alt="Promotion" class="side-logo-promo"/>
<?php echo $this->getChildHtml('topMenu') ?>
<?php //<?php echo $this->getLogoSrc() ?>
</div>
I hope you can help guys. Thanks.
回答1:
Shouldn't you be simply echoing $logo
? you are assigning the image to $logo
but you never set anything to $logo
.
来源:https://stackoverflow.com/questions/6116081/replace-an-img-src-value-based-on-a-referring-urls-query-string