在foreach语句中为一行设置if语句


Setting an if statement for a row in a foreach statement

我不知道如何做以下事情:

我正在发送JSON数据在我的php文件中显示,唯一的问题是这一行:

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

这是一个问题的原因是因为它只检查数据库中的个人资料图片。我为用户创建的系统要么是他们的原始默认图像,要么是他们上传的图像。所以,这个问题是,如果有一个默认的图像,那么他们没有从数据库中提取的图像。所以我试图找出一种方法来检查$row['img'],如果没有,那么显示默认值。

默认映像映射如下:<img class="home-profile-pic" src="profile_images/default.jpg">

我是否能够在foreach下有一个if语句,然后为此设置一个变量?如果有,怎么做?

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;
            }

编辑
$user = new User();
        //Get the last insert id
            $select_comments_sql = "
            SELECT c. *, p.user_id, p.img
            FROM home_comments AS c
            INNER JOIN (SELECT max(id) as id, user_id 
                        FROM profile_img 
                        GROUP BY user_id) PI
              on PI.user_id = c.user_id
            INNER JOIN profile_img p
              on PI.user_id = p.user_id
             and PI.id = p.id
            ORDER BY c.id DESC
        ";
        if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
            $select_comments_stmt->execute();
            $rows = $select_comments_stmt->fetchAll(PDO::FETCH_ASSOC);
            $comments = array();
            foreach ($rows as $row) {
                    $html = "";
                    $html .= '<div class="comment-post-box" id="comment-'.$row['id'].'">';
                    //$html .= '<img class="home-comment-profile-pic" src="'.$row['img'].'">';
                    $html .= sprintf(
                        '<img class="home-comment-profile-pic" src="%s">',
                        empty($row['img']) ? 'profile_images/default.jpg' : $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;
            }
        }
                echo json_encode($comments);

在这种情况下,"三元运算符"非常方便:

$html .= sprintf(
    '<img class="home-profile-pic" src="%s">',
    empty($row['img']) ? 'profile_images/default.jpg' : $row['img']
);

如果你确实需要使用两个不同的类,你当然可以扩展这种方法:

$html .= sprintf(
    '<img class="%s" src="%s">',
    empty($row['img']) ? 'home-profile-pic' : 'home-comment-profile-pic',
    empty($row['img']) ? 'profile_images/default.jpg' : $row['img']
);

在php实现的其他比较操作符中记录了三元操作符:http://php.net/manual/en/language.operators.comparison.php

当然,您只需检查$row['img']是否有值。例如,如果为空:

if (trim($row['img']) == '') {
    $html .= '<img class="home-profile-pic" src="profile_images/default.jpg">';
} else {
    $html .= '<img class="home-comment-profile-pic" src="'.$row['img'].'">';
}