user991830
user991830

Reputation: 894

php add image to title if certain word is used

I have the following code:

<h2>
  <?php echo $this->item->title; ?>
</h2>

Dependent on the word being pulled in as the title, I need to place a different image to the left of the title text.

Images: uk.png, france.png, germany.png

So if the title text says France, I need to pull in the france.png image. So I need a list of images and titles that 'could' be used and if a title doesn't match an image, no image is shown.

Hope this makes sense...

Upvotes: 0

Views: 344

Answers (5)

Vadim Belyaev
Vadim Belyaev

Reputation: 2859

Usage of the country name as a key to display the corresponding image may be considered as not very reliable solution.

I'd recommend to use ISO 3166-1 country codes as keys. Then, based on the coutry code you might have two functions which return country name and an image.

You may come up to someting like that (I didn't use classes and error handling in this example intentionally):

<?php
function getCountryNameByIsoCode($iso_code)
{
    static $country_names = array ("FRA" => "France", "GBR" => "United Kingdom", ...);
    return $country_name[$code];
}

function getCountryFlagImageFileNameByIsoCode($code)
{
    static $country_flags = array ("FRA" => "france.png", "GBR" => "uk.png", ...);
    return $country_flags[$iso_code];
}
?>

<h2>
    <img src="/img/<?php echo getCountryFlagImageFileNameByIsoCode($this->item->iso_code); ?>" alt="" />
    <?php echo getCountryNameByIsoCode($this->item->iso_code); ?>
</h2>

Upvotes: 0

Sondre
Sondre

Reputation: 1898

This may not be the correct answer for your question, but for links you can achive what you want using just css.

a[href$='.zip'], a[href$='.rar'], a[href$='.gzip'] {
    background:transparent url(../images/zip.png) center left no-repeat;
    display:inline-block;
    padding-left:20px;
    line-height:18px;
}

See web-kreation for more examples.

Just tought I'd add this as it's a good tip for anyone wanting to add icons to links depending on parts of the link, which is somewhat similar to what you wanted.

Upvotes: 0

Romi Halasz
Romi Halasz

Reputation: 2009

You could write a function which returns the image file, based on an input string, which based on your example, we assume is $this->item->title. Its main purpose is to return a string, after determining the 'country' from the input string.

function getCountryImage($input)
{
    // an array containing the mapping of country names to image file names
    $images = array(
        'France' => 'france.png',
        'UK' => 'uk.png',
        'Germany' => 'germany.png' );

    for each( $images as $country => $filename )
        if( $country == $input )
            return $filename;

    return '';
}

The only thing left if to display the image:

<img src="[image path]/<?php echo getCountryImage($this->item->title);?>" />

Have a great day.

Upvotes: 0

JRL
JRL

Reputation: 78013

for example:

function getPic($title)
{
    static $pics = array('uk'      => 'uk.png',
                         'france'  => 'france.png',
                         'germany' => 'germany.png');
    return isset($pics[$title]) ? "<img src='{$pics[$title]}' >" : "";
}

Upvotes: 0

DaveRandom
DaveRandom

Reputation: 88707

<?php

  $images = array (
    'France' => 'france.png',
    'UK' => 'uk.png',
    'Germany' => 'germany.png'
  );

  if (isset($images[$this->item->title])) {
?>

<img src="<?php echo $images[$this->item->title]; ?>" />

<?php } ?>

<h2>
<?php echo $this->item->title; ?>
</h2>

Upvotes: 1

Related Questions