Moban Roban Moban Roban - 18 days ago 8
PHP Question

How to add if else into PHP code?

I have 4 social icons and if all of them weren't available then I want to show default.jpg , currently in the column, it's showing 5 icons including default.jpg too.

I added $icons_available, but not working, here is my complete code:

<?php
$icons_available = (
!empty($rec->telegram) &&
!empty($rec->google_plus) &&
!empty($rec->instagram) &&
!empty($rec->facebook)
);
?>
<td>
<?php if(!$icons_available) {?><img class="social" src="<?php echo base_url(); ?>social/default.jpg" /><?php }?>
<?php if($rec->telegram){ ?><a href="<?php echo $rec->telegram; ?>"><img class="social" src="<?php echo base_url(); ?>social/telegram.jpg" /><?php } ?></a>
<?php if($rec->google_pluse){ ?><a href="<?php echo $rec->google_pluse; ?>"><img class="social" src="<?php echo base_url(); ?>social/g.jpg" /><?php } ?></a>
<?php if($rec->instagram){ ?><a href="<?php echo $rec->instagram; ?>"><img class="social" src="<?php echo base_url(); ?>social/insta.jpg" /><?php } ?></a>
<?php if($rec->facebook){ ?><a href="<?php echo $rec->facebook; ?>"><img class="social" src="<?php echo base_url(); ?>social/f.jpg" /><?php } ?></a>
</td>


Here is my question:

**


If all of 4 icons weren't available together, then it will show
default.jpg otherwise it won't show default.jpg, How to make it?


**

Answer

try below code: (replace you above mention code and try with this)

<td>
    <?php 
        if((!empty($rec->telegram) && !empty($rec->google_plus) && !empty($rec->instagram) && !empty($rec->facebook))) { ?>
            <img class="social" src="<?php echo base_url()."social/telegram.jpg"; ?>" />
            <img class="social" src="<?php echo base_url()."social/g.jpg"; ?>" />
            <img class="social" src="<?php echo base_url()."social/insta.jpg"; ?>" />
            <img class="social" src="<?php echo base_url()."social/f.jpg"; ?>" /><?php 
         }else{ 
            $defaultSrc = base_url()."social/default.jpg";
            for($i=0; $i < 4;$i++){
                echo '<img class="social" src="'.$defaultSrc.'"/>';
            }
        } 
    ?>
</td>
Comments