-1

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

EDIT

$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);
Paul
  • 3,348
  • 5
  • 32
  • 76

2 Answers2

3

The "ternary operator" is quite handy in such situations:

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

If you really need to use two different classes you certainly can extend that approach:

$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']
);

The ternary operator is documented among the other comparision operators php implements: http://php.net/manual/en/language.operators.comparison.php

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • @Paul Sorry, forgot a comma behind the first operand. Fixed. – arkascha Oct 27 '16 at 14:17
  • Quick example for OP how the ternary operator / shorthand operator works. `$var = 5; $var_is_greater_than_two = ($var > 2 ? true : false); // returns true` – Rimble Oct 27 '16 at 14:17
  • OP has 2 different classes depending on if the image is default or not; this solution doesn't account for that. – mister martin Oct 27 '16 at 14:27
  • ok that fixed the error. I think the issue may actually be within my query. I posted it in my question. – Paul Oct 27 '16 at 14:29
  • This is where the users' profile images are stored, if they have them. Is there anyway to change the query so that if a profile_img is not set then the query will still run? `FROM profile_img ` – Paul Oct 27 '16 at 14:31
  • 1
    @mistermartin Thanks, I added another variant taking that into account. – arkascha Oct 27 '16 at 14:32
  • 1
    @Paul Sorry, I am confused. Your original question did not mention a SQL query, not any potential issues with that. Does that mean that the _real_ question you have is about that sql query which might not deliver a result entry if no profile picture exists? – arkascha Oct 27 '16 at 14:39
  • Potentially. I thought I had it pin-pointed, but I guess I was one layer away. Your answer may still help, but I believe the query is the main culprit. – Paul Oct 27 '16 at 14:41
  • 1
    @Paul Ok, happens, though I am a bit puzzled... I suggest you ask a separate question, since an SQL query is a totally different thing. – arkascha Oct 27 '16 at 14:49
  • I will and will come back and award the appropriate answer. – Paul Oct 27 '16 at 15:01
  • As promised. This helped after I resolved the query. Thanks! – Paul Oct 28 '16 at 20:11
2

sure, you would just check if $row['img'] has a value or not. for example, if it's empty:

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'].'">';
}
mister martin
  • 6,197
  • 4
  • 30
  • 63
  • hmmm this still isn't allowing the user without the profile pic to post. – Paul Oct 27 '16 at 14:16
  • 1
    what do you mean? this code doesn't have anything to do with whether the user is allowed a privilege or not... It just displays a default image if one isn't set. that's exactly what you asked for: "I am trying to figure out a way to check for the $row['img'] or if not, then display the default." – mister martin Oct 27 '16 at 14:21