Forums

[SOLVED] if-else statement to show image from category

flyboeing 28 Jan, 2013
Hello all,

For my website I have the following code:

<?php
$icon = '';
if ( $MyRow->owner2 ) {
  echo "<a href='#' >
<img src='../images/logo/{owner2}_{owner}.png' title='{owner2}' /></a>";
}
?>
With this code I am able to show an image if that cell is not empty. If the cell in my database is empty, no image will be shown.
Now I have the following problem (ok, actually a challenge). The code above is for the "category" civil. For the military category I would like to have an other code.
As you can see, in the code above the name of the image is made out of two pieces: owner2 and owner. For the military images I only want to use the owner.

I hope someone can help me with this.

Regards,
Ruud
GreyHead 28 Jan, 2013
Hi Ruud,

PLease try something like this:
<?php
$icon = '';
if ( $MyRow->owner2 ) {
  $image = 'no_image.png';
  if ( $MyRow->category == 'military' ) {
    $image = $MyRow->owner.'.png';
  } elseif ( $MyRow->category == 'civilian' ) {
    $image = $MyRow->owner2.'_'.$MyRow->owner.'.png';
  }
  echo "<a href='#' >
  <img src='../images/logo/{$image}' title='{owner2}' />
</a>";
}
?>

Bob
flyboeing 28 Jan, 2013
Hello Bob,

Thank you for your reply.
It worked for me, after editing your code (you just misted the = in the first $image :wink:).

But now it works! Thanks!😀

EDIT: This one works on the older version of CC. I am testing it now on my CC V4, but there it is not working😢 Any ideas?
Max_admin 03 Feb, 2013
Hi,

This code in V3:
$MyRow->owner2

should be like this in V4:
$row['owner2']


Other stuff should be the same😉

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
flyboeing 05 Feb, 2013
Thank you Max! That worked for the images!😀
This topic is locked and no more replies can be posted.