I am making an ajax comment system for my website and I am querying my database for any comments with a certain field. I then look and see if the number of returned results is larger than 3, or less than/equal to 3(I have a specific reason for 3). If it is larger than 3, I set a variable = 3 but if it is less than/equal to 3, I set the variable equal to the number of returned rows(mysql_num_rows()). I then echo some text before the loop, then I echo the stuff in the loop and then after the loop I echo more stuff. For some reason my for loop is running more than the given times. What is very strange is that it is echoing the same comment 3 times before moving onto the next comment and echoing that 3 times. After the for loop, I have 9 comments in total. This dosn’t work with my design as I can only fit 3 comments where I am placing them. My for loop is nested in a few whiles because I need to query multiple times to get to the information I need based on the info I send to the php(specific reason for this also). Another thing, the PHP only echoes the comments 9 times only if the number of returned results is greater than 3 works fine if it is less than 3. I have been over this code 50 times but I can’t find anything. Maybe you guys can. Heres the code I am using:
$comments = mysql_query("SELECT * FROM comments WHERE profid = '".$row['id']."'");
$numComments = mysql_num_rows($comments);
$totalCommComments = 0;
if ($numComments > 0) {
if (mysql_num_rows($comments) > 3) {
$totalCommComments = 3;
echo '
<div class="comments">
<div class="commContainer">
<ul class="'.$totalCommComments.'Comm">';
while ($get1 = mysql_fetch_array($comments)) {
for ($l = 1;$l<$totalCommComments;$l++) {
echo 'STUFF IS ECHOED HERE';
}
}
echo '
</ul>
</div>
</div>';
I know the variable $totalCommComments is set to 3 because I can see the class in the <ul>.
PS. This isn’t the full code, only the relevant code. Everything else is querying different database for other information needed to make this query.
Thank you so much
try that. You have a lot of redundant code in there
there are a lot of different ways i could continue to edit this, if you let us know what you are trying to do, i can update this code