Dan Dan - 1 month ago 6
PHP Question

add additional echo in if statement

I have this following snippet:

<?php
if($people->num_rows >= 1) {
while($person = $people->fetch_object()) {
echo '
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="card card-user">
<div class="content">
<div class="author">
<a href="'.$system->getDomain()?>/user/<?=$person->id.'">
<img class="avatar" src="'.$system->getProfilePicture($person).'">
<h4 class="title">'.$system->getFirstName($person->full_name).', '.$person->age.'</h4>
</a>
</div>
<p class="text-center text-muted">
';
echo $person->city.$system->ifComma($person->city); echo ' '.$person->country.'
</p>
</div>
</div>
</div>
';
}
} else {
?>


however next to the actual age (after the closing H4 tag) I'd like to add a further function which shows the current online status of the user.

The code for this would be as follows:

<?php if($system->isOnline($profile->last_active)) { echo '<i class="online-status online"></i>'; } else { echo '<i class="online-status offline"></i>'; } ?>


What is the best way to combine this snippet (online status) with the statement above?

Some expert help would be greatly appreciated

Answer

Separate your HTML and PHP code and do it like the below. Your requirement is done using a single line ternary operator.

<?php 
if($people->num_rows >= 1) {
    while($person = $people->fetch_object()) { ?>
        <div class="col-lg-4 col-md-4 col-sm-4">
            <div class="card card-user">
                <div class="content">
                    <div class="author">
                        <a href="<?php echo $system->getDomain().'/user/'.$person->id;?>">
                            <img class="avatar" src="<?php echo $system->getProfilePicture($person);?>">
                            <h4 class="title"><?php echo $system->getFirstName($person->full_name).', '.$person->age;?></h4> 
                            <?php echo ($system->isOnline($profile->last_active)) ? '<i class="online-status online"></i>' : '<i class="online-status offline"></i>'; ?>
                        </a>
                    </div>
                    <p class="text-center text-muted">
                        <?php echo $person->city.$system->ifComma($person->city); echo ' '.$person->country;?>
                    </p>
                </div>
            </div> 
        </div>      
    <?php 
    } 
}
?>
Comments