jeudi 27 octobre 2016

Setting an if statement for a row in a foreach statement

I cannot figure out how to do the following:

I am sending JSON data in my php file to be displayed, the only issue is this line:

$html .= '<img class="home-comment-profile-pic" src="'.$row['img'].'">';

The reason it is an issue is because it is only checking for profile pictures within the database. Well, the system I created a user will either have their original default image or the one they uploaded. So, this issue is if there is a default image, then they do not have an image to be pulled from the database. So I am trying to figure out a way to check for the $row['img'] or if not, then display the default.

The default image is mapped like this: <img class="home-profile-pic" src="profile_images/default.jpg">

Am I am able to have an if statement under the foreach and then set a variable for this? If so, how?

foreach ($rows as $row) {
                    $html = "";
                    //$html .= '<div class="comment-post-box">';
                    $html .= '<div class="comment-post-box" id="comment-'.$row['id'].'">';
                    $html .= '<img class="home-comment-profile-pic" src="'.$row['img'].'">';
                    $html .= '<div class="comment-post-username">'.$row['username']. '</div>';
                    $html .= '<div>'.$row['date']. '</div>';
                    $html .= '<div class="comment-post-text">'.$row['comment']. '</div>';
                    $html .= '</div>';
                    $data = array('id' => $row['id'], 'date' => $row['date'], 'html' => $html);
                    $comments[] = $data;
            }

Aucun commentaire:

Enregistrer un commentaire