Codeigniter foreach选择菜单'选定'选项

问题描述 投票:0回答:1

我正在为App创建主题选择选项。我希望用户从选择选项中选择他们想要的主题,无论是浅色还是深色。

我将主题名称存储在数据库中,如下所示:

database

然后我调用模型中的所有主题并使用以下代码返回数组:

public function getThemes() {
    $query = $this->db->query('SELECT * FROM theme');
    return $query->result_array();
}

我正在使用我的构造函数来获取数据并将其渲染到我的设置视图中,如下所示:(我在$ this-> data中同时发送一些内容,这就是为什么它在数组中,但我将代码删除了)

$this->data = [
            'themes' => $this->model_setting->getThemes()
        ];
        $this->render('backend/standart/administrator/setting/setting_general', $this->data);

在我的HTML视图中,我有以下HTML,它成功显示了正确的2个主题:

<div class="col-sm-12">
                <label>Select Your Desired Theme</label><br>
                <select class="form-control" name="site_color_theme" id="site_color_theme">
                  <?php foreach ($themes as $theme): ?>
                    <option value="<?= $theme['theme']; ?>" ><?= $theme['name']; ?></option>
                  <?php endforeach; ?>
                </select>
            </div>

当我保存设置表单时,我正在使用主题名称更新数据库中的选项表。 Html Body Class调用此选项表并查找所选主题并使用它来呈现特定的CSS样式表。

我遇到的问题是当我保存设置时,选择选项不显示保存的主题名称以显示这是所选的活动主题。

当用户访问设置页面以选择其他主题时,如何让选择选项显示所选主题?

database codeigniter select option
1个回答
0
投票
  1. 你应该在active_theme表中有theme
  2. 当表格下拉active_theme被提交时,1设置为site_color_theme(其余主题记录设置为0
  3. 你应该使用Codeigniter Form Helper来做到这一点:https://www.codeigniter.com/user_guide/helpers/form_helper.html#form_dropdown
// $themes = Theme list array [id => theme-name, id => theme-name]
// $active_theme = Record of "active_theme" with value equal to "1" 
// $extra = Custom HTML attributes e.g. class="something" onclick="function(js)"

echo form_dropdown('site_color_theme', $themes, $active_theme, $extra);
© www.soinside.com 2019 - 2024. All rights reserved.