在PHP页面中,我的工作原理非常完美,我希望按照'顺序排列。表数组中的一个表。我使用此函数从多个查找表中提取字段值,我用它来填充Web表单中的选择下拉字段。同样,表单工作正常,但我想在一个特定的下拉列表中对值进行排序,这是由“evtcarclasses'表
我在这里搜索了"表格数组","在数组中排序值"和许多其他关键短语。我在Google和其他来源也做过同样的事情。所有搜索引导我朝着不考虑在单个数组中使用表和值集合的方向发展。
以下是我的功能,收集所有相关数据。有没有办法对从" evtcarclasses"中选择的值进行排序?表
/**
* 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;
}
这里是现在收集的表格数据:
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 |
+---------------+---------+------------+-------------+--------------------------------+------------------+---------------------+
我想要做的就是通过" carclasstag"对这些结果进行排序。使用我上面分享的函数的一些变体。
如果我能得到任何提示和建议,我将不胜感激。
谢谢,