Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6808945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:01:07+00:00 2026-05-26T20:01:07+00:00

Thanks in advance! I have a simple mysql and php blog that I built

  • 0

Thanks in advance!

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:

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.

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

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?

<?
$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";
        }
        ?>
<img src="<?=$pathFile?>" alt="<?=$row['comment_user']?>">
</p>
<?
       $counter++;
    }
}
?>

This displays out as (Guest Image)(Guest Image)(Guest Image)(Guest Image)(Guest Image)(Guest Image)(Admin Image)(Guest Image)(Guest Image)(Guest Image).

Any help on throwing something together would be great! Trying to keep it simple to!

EDIT:

This is how the comments are displayed, along with the code from FlyingGuy’s answer.

<?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 "<img src=\"". $pathFile ."\" alt=\"". $row['comment_user'] ."\"\><br>";

}
?>
<h4>By <?php echo $comment['user']; ?> on <?php echo $comment['date']; ?></h4>
<p><?php echo $comment['body']; ?></p>
<hr />

<?php

}

?>

This is how the functions look for displaying and adding comments:

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

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:01:08+00:00Added an answer on May 26, 2026 at 8:01 pm

    Look what you are trying to do is select the image that matches the name of the user in the current row of your result set. So you will set your image file variable as appropriate for each row and you are sending that to the browser.

    For starters and can see the probability of case issues here. Are all user names forced to lower case and all image names forced to lower case? If this is on a linux box that is a land mine on windows not so much, but this should be taken into account.

    It will set an image name for each row of your queries result set so it will look like:

    [image] [comments]
    [image] [comments]
    [image] [comments]

    if you have three rows in your result set.’

    Personally I avoid all of the turning php on and off all over the place. Concat a single string and then simply echo it out for each row. So I would code it like so:

    <?
    $commentCount = 0 ;
    $sql = "SELECT comment_user FROM comments";
    $result = mysql_query($sql) or die(mysql_error());
    
    while ($row = mysql_fetch_assoc($result)) {
       $commentCount++ ;     
       $pathFile = $pathImg . $row['comment_user'] . ".png";
    
       if (!file_exists($pathFile)) {
        $pathFile = $pathImg . "guest.png";
       }
    
       echo "<img src=\"". $pathFile ."\" alt=\"". $row['comment_user'] ."\"\><br>";
    
    }
    

    So I have eliminated a lot of things from your code example like counters etc. You don’t really need to check and see if there are rows since the while loop simply will not execute of there are no rows so you will simply have a question of comment with no subordinate comments and it will only send the image link if there are comments.

    No if it were me doing this I would create an avatar file name is the user table and store the path to those as part of the system configuration which would be part of the global set of variables that are always present. Your query would then join in the users table and the image name or guest image would be in your result set. A bit more complex but much cleaner and it simplifies your code.

    One of the reasons I don;t like dynamic typing. $row was being mutated to an array of ALL the rows..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks in advance.. I have a windows phone application. In that I am showing
First hi and thanks in advance in ASP.NET : assume that i have a
I built a really basic php/mysql site for an architect that uses one 'projects'
I have written a simple mysql search form that searches between two dates. This
I have an incredibly simple query (table type InnoDb) and EXPLAIN says that MySQL
I use PHP, MySQL and Zend Framework. I have some simple tables with simple
My problem in fairly simple. I have a login system constructed using php, mysql
Thanks in advance for your help. I have a need within an application to
Thanks in advance for your assistance. I have the following exported part: [Export (typeof(INewComponent))]
thanks in advance for the replies.... I have started to create a network topology

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.