php – create a dropdown for every series

I have a MySQL table and would like to create a number of dropdowns where my raceid = programid.

For example

Read More
Table MYRACES
ID RACEID PROGRAMID TITLE DISTANCE
1    1        1      MYRACE    5
2    1        1      HISRACE   6
3    1        1      HERRACE   7
4    2        2      THATRACE  8
5    2        2      WHATRACE  9
6    3        3      HRDRACE   10
7    3        3      TUFFRACE  11

So in essence using PHP and MySQL I would like to create a separate dropdown for every instance where my raceid = programid and return the title in the dropdown. In the example above I would have 3 separate dropdowns.

So far I have

$programs = $wpdb->get_results("SELECT * FROM myraces WHERE raceid = programid", 'ARRAY_A');
foreach ($programs as $program) {
    echo '<select>';
    echo '<option value="'.$program['$id'].'">'.$program['title'].'</option>';
    echo '</select>';
}

Thanks in advance.

Related posts

Leave a Reply

1 comment

  1. I’m guessing you want something like this:

    $programs = $wpdb->get_results("SELECT * FROM myraces WHERE raceid = programid", 'ARRAY_A');
    
    $grouped = array();
    foreach ($programs as $program) {
        $grouped[$program['raceid']][] = $program;
    }
    
    foreach ($grouped as $group) {
        echo '<select>';
        foreach ($group as $program) {
            printf('<option value="%s">%s</option>',
                   $program['id'], htmlentities($program['title']));
        }
        echo '</select>';
    }