How to show selected value from database and show in dropdownlist using codeigniter and bootstrap

我的梦境 提交于 2020-01-03 05:07:14

问题


When I select the all data of a product by it's product_id. and load product data in a form for an update. but here I cannot set selected drop-down list value in this case.

here, is my view

      <form action="<?php //echo base_url().'admin_product/pro_update/'?>" method="post" class="form-horizontal">

        <div class="control-group">
          <label class="control-label">Title :</label>
          <div class="controls">
            <input type="text" class="span5 m-wrap" placeholder="Product title" name="title" value="<?php echo $item[0]['p_title']?>" />

           <h6 style="color:red"><?php echo form_error('title')?></h6>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Category :</label>
          <div class="controls">
            <select name="cat" id="cat">

              <option value="">--Select Category--</option>

            <?php foreach($cat as $c):?>

    <option value="<?php echo $c->cat_id?>"><?php echo $c->cat_name;?>//selected set value here</option>
   <?php endforeach;?>

            </select>
            <h6 style="color:red"><?php echo form_error('cat')?></h6>
          </div>
        </div>

here is my controller code,

 public function pro_edit($id)
{
    $p['item']=$this->ap->product_update($id);
    $p['cat']=$this->ap->up_cat();
    $p['color']=$this->ap->up_color();  
   $this->load->view('layouts/header',$p);
   $this->load->view('admin/product_update',$p);
   $this->load->view('layouts/footer',$p);
}

and model code,

public function product_update($id)
{
    $this->db->select('p_title,p_description,p_category,p_stock_quantity,p_pprice,p_sprice,p_size_variant,cat_name,v_color,v_size,p_id');
    $this->db->from('tbl_product');
    $this->db->join('tbl_category','tbl_product.p_category=tbl_category.cat_id');
    $this->db->join('tbl_variant','tbl_product.p_color_variant=tbl_variant.v_id');
    $this->db->where('p_id',$id);
    $query=$this->db->get();


        return $query->result_array();

}

回答1:


Assumptions

p_category is your category id

Code

<select name="cat" id="cat">

    <option value="">--Select Category--</option>

    <?php 
        foreach($cat as $c):?>

        <option value="<?php echo $c->cat_id?>" <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>>
            <?php echo $c->cat_name;?>

            </option>
    <?php 
        endforeach;
    ?>

</select>

Explain

Q - What this if do in above <?php echo ($item[0]['p_category'] == $c->cat_id) ? 'selected' : '' ?>

A - Assume $item[0]['p_category'] has value 2 so when ever $c->cat_id equesl to that value its print selected keyword

Ex:

<select>
    <option value="1">aa</option>
    <option value="2" selected>bb</option>
    <option value="3">cc</option>
    <option value="4">dd</option>
</select>


来源:https://stackoverflow.com/questions/46743947/how-to-show-selected-value-from-database-and-show-in-dropdownlist-using-codeigni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!