I have a database table with some rows...
...where often there will be several repeats of items in the option_name column. As you can see, there are more than one instance of Fish, Cat, Bird and Snake. This is expected and fine.
So I am returning these rows on the front end like this...
$poll_id = 211;
$poll_table = $wpdb->prefix . "the_votes";
$poll_items = $wpdb->get_results("
SELECT *
FROM $poll_table
WHERE poll_id = $poll_id
");
echo '<ul>';
foreach ( $poll_items as $poll_item ) {
echo '<li>' . $poll_item->option_name . '</li>';
}
echo '</ul>';
...which will display like this:
- Cat
- Fish
- Bird
- Cat
- Snake
- Bird
- Cat
- Cat
- Fish
- Fish
- Fish
- Fish
- Fish
- Fish
- Fish
- Snake
- Cat
But what I actually need, is to make it so that there are no duplicates returned, and a count next to each. So it should show like this instead:
- Cat (5)
- Fish (8)
- Bird (2)
- Snake (2)
Thanks in advance.
