Hi, ( I have attached a screen grab of what it looks like )

I am trying to collect tags for tagged photos based on the photo id.

I get duplicates when i do GROUP BY tag_name or DISTINCT tag_name.

  1. The first query is set up like that because im using pagination.
  2. The second query gets my tags based on photo id in a loop.

If anyone could help make sense of my queries i would appreciate it

<?php

// Get photo id based on category and dont go beyond the pagination limit.

$sql1 = "SELECT id FROM $tableName WHERE cat_id=".$catid."  LIMIT $start, $limit";
$result = mysql_query($sql1);

while ($row1 = mysql_fetch_array($result)) { 

// Get tags based on the photo id

$sql2 = "SELECT tag_name FROM photoTagged WHERE file_id=".$row1['id']." GROUP BY tag_name";
$result = mysql_query($sql2) or die(mysql_error());
 
while ($row2 = mysql_fetch_array($result)) { 

$tag = $row2['tag_name']; 

?>

<li><a href="#"><?php echo $tag; ?></li>

<?php }} ?>

Recommended Answers

All 2 Replies

SELECT [U]DISTINCT[/U] tag_name FROM photoTagged WHERE file_id=" . $row1['id'] should give you unique tag_names. If they are not unique, chances are that they contain invisible characters like trailing spaces in which they differ from one another.

SELECT [U]DISTINCT[/U] tag_name FROM photoTagged WHERE file_id=" . $row1['id'] should give you unique tag_names. If they are not unique, chances are that they contain invisible characters like trailing spaces in which they differ from one another.

Hi,

Thank you so much for your reply, i have tried and also have carefully checked the keywords. There are no spaces or characters of any sort. The thing is that the keywords are in a simple table.

id tag_name file_id
1 flowers 91
2 flowers 92
3 fish 91
4 fish 92

I think its to do with the fact that i have a loop inside a loop and its somehow creating duplicates.

I am going to try my query outside the loop and use DISTINCT or GROUP BY and see if that works.

Thanks again for your response, i'llbe sure to double check what you said though.

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.