while loop showing multiple times??

So I have a while loop inside a while loop. It is a list of photos and under each photo is the comment section. However, each comment is listing as many times as I have photos. Example, photo 1 of 4 left one comment and it is repeated 4 times (same comment). I just need each comment to be listed once. What can I do with this code? Keep in mind this is only the section that I’m having an issue with. I tried to highlight the while statement in red that is repeating. Thanks!
[php]
else if( $cid && empty( $pid ) )
{
$number_of_thumbs_in_row = 3;

$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos WHERE photo_category='".addslashes($cid)."'" );
$nr = mysql_num_rows( $result );

if( empty( $nr ) )
{
$result_final = "\t<tr><td><p>You have no personal photos uploaded by PixiPics at this time.<br/>
If you feel this is not correct please <a href=\"inquiry.php\">contact us</a>.</p></td></tr>\n";
}
else
{
while($row = mysql_fetch_array( $result ))
{
$result2 = mysql_query( "SELECT f.id, f.detail, f.question_filename, g.photo_filename, g.photo_category FROM forum_question as f, gallery_photos as g WHERE question_filename = '".$row[2]."' AND photo_category = '".addslashes($cid)."' ORDER BY id DESC");
$data = '';
while ($rows=mysql_fetch_array($result2)){
$data .= '<tr><td><a href="view_topic.php?id='.$rows[id].'">'.$rows[detail]."</a></td></tr>";
}
$result_array[] = "<a href='http://184.173.230.159/~pixipics/members.php?cid=$cid&pid=\"$row[0]\"'><img src='".$images_dir."/tb_".$row[2]."' border='4' alt='".$row[1]."' /></a><br /><p>Comments:</p>
<div class='toggle'>
<table>
<form id='form1' name='form1' method='post' action='add_topic.php'>
<tr>
<td><strong>Add New Comment:</strong></td>
</tr>
<tr>
<td><textarea name='detail' cols='25' rows='3' id='detail'></textarea></td>
</tr>
<tr>
<td><input type='hidden' id='question_name' name='question_name' value='".$info['name']."'/></td>
</tr>
<tr>
<td><input type='hidden' id='question_filename' name='question_filename' value='".$row[2]."'/></td>
</tr>
<tr>
<td><input type='submit' name='Submit' value='Submit' /> <input type='reset' name='Submit2' value='Reset' /></td>
</tr>
</form>
<tr>
<td><strong>Select a Comment:</strong></td>
</tr>
".$data."
</table>
</div>
";
					
					
}
				
mysql_free_result( $result );	
				
				
$result_final = "<tr>\n";
		
foreach($result_array as $thumbnail_link)
{
if($counter == $number_of_thumbs_in_row)
{	
$counter = 1;
$result_final .= "\n</tr>\n<tr>\n";
}
else
$counter++;

$result_final .= "\t<td>".$thumbnail_link."</td>\n";
					
}
		
if($counter)
{
if($number_of_photos_in_row-$counter)
$result_final .= "\t<td colspan='".($number_of_photos_in_row-$counter)."'>&nbsp;</td>\n";

$result_final .= "</tr>";

}
}
}
[/php]

since the color code did not work this is the statement with the issue from above:
[php]
while ($rows=mysql_fetch_array($result2)){
$data .= ‘

’.$rows[detail]."";
}
[/php]

That’s some pretty strange code. Need to look at those loops. The problem is most likely that last if statement - if($counter) {}.

This -
$nr = mysql_num_rows( $result );
if( empty( $nr ) )

can be condensed into

if(mysql_num_rows($result) != 0) {}

$nr will never be empty. if there’s no query results, it’ll be 0 and the code as you have it will break since there’s no error capture. So what you can do is this (to keep it kinda like what you have now)

if(mysql_num_rows($result) == 0) {
$result_final = “\t

You have no personal photos uploaded by PixiPics at this time.
If you feel this is not correct please <a href=“inquiry.php”>contact us.

\n”;
} else {

}

this - if($number_of_photos_in_row-$counter) - really doesn’t do much of anything. basically, you’re asking an if statement to do basic math, basically if(3-2), do something. And this is most likely the culprit since it does’t know where to stop since its being asked if it equals, is less than, greater than, etc some value.

Hope this helps some. Also need to work on code organization some, its hard to read through what you have there.

thank you so much for looking over my strange code. So I made the changes you mentioned and am still having the issue. the photos loop fine and each one does as should. Also when I take out the while loop i mentioned above the page does exactly as it should. However, once I put the while loop back in then it gives me 4 results back for each comment when I have 4 pictures. But the comments are for the correct picture and that is all good. I tried to LIMIT 1 on the query and then I don’t get all of the comments for the photo, just the last one but I don’t get 4 of them anymore like I used to (which is close to the desired result but not quite there yet). Is there a way to query this so it pulls only the comment once but can still loop through all of the comments for that picture? Like a limit 1 for each result kind of query? Thanks so much for helping me.

using LIMIT 1 will give you 1 result, and without using ORDER BY, its usally the last one.

If the while loop is causing the problems, and the script works without it, then why have it in there?

