I'm trying to remove duplicates from the result of a foreach loop:
foreach ($tags as $tag) {
$sql = "SELECT url,title,image,gift FROM listings LEFT JOIN tags ON listings.id=tags.product_id INNER JOIN tag_names ON tags.tag_id=tag_names.tag_id WHERE tag_names.tag_name=? and id!=$id ORDER BY RAND() LIMIT 5";
$stmt = mysqli_prepare($mysqli, $sql);
$stmt->bind_param('s',$tag[0]);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_all();
if (count($row) > 0) {
echo etc.
}
The problem is I don't know how to skip echoing duplicate products under the different tags. I.e.
The same product can be under the tag A and under the tag B, but I only want it to be displayed once.
Any help would be appreciated.