Output icon (image) when integer is 1

Apologies on being vague - essentially I’m building a simple search directory. In it I want an organization to show a icon if one of the INT fields in my database =1.

The icon is to be a downloaded link - so I’m wanting to output some html say

<a href="link.html"><img src="image.jpg" alt="image" /></a>

Any help / advice much appreciated.

Unless I’m not understanding the problem, it is simply something like:

if ( $whatever == 1 )
  echo '<a href="link.html"><img src="image.jpg" alt="image" /></a>';

Hi @Gandalf - that looks perfect to me - if I was to add another condition like;

if ( $whatever ==1 )
echo '<a href="link.html"><img src="image.jpg" alt="image" /></a>';

OR if ( $whatever == 2 )
echo '<a href="link.html"><img src="image2.jpg" alt="image" /></a>';

Could I have say 3 conditions?

That’s fine, although it would be better if the 2nd and subsequent if’s were elseif’s and what I have given is fine if there is only one statement to execute if the condition is true. If you have multiple statements they need to be contained in curly brackets. Something like

if ( condition1 ) {
  statement1
  statement2
} elseif { condition2 ) {
  statement3
  statement3
}

Hope this makes sense.

1 Like

Yes perfect sense - im coming from an excel background into php so still a big noob…

1 Like

Also have a look at the switch() statement:

switch($whatever) {
 case 1:
  // do some stuff
  break;
 case 2:
  // do some other stuff
  break
 default:
  // do the stuff if nothing else is true
  }

It’s largely a matter of personal choice, I prefer switch as soon as I’ve got more than one if and one else.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.