You might try changing that problematic while loop to a for loop. something like
[php]
while($row = mysql_fetch_array( $result )) {
$result2 = mysql_query( “SELECT f.id, f.detail, f.question_filename, g.photo_filename, g.photo_category FROM forum_question as f, gallery_photos as g WHERE question_filename = '”.$row[2]."’ AND photo_category = ‘".addslashes($cid)."’ ORDER BY id DESC");
$data = array();
$rows = mysql_fetch_array($result2);

for($i = 0; $i < mysql_num_rows($result2); $i++) {
     $data[$i] = '<tr><td><a href="view_topic.php?id='.$rows[id].'">'.$rows[detail]."</a></td></tr>";
}
$result_array[] = "<a href='http://184.173.230.159/~pixipics/members.php?cid=$cid&pid=\"$row[0]\"'><img src='".$images_dir."/tb_".$row[2]."' border='4' alt='".$row[1]."' /></a><br /><p>Comments:</p>";

[/php]

The foreach loop used to process it should still work

Thank you for the suggestion. I did as you suggested and I think i’m having a problem calling it back. It keeps putting the results at the top of my page, not in the section I need it to be. Plus it has multiples as well. When I stated that the script works fine without this while statement I just meant that everything is working 100% the way I need it to, except that I need to have this information included as well. So it seems everything we’ve done still puts in the comment for each time it loops for the pictures. I just need it to stop after 1, then move to the next comment on that picture. Then when those are done, then move onto the next picture and repeat. Any other suggestions? You have been the most helpful so far and I appreciate that.

Would you suppose that maybe a do while statement would work better? maybe it wouldn’t loop through each time and keep duplicating the commnents since it is outside of the main while loop?

Itll do the same thing as the for loop. It might be something before that loop

do while did not help… bummer. So can this not be done? I’m sure it can. I just need a way to have a while loop through the photos, then inside that loop through the comments made on each photo. So basically just a loop inside a loop. But in this case my comments show the amount of times it loops for photos. Example, photo 1 - comment 1, comment 1, comment 1, comment 1, comment 2, comment 2, comment2, comment 2. Photo 2 - comment 1, comment 1, comment 1, comment 1… etc. I appreciate any and all help!

how are the comments tied to the picture?

its supposed to be just pic 1 - comment, pic 2, comment and so on? its going to loop how ever many times there are picture, because its inside the same loop. If there’s a common column, like a pic id, then you can use some sort of join or union for the query.

Thank you so much for sticking with me on this. So each picture is posted on the site, then the member can comment on the pictures. Sometimes more than once per picture but each picture will have a different amount of comments. So I need this to loop and post the picture followed by all comments for that picture, then move on to the next picture. My query pulls the common parts out, the photo_filename which is in both tables so I am matching the photo_filename in the query with the photo_filename of the picture in the loop. That’s why they are nested. If I pull them apart then the while loop for the photos does not fill in the mysql query correctly.

i get what your doing. since you have a common column, look up JOIN and its varients (they’re usually listed together anyways). but basically, your not really pulling out common fields, you’re just selecting columns from 2 different tables.

The example below is kinda a long the same lines, but its for displaying total units for apps.
[php]

App Trends
<?php $fla = mysql_query("SELECT appname FROM venzo_user_apps WHERE uid = $_SESSION[uid]"); if(mysql_num_rows($fla) != 0) { for($v = 0; $v < mysql_num_rows($fla); $v++) { $l = mysql_fetch_array($fla); $find = mysql_query("SELECT SUM(units) unit FROM venzo_app_trends WHERE title = '{$l[appname]}' GROUP BY title") or die(mysql_error()); while($row = mysql_fetch_array($find)) { $total = $row['unit']; } } ?>
<div style='padding-left: 5px;'>Total Units (To-Date): <?=number_format($total, 2)?><br />
	<span style='font-size: 8pt;'>Since account was opened (Updated weekly)</span></div>
<?php //} unset($rows); unset($row); } else { $total = "No Sales"; }[/php]

The first query pulls the app names for a given user. the second query uses the app names to pull out the necessary sales info for display. the totalling is done in the query.

That’s one way you could probably do yours, but i think the better way would to use JOIN.

I came up with this:
[php]$result = mysql_query( “SELECT g.photo_id,g.photo_caption,g.photo_filename,g.photo_category,f.id,f.detail,f.question_filename FROM gallery_photos as g LEFT JOIN forum_question as f ON f.question_filename = g.photo_filename AND photo_category = '”.addslashes($cid)."’" );[/php]
And now I get just the opposite happening. I have 3 of the same pictures that have 3 comments and one of each picture with only 1 comment or 0 comments… Ugh. I am nearing the end of my rope I think.

would you mind sending me the page, some sample pics and a sql dump? i get what you’re trying to do, but i don’t think the problem is with the code you’ve listed in here. you can send it to compfnatic80 at hotmail dot com. i have a local server that the world can’t see that i use for testing projects before they go live.

Yes. Thank you! I will send to you tomorrow. Its late here.

I got your email this morning when i got up. i’ll take a look at it when i get home tonight.

Thank you!!

Sponsor our Newsletter | Privacy Policy | Terms of Service