简体   繁体   English

使用optgroup从SQL查询中建立选择

[英]Building a select with optgroup from a SQL query

I have the following table: 我有下表:

___Titles ___Titles

| TIT_Id | TIT_en | TIT_Popularity |
| 1      | Sir    | High           |
| 2      | Madam  | High           |
| 2      | Emir   | Low            |

How can I have this select ? 我该如何选择?

<select>
  <optgroup label="High">
    <option value='1'>Sir</option>
    <option value='2'>Madam</option>
  </optgroup>
  <optgroup label="Low">
    <option value='3'>Emir</option>
  </optgroup>
</select>

I tried: 我试过了:

SELECT TIT_Id, TIT_en FROM ___Titles
GROUP BY TIT_Popularity
ORDER BY TIT_en ASC

Thanks. 谢谢。

How about this. 这个怎么样。 Fetch your results using a query like this: 使用以下查询来获取结果:

SELECT * FROM ___Titles ORDER BY TIT_Popularity;

This will essentially group your results together by their TIT_Popularity . 这实际上将根据您的结果将它们的TIT_Popularity分组在一起。 You can then iterate over them like this: 然后,您可以像这样遍历它们:

echo "<select>\n";

$currentGroup = null;
foreach( $results as $result ) {
    // start a new optgroup
    if( $currentGroup == null || $result->TIT_Popularity != $currentGroup ) {
        // end the previous group
        if( $currentGroup != null ) {
            echo "</optgroup\n>";
        }

        // start a new group
        echo "<optgroup label='{$result->TIT_Popularity}'>\n";

        $currentGroup = $result->TIT_Popularity;
    }

    echo "<option value='{$result->TIT_Id}'>{$result->TIT_en}</option>\n";
}

// end the last opt group
if( $currentGroup != null ) echo "</optgroup>\n";


echo "</select>\n";

Alternatively, you could just process the array from the selected db (since the queries are not inside the question), and just build them and reorder them on that newly created array (provided the popularity is just high/low). 或者,您可以只处理所选数据库中的数组(因为查询不在问题内),然后构建它们并在该新创建的数组上对其进行重新排序(前提是受欢迎程度高/低)。 Consider this example: 考虑以下示例:

<?php

// dummy data, since no values are provided on the question
$values_from_db = array(
    array('TIT_Id' => 3, 'TIT_en' => 'Emir', 'TIT_Popularity' => 'Low'),
    array('TIT_Id' => 4, 'TIT_en' => 'Test', 'TIT_Popularity' => 'Low'),
    array('TIT_Id' => 1, 'TIT_en' => 'Sir', 'TIT_Popularity' => 'High'),
    array('TIT_Id' => 2, 'TIT_en' => 'Madam', 'TIT_Popularity' => 'High'),

);

$sorted_values = array();
foreach($values_from_db as $key => $value) {
    $sorted_values[$value['TIT_Popularity']][] = array(
        'TIT_Id' => $value['TIT_Id'],
        'TIT_en' => $value['TIT_en'],
    );
}

ksort($sorted_values);

?>

<select name="whatever">
    <option selected disabled>Select Value</option>
<?php foreach($sorted_values as $key => $value): ?>
    <optgroup label="<?php echo $key; ?>">
        <?php foreach($value as $index => $element): ?>
            <option value="<?php echo $element['TIT_Id']; ?>"><?php echo $element['TIT_en']; ?></option>
        <?php endforeach; ?>            
    </optgroup>
<?php endforeach; ?>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM