0

In a PHP page I have that works perfectly as-is, I want to 'order by' one table in an array of tables. I'm using this function to extract field values from multiple lookup tables which I use to populate select drop-down fields in a web form. Again, the form is working fine as-is, but I want to sort the values in one particular drop-down, which is fed by the 'evtcarclasses' table.

I have searched here for "array of tables", "sorting values in an array" and many other key phrases. I've done likewise in Google and other sources. All searches lead me in a direction that doesn't consider using a collection of tables and values in a single array.

Following is my function that collects all the relevant data. Is there any way to sort the values selected from the "evtcarclasses" table?

    /**
 * Get data from database for select options
 *
 * @var array
 * @return string
 */
protected function get_data_options($optionKey)
{
    //Defaults
    $return = [];

    //Database tables
    $databaseTables = [
        'state'     => ['table' => 'states', 'column' => 'statename'],
        'carYear'   => ['table' => 'evtcaryears', 'column' => 'caryear'],
        'carModel'  => ['table' => 'evtcarmodels', 'column' => 'carmodel'],
        'bodyStyle' => ['table' => 'evtcarbodtypes', 'column' => 'carbodytype'],
        'carType'   => ['table' => 'evtcartypes', 'column' => 'cartype'],
        'carClass'  => ['table' => 'evtcarclasses', 'column' => 'carclassname'],
    ];

    //Fetch rows from table
    $fetchRows = $this->db->get($databaseTables[$optionKey]['table']);

    //If rows are found
    if (count ($fetchRows) > 0) {

        foreach ($fetchRows as $k => $row) {

            //If option is car class
            if ($optionKey == 'carClass') {

                //If group name is present
                if (!empty ($row['carclassgroup'])) {

                    if (!in_array($row['carclassgroup'], $return)) {
                        $return[$row['carclassgroup']]['label'] = $row['carclassgroup'];
                    }
                    $return[$row['carclassgroup']]['value'][] = [
                        'label' => $row[$databaseTables[$optionKey]['column']],
                        'value' => $row['carclasstag'] . ' ' . $row[$databaseTables[$optionKey]['column']],
                    ];
                } else {
                    $return[$k] = [
                        'label' => $row[$databaseTables[$optionKey]['column']],
                        'value' => $row['carclasstag'] . ' ' . $row[$databaseTables[$optionKey]['column']],
                    ];
                }
            } else {
                $return[$k] = [
                    'label' => $row[$databaseTables[$optionKey]['column']],
                    'value' => $row[$databaseTables[$optionKey]['column']],
                ];
            }
        }

        //If option is car class
        if ($optionKey == 'carClass') {
            $return = array_values($return);
        }
    }

    //Return options
    return $return;
}

Here's the table data that's being gathered now:

