0

So I got a database with keywords. The keywords is setup like so: "PHP, HTML, CSS".

I want to split the keywords up, and paste them into a select tag.

I wrote the following code which does that:

$query10 = "SELECT * FROM projects";
    if($result=mysqli_query($conn,$query10))
        while ($row=mysqli_fetch_assoc($result)) {
            $mark = explode(",", $row['projectKeywords']);
                                    
            foreach ($mark as $out) {
                ?> <option><?php echo $out ?></option> <?php
            }                       
        }
   ?>

It works fine, but I get duplicates. How do I delete does duplicates. I have tried with array_unique, but I couldn't figure it out.

-Matteo

  • https://www.php.net/manual/en/function.array-unique.php – AbraCadaver Jun 18 '21 at 18:22
  • You might want to look at https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – Progman Jun 18 '21 at 18:23
  • your prob seeing dupes of different projects not dupes in per projectKeywords, if you don't want dupes, make an array first of all the projects then use array_unique then output the select – Lawrence Cherone Jun 18 '21 at 18:26
  • also you should have a `project_keyword` table (project_id, keyword_id) which you link to a `keyword` table (id, word), then you can directly query the keywords and not need to parse csv, i.e a normalised db structure – Lawrence Cherone Jun 18 '21 at 18:29

0 Answers0