Please I need help on this coding

Please am new here, how can i add variable to this code.

if ($res[‘image’] ==1) {
echo ‘<img src=“images/dl.gif” alt=“”/>’;
} elseif ($res[‘image’] ==2) {
echo ‘<img src=“images/dp.gif” alt=“”/>’;
}

To be like

$img= if ($res[‘image’] ==1) {
echo ‘<img src=“images/dl.gif” alt=“”/>’;
} elseif ($res[‘image’] ==2) {
echo ‘<img src=“images/dp.gif” alt=“”/>’;
}

So i can call it using ‘.$img.’ in a div class, pls help me.

Welcome to SitePoint Proudlymob

Am moving this to the PHP forum where someone with more expertise may clarify my rough answer.

if you are CERTAIN ONLY have TWO options for the IMG SRC you could approach it this way:

<?php
$img= ($res['image'] ==1) ? '<img src="images/dl" alt=""/>' : '<img src="images/dp" alt=""/>';
?>

or if you have many options , you could use a case statement

<?php
switch ($res['image'] ) {
    case 1:
       $img= '<img src="images/dl.gif" alt=""/>';
        break;
    case 2:
       $img= '<img src="images/dl.gif" alt=""/>';
        break;
    case 3:
       $img= '<img src="images/other.gif" alt=""/>';
        break;
    default:
       $img= '';
}
?>

hope that helps

Lemme try it, thanx but how can i call the switch (2nd code) in a div class?

Please help me.

you cant ‘call’ php from html. Thats woulnd’t even make sense. However, you can can call a function in PHP.

also what do you mean by “div class”? the tag DIV is something wholly different from a “class”.

So let’s say you wanted to add an image inside a DIV , when certain condition were met.

<?php
function addIMG($cond){
switch ($cond ) {
    case 1:
       $img= '<img src="images/dl.gif" alt=""/>';
        break;
    case 2:
       $img= '<img src="images/dl.gif" alt=""/>';
        break;
    case 3:
       $img= '<img src="images/other.gif" alt=""/>';
        break;
    default:
       $img= '';
}
return $img
}

?>
<div> <? echo php addIMG($res['image'])?> ?>
...other content...
 </div>

thats just the way way is done. coding is as much about thinking as it is about knowing ‘commands’.