【问题标题】:How to add class selected in select form php如何添加在选择表单php中选择的类
【发布时间】:2018-11-06 02:43:12
【问题描述】:

我想在我的选择表单中将class="selected" 添加到option

例如在$tags['tags_name'] 我有这样的数据库中的数据。

tags_name
-----------
English
Japanese
Korean
Mandarin
Indonesian

例如,在我的帖子中,我已经选择了英语、日语和印度尼西亚语并将其插入数据库。然后我用$data['tags_name'] 调用它。

所以在我的选择表单中,我想为我发布的每个tags_name 添加class="selected"

这是选择表单应该喜欢的。

<select multiple class="selectpicker" id="tags[]" name="tags[]" form="edit_post">
<option class="selected">English</option>
<option class="selected">Japanese</option>
<option>Korean</option>
<option>Mandarin</option>
<option class="selected">Indonesian</option>
</select>

因为在我的数据库发布中我有英语、日语和印度尼西亚语,所以在他们的选项中添加了class="selected"

我在这里尝试

<select multiple class="selectpicker" id="tags[]" name="tags[]" form="edit_post">
<?php foreach ($datatag as $tags): ?>
<option <?php if (isset($data['tags_name'] == $tags['tags_name'])): ?>
    <?php echo 'class="selected"'; ?>
<?php endif ?>><?php echo $tags['tags_name'] ?></option>
<?php endforeach; ?>
</select>

【问题讨论】:

    标签: php explode implode


    【解决方案1】:

    替换:

    if (isset($data['hardsub'] == $tags['tags_name']))
    

    与:

    if ($data['hardsub'] == $tags['tags_name'])  // no `isset` here
    

    如果$data 是一个数组,则需要in_array 或类似方法。

    strpos 示例:

    if (strpos($data['hardsub'], $tags['tags_name']) !== false)
    

    【讨论】:

    • 不工作,为什么?因为在$data['hardsub'] 它的打印English, Japanese and Indonesian 但在$tags['tags_name'] 打印一种语言,所以在手册中 English、Japanese 和 IndonesiaEnglish 不同跨度>
    • 然后使用strpos。或通过,and 展开以获取值数组。
    • 是的,我知道必须使用 strpost 或爆炸,但我不知道如何使用它。哈哈
    • 添加了strpos 示例。更多信息 - 请参阅 php 手册。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2014-03-03
    • 1970-01-01
    • 2018-07-15
    • 2021-09-23
    • 2023-03-07
    • 2018-02-19
    相关资源
    最近更新 更多