Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>It will set an image name for <strong>each row</strong> of your queries result set so it will look like:</p> <p>[image] [comments]<br> [image] [comments]<br> [image] [comments]<br></p> <p>if you have three rows in your result set.'</p> <p>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:</p> <pre><code>&lt;? $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 "&lt;img src=\"". $pathFile ."\" alt=\"". $row['comment_user'] ."\"\&gt;&lt;br&gt;"; } </code></pre> <p>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 <strong>only</strong> send the image link if there are comments.</p> <p>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 <strong>much</strong> cleaner and it simplifies your code.</p> <p>One of the reasons I don;t like dynamic typing. $row was being mutated to an array of ALL the rows..</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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