Skip to content Skip to sidebar Skip to footer

Changing The Style Inside "if" Statement

I was trying to change the style of only a part of php. This is my codes; if($fetch_array) { $foto_destination = $fetch_array['foto']; echo '

Solution 1:

If I understand you correctly, it should be:

<?phpif($fetch_array){
?><divstyle= "position:absolute; left:350px; top:70px;"><?php$foto_destination = $fetch_array['foto'];
    print"  <img src = '$foto_destination' height='150px' width='150px'>";
}else{
?><divstyle= "position:absolute; left:350px; top:70px;"><imgsrc = 'images/avatar_default.png'height='150px'width='150px'><?php
}
?></div>

It shows the $foto_destination, if there is one.

HTH

Solution 2:

Did you mean like this?

<?phpif($fetch_array) {


    $photo = $fetch_array['foto'];
    $styles = 'position:absolute; left:350px; top:70px;';

} else {

    $photo = 'images/avatar_default.png';
    $styles = 'position:absolute; left:350px; top:70px;';

}
?><divstyle="<?phpecho$styles; ?>"><imgsrc="<?phpecho$photo; ?>"height="150"width="150" /></div>

Solution 3:

That is correct or you can do an isset()

if (isset($fetch_array) {
  ...

The only advantage being that it will not error if the variable is undefined

Solution 4:

Here's a more compact version, shorttags must be enabled.

<divstyle="position:absolute; left:350px; top:70px;"><imgsrc="<?=isset($fetch_array['foto']) ? "images/avatar_default.png" : $foto_destination['foto'] ?>"height="150px"width="150px" /></div>

otherwise:

<divstyle="position:absolute; left:350px; top:70px;"><imgsrc="<?phpechoisset($fetch_array['foto']) ? "images/avatar_default.png" : $foto_destination['foto'] ?>"height="150px"width="150px" /></div>

Post a Comment for "Changing The Style Inside "if" Statement"