# Running foreach is causing duplicate results

**URL:** <https://www.phphelp.com/t/running-foreach-is-causing-duplicate-results/35766>\
**Category:** Beginners - Learning PHP\
**Created:** [October 17, 2023, 4:24am UTC](https://www.phphelp.com/t/running-foreach-is-causing-duplicate-results/35766 "2023-10-17T04:24:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffw](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@Jeffw](https://www.phphelp.com/u/Jeffw)\
**Post date:** [October 17, 2023, 4:24am UTC](https://www.phphelp.com/t/running-foreach-is-causing-duplicate-results/35766/1 "2023-10-17T04:24:19Z")

</div>

On my crud website the homepage displays all published posts and just above that I’m putting the various categories as links that the posts are attributed to. so that the user can click the category link and display only the posts attributed to that particular category.  
So far I have tried using a foreach to display the categories, and they do display, but it is displaying all of the categories the posts are attributed to which causes some categories to display multiple times. I think this is because multiple posts are tied to the same category. I tried placing a break like this but then it displays only one category. I wanted to see if I can get some advice on getting each category to display only once?  
Also my database query contains some joins which i’m worried is limiting my options. When I click the category link though the posts do display as intended so I was hoping there is a way around this.Thank you

```auto
<?php foreach ($stmt as $row) { ?>
    
    <h3><table><tr><th><a href="category.php?id=<?php echo $row['category_id'] ?>"><?php echo ($row['category']) ?></th></tr></table></h3>
    
    
    <?php break; ?>
     
    <?php } ?>

```

---

<div class="post-metadata">

**Author:** ![Strider64](https://www.phphelp.com/user_avatar/www.phphelp.com/strider64/32/3161_2.png) [@Strider64](https://www.phphelp.com/u/Strider64)\
**Post date:** [October 17, 2023, 8:13pm UTC](https://www.phphelp.com/t/running-foreach-is-causing-duplicate-results/35766/2 "2023-10-17T20:13:49Z")

</div>

Wouldn’t it be better just to do that in the query? I personally don’t see why it would limit your options even if you are using multiple tables? It would be much easier unless I am not understanding you correctly?

---

<div class="post-metadata">

**Author:** ![Jeffw](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@Jeffw](https://www.phphelp.com/u/Jeffw)\
**Post date:** [October 18, 2023, 3:08am UTC](https://www.phphelp.com/t/running-foreach-is-causing-duplicate-results/35766/3 "2023-10-18T03:08:17Z")

</div>

Yes if there’s a way to do that i’m all for it. would something like that involve another WHERE? this is the query. my other idea is adding a something to the foreach to check for duplicates and only echo each one once.

```auto
       $stmt = $pdo->query("SELECT posts.id, posts.title, posts.summary, image.file, posts.image_id, 
                                  posts.member_id, posts.category_id, c.name AS category,
                           
                        CONCAT(m.first_name, ' ', m.last_name) AS author

                           FROM posts
                           JOIN category AS c ON posts.category_id = c.id
                           JOIN member AS m ON posts.member_id = m.id
                           LEFT JOIN image ON posts.image_id = image.id
                           WHERE posts.published = 1")->fetchAll(); ?>
```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [October 18, 2023, 12:04pm UTC](https://www.phphelp.com/t/running-foreach-is-causing-duplicate-results/35766/4 "2023-10-18T12:04:02Z")

</div>

See php’s array\_column() function to get a specific column of data (category), indexed by another specific column (category\_id), which keeps only the last entry when there are duplicates, then loop over the result to produce the category links.

---

<div class="post-metadata">

**Author:** ![Jeffw](https://www.phphelp.com/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@Jeffw](https://www.phphelp.com/u/Jeffw)\
**Post date:** [October 20, 2023, 4:21am UTC](https://www.phphelp.com/t/running-foreach-is-causing-duplicate-results/35766/5 "2023-10-20T04:21:33Z")

</div>

thank you that seems to be what I’ve been looking for. The only issue is I’m only able to get it to output a the category id as a link rather than category name.

```auto
<?php 

foreach (array_column($stmt, 'category_id', 'category') as $row) {
    
    

    ?>
    
    <h3><table><tr><th><a href="category.php?id=<?php echo $row ?>"><?php echo $row ?></th></tr></table></h3>
    
   
    
    
    <?php } ?>

```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [October 20, 2023, 5:57am UTC](https://www.phphelp.com/t/running-foreach-is-causing-duplicate-results/35766/6 "2023-10-20T05:57:57Z")

</div>

You would want to use the foreach - `foreach (iterable_expression as $key => $value)` syntax since you want both the key and value.
