I have a table where I need to get all data, but I need to display the “type” column in a dropdown list. Now in my table it has 2 green, 1 red and 3 white.
id type_id type info1 info2
1 1 green some important data some important data
2 1 green some important data some important data
3 2 red some important data some important data
4 3 white some important data some important data
5 3 white some important data some important data
6 3 white some important data some important data
I wanted to display this on php like this (don’t worry if the codes are incomplete, I got it :D),
<select>
<?php
foreach($result as $r):
echo '<option>'. $r['type'] . '</option>';
endforeach;
?>
</select>
The problem with this is that it display all data in column(type), I need to display 1 each type_id. I can’t use GROUP BY because it won’t show infos that I need under that type. How can I achieve this? Thanks!
If you don’t want to run a second query:
If it is ok to run a second query, read Alex Belyaev’s answer.