【问题标题】:How to display selected option get by id when update a data in codeigniter更新codeigniter中的数据时如何显示通过id获取的选定选项
【发布时间】:2016-03-06 15:02:57
【问题描述】:

我想在选择选项标签中显示一个选定的值,带有特定的 id, 我有一个帖子表

+---------+-------------+---------------+-------------+
| post_id | post_title  | post_content  | category_id |
+---------+-------------+---------------+-------------+
| 1       | Title One   | Content One   | 1           |
+---------+-------------+---------------+-------------+
| 2       | TitleTwo    | Content Two   | 1           |
+---------+-------------+---------------+-------------+
| 3       | Title Three | Content Three | 2           |
+---------+-------------+---------------+-------------+

然后我有一个类别表

+-------------+----------------+
| category_id | category       |
+-------------+----------------+
| 1           | Category One   |
+-------------+----------------+
| 2           | Category Two   |
+-------------+----------------+
| 3           | Category Three |
+-------------+----------------+

我用选择选项标签显示类别

控制器添加

function add(){
    if ($this->input->post('submit'){
        $data = array(
            'post_title' => $this->input->post('post_title'),
            'post_content' => $this->input->post('post_content'),
            'category_id' => $this->input->post('category_id'));
        $this->db->insert('post_table',$data);
    }
    $data[cats] = $this->db->get(category_table)->result();
    $this->load->view('view_add_post');
}

查看添加,

<form method="post" action="<?php echo base_url();?>index.php/post/add">
    <input type="text" name="post_title">
    <textarea name="post_content"></textare>
    <select name="category_id">
        <?php foreach ($cats as $category) { ?>
        <option value="<?php echo $category->category_id ?>"><?php echo $category->category?>
        <?php } ?>
    </select>
    <input type="submit" name="submit" value="Save">
</form>

控制器编辑

function edit($id){
    $data['post'] = $this->db->get_where('post_table' array('post_id',$id))->row();
    $data['cats'] = $this->db->get('category_table)->result();
    $this->load->view('view_edit_post',$data)
}

编辑视图

这是我想在下拉选择选项中显示类别的问题,其中选择的是我输入的第一个,例如我已经输入了类别 id 为 1 的帖子,然后当我在选择选项中编辑类别 1 时自动选中,怎么做?

<select name="category_id">
    <?php foreach ($cats as $category) { ?>
    <option value="<?php echo $category->category_id ?>"><?php echo $category->category?>
    <?php } ?>
</select>

// the selected option is automatically

对不起,我的英语不好。

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    更改此代码

    <select name="category_id">
    <?php foreach ($cats as $category) { ?>
    <option <?php if($category->category_id == "your desired id"){ echo 'selected="selected"'; } ?> value="<?php echo $category->category_id ?>"><?php echo $category->category?> </option>
    <?php } ?>
    </select>
    

    其实我给你加了一个提示,因为你的问题不是很清楚。

    您必须为所选类别输入所需的 id 以匹配两者

    【讨论】:

    • 我是多么愚蠢,很简单,谢谢,对不起,我的英语很差,反正我看不懂我在写什么,哈哈哈我爱你先生。
    • 很高兴为您提供帮助:)
    • 先生。 Uzair 有邮箱吗?我又遇到问题了,请帮帮我!如果是多个,如何显示选定的菜单?
    • 请添加一个新问题并在那里标记我,就像@yuliantosaparudin 我已经标记了你。
    【解决方案2】:

    您可以通过在option 中使用此条件来使用selected=""

    <select name="category_id"> 
    
    <?php foreach ($cats as $category) { ?>    
    <option <?=( $category->category == $post[0]->category_id ? 'selected=""' : '')?> value="<?php echo $category->category_id ?>">
    
    <?php echo $category->category?> <?php } ?> 
    </option>
    </select>
    

    在您的示例中,您缺少结束标记 &lt;/option&gt;

    【讨论】:

      【解决方案3】:
          <div class="form-group">
            <label>Category:</label>
             <select class="form-control" name="roomtype" required>
               <option disabled selected value>Category</option>
                <?php   foreach($roomstype as $roomstypedatas) { ?>
                                     
       <option value="<?php echo $roomstypedatas['id']?>"
           <?php if($editroomsdata['roomtype']==$roomstypedatas['id']){ echo "selected"; } ?> >
           <?php echo $roomstypedatas['type']?>
       </option>  
                <?php  } ?>
                </select>
               </div> 
      

      【讨论】:

        猜你喜欢
        • 2020-06-15
        • 1970-01-01
        • 2020-03-18
        • 2019-09-13
        • 2023-04-06
        • 2011-11-17
        • 2021-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多