mysql> select * from evtcarclasses;
+---------------+---------+------------+-------------+--------------------------------+------------------+---------------------+
| evtcarclassid | eventid | carclassid | carclasstag | carclassname                   | carclassgroup    | updated             |
+---------------+---------+------------+-------------+--------------------------------+------------------+---------------------+
|             2 |      12 |         11 | L           | 04-06 GTO-Modified Coupe       | GTO MODIFIED     | 2018-02-19 19:59:06 |
|             3 |      12 |          5 | E           | 04-06 GTO-Stock Coupe          | GTO STOCK        | 2018-02-19 19:57:56 |
|             4 |      12 |         30 | U           | 1967-02 Formula All            | FIREBIRD/TRANSAM | 2018-02-19 20:03:12 |
|             5 |      12 |         27 | Z           | 75-10 Other Pontiacs-Stock/Mod | OTHER PONTIACS   | 2018-02-19 20:04:06 |
|             6 |      12 |         29 | BB          | 26-10 Pontiacs-Project         | PROJECT          | 2018-02-19 20:04:38 |
|             7 |      12 |         32 | W           | 67-02 Special Edition Firebird | FIREBIRD/TRANSAM | 2018-02-19 20:03:23 |
|             9 |      12 |         22 | X           | 84-88 Fiero-Stock/Mod          | FIERO            | 2018-02-19 20:03:41 |
|            10 |      12 |          6 | F           | 64 GTO-Modified HT/Coupe       | GTO MODIFIED     | 2018-02-19 19:58:46 |
|            11 |      12 |          1 | A           | 64-65 GTO-Stock HT/Coupe       | GTO STOCK        | 2018-02-19 19:57:27 |
|            12 |      12 |         15 | P           | 64-71 GTO-Modified Conv        | GTO CONVERTIBLE  | 2018-02-19 20:00:26 |
|            13 |      12 |         13 | M           | 64-67 GTO-Stock Conv           | GTO CONVERTIBLE  | 2018-02-19 20:00:16 |
|            14 |      12 |         20 | S           | 68-74 Tempest/Le Mans-All      | TEMPEST/LEMANS   | 2018-02-19 20:02:20 |
|            15 |      12 |          7 | G           | 65 GTO-Modified HT/Coupe       | GTO MODIFIED     | 2018-02-19 19:58:50 |
|            16 |      12 |          8 | H           | 66 GTO-Modified HT/Coupe       | GTO MODIFIED     | 2018-02-19 19:58:55 |
|            17 |      12 |          2 | B           | 66-67 GTO-Stock HT/Coupe       | GTO STOCK        | 2018-02-19 19:57:40 |
|            18 |      12 |          9 | J           | 67 GTO-Modified HT/Coupe       | GTO MODIFIED     | 2018-02-19 19:58:59 |
|            19 |      12 |         10 | K           | 68-74 GTO-Mod. All/Not Conv    | GTO MODIFIED     | 2018-02-19 19:59:03 |
|            20 |      12 |          3 | C           | 68-69 GTO-Stock HT             | GTO STOCK        | 2018-02-19 19:57:45 |
|            22 |      12 |          4 | D           | 70-74 GTO-Stock All/Not Conv   | GTO STOCK        | 2018-02-19 19:57:49 |
|            23 |      12 |         14 | N           | 68-71 GTO-Stock Conv           | GTO CONVERTIBLE  | 2018-02-19 20:00:22 |
|            25 |      12 |         17 | Q           | 69-71 GTO Judge-All            | GTO JUDGE        | 2018-02-19 20:01:42 |
|            26 |      12 |         19 | R           | 64-67 Tempest/Le Mans-All      | TEMPEST/LEMANS   | 2018-02-19 20:02:15 |
|            27 |      12 |         21 | T           | 1967-02 Firebird All           | FIREBIRD/TRANSAM | 2018-02-19 20:03:04 |
|            29 |      12 |         31 | V           | 1967-02 Trans Am All           | FIREBIRD/TRANSAM | 2018-02-19 20:03:07 |
|            30 |      12 |         24 | Y           | 26-74 Other Pontiac-Stock/Mod  | OTHER PONTIACS   | 2018-02-19 20:04:03 |
|            31 |      12 |         28 | AA          | 26-10 Pontiacs-Competition     | COMPETITION      | 2018-02-19 20:04:18 |
+---------------+---------+------------+-------------+--------------------------------+------------------+---------------------+

All I want to do is sort these results by "carclasstag" using some variation of the function I shared above.

I would be grateful for any tips and advice I can get.

Thank you,

Neil Page
  • 1
  • 1
  • Sort values __how__? Please add a sample of data before and after applying sorting. – u_mulder Feb 20 '18 at 17:04
  • Please, __always__ [edit] your question with required data. Do not write code or other stuff in comments. – u_mulder Feb 20 '18 at 17:26
  • @u_mulder I have updated my original post per your advice, thank you. – Neil Page Feb 20 '18 at 17:36
  • It might be easier if you try to fetch the data already in the format that you want directly from your DB. In this case, You would have a single output with a bunch of columns and you do a simple order by the column that you need. Take a look at [this question](https://stackoverflow.com/questions/12095109/select-from-multiple-tables-without-a-join). – Paulo Hgo Feb 20 '18 at 18:11
  • Read through that article, doesn't quite relate to what I'm trying to do. I'm already getting the data I need, I just need to figure out how to change my function to sort the data on one table. – Neil Page Feb 20 '18 at 20:58

0 Answers0