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:

<?php

if($fetch_array){
?>
<div style= "position:absolute; left:350px; top:70px;">
<?php
    $foto_destination = $fetch_array['foto'];
    print "  <img src = '$foto_destination' height='150px' width='150px'>";
}else{
?>
<div style= "position:absolute; left:350px; top:70px;">
  <img src = '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?

<?php
if($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;';

}
?>
<div style="<?php echo $styles; ?>">
<img src="<?php echo $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.

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

otherwise:

<div style="position:absolute; left:350px; top:70px;">
  <img src="<?php echo isset($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"