Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay image on php mysql blog based on username of post
    primarykey
    data
    text
    <p>Thanks in advance!</p> <p>I have a simple mysql and php blog that I built based on a tutorial I found online. What I would like to be able to do, but have no idea how to go about it, is this:</p> <p>I would like a picture (avatar) to be displayed with each comment on each post. The picture that is chosen would be based off of the name in the Posted By: area of the comment. So for instance: Let's say me, the admin, leaves a comment on the thread. My name is automatically pulled in via a '$_SESSION' variable so I don't have to worry about entering that each time. When the comment is displayed on the blog thread page, it shows Commented on By: Admin. This name is stored in the db and pulled in with the a php echo statement. </p> <p>So what I want this avatar code to be able to do is 1) look at the area where the Commented on By: text is 2) read the text 3) see that it says Admin and display the admin.png image next to it. If it sees anything other than Admin in the Commented on By: area, then it will display something like guest.png</p> <p>Here is a snippet of code I found in my stackoverflow and google searches. It works but it pulls in the guest image 6 times, then the actual admin.png image, and then the guest image 3 more times. And it displays this way on EACH comment on EACH thread! And when I add a new thread and a new comment to that thread, it adds the guest image again at the end of the multiple images being displayed on each comment. Did I set it up wrong?</p> <pre><code>&lt;? $sql = "SELECT comment_user FROM comments"; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result) != 0) { $counter = $starting + 1; $pathImg = "images/"; while ($row = mysql_fetch_array($result)) { //calculate url image $pathFile = $pathImg . $row['comment_user'] . ".png"; if (!file_exists($pathFile)) { $pathFile = $pathImg . "guest.png"; } ?&gt; &lt;img src="&lt;?=$pathFile?&gt;" alt="&lt;?=$row['comment_user']?&gt;"&gt; &lt;/p&gt; &lt;? $counter++; } } ?&gt; </code></pre> <p>This displays out as (Guest Image)(Guest Image)(Guest Image)(Guest Image)(Guest Image)(Guest Image)(Admin Image)(Guest Image)(Guest Image)(Guest Image).</p> <p>Any help on throwing something together would be great! Trying to keep it simple to!</p> <p>EDIT:</p> <p>This is how the comments are displayed, along with the code from FlyingGuy's answer.</p> <pre><code>&lt;?php foreach ($post['comments'] as $comment){ $commentCount = 0 ; $sql = "SELECT comment_user FROM comments"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $commentCount++ ; $pathImg = "images/"; $pathFile = $pathImg . $row['comment_user'] . ".png"; if (!file_exists($pathFile)) { $pathFile = $pathImg . "guest.png"; } echo "&lt;img src=\"". $pathFile ."\" alt=\"". $row['comment_user'] ."\"\&gt;&lt;br&gt;"; } ?&gt; &lt;h4&gt;By &lt;?php echo $comment['user']; ?&gt; on &lt;?php echo $comment['date']; ?&gt;&lt;/h4&gt; &lt;p&gt;&lt;?php echo $comment['body']; ?&gt;&lt;/p&gt; &lt;hr /&gt; &lt;?php } ?&gt; </code></pre> <p>This is how the functions look for displaying and adding comments:</p> <pre><code>function get_comments($pid){ $pid = (int)$pid; $sql = "SELECT `comment_body` AS `body`, `comment_user` AS `user`, DATE_FORMAT(`comment_date`, '%m/%d/%Y') AS`date` FROM `comments` WHERE `post_id` = {$pid}"; $comments = mysql_query($sql); $return = array(); while (($row = mysql_fetch_assoc($comments)) !== false){ $return[] = $row; } return $return; } // adds a comment function add_comment($pid, $user, $body){ if (valid_pid($pid) === false){ return false; } $pid = (int)$pid; $user = mysql_real_escape_string(htmlentities($user)); $body = mysql_real_escape_string(nl2br(htmlentities($body))); mysql_query("INSERT INTO `comments` (`post_id`, `comment_user`, `comment_body`, `comment_date`) VALUES ({$pid}, '{$user}', '{$body}', NOW())"); return true; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload