在下拉菜单中获取价值,Codeigniter

Get value in dropdown, Codeigniter

我想 edit/update 一个项目的值,但我无法从数据库中获取它的值。我的数据库中有这样的东西

Products table
   -id
   -category_id
   -sub_category_id
   -name
Category table
   -id
   -category
Sub Category table
   -id
   -sub_category

在我的表单中,我有类似 this 的东西。如果我的产品table中的category_id存在,应该显示其对应的分类名称怎么办?我试图在我的表单中使用类似的东西

<div class="form-group">
    <label for="" class="control-label"> Category</label>
        <select name="category_id" id="category" class="custom-select select">
            <option value="">Select Category</option>
               <?php
                   foreach ($category as $row) {
                       echo '<option value="' . isset($products->category_id) ? $row['id'] : ''  . '">' . $row['category'] . '</option>';
                 }
               ?>
      </select>
   </div>
<div class="form-group">
 <label for="" class="control-label">Sub Category</label>
      <select name="sub_category_id" id="subcategory" class="custom-select select">
<option value="<?php isset($products->sub_category_id) ? $products->sub_category_id : ''; ?>">Select Sub Category</option>
</select>
</div>

这是我的控制器

 public function edit_product($id)
{
    $data['title'] = 'Update Product';

    $this->load->view('../admin/template/admin_header');
    $products = new Admin_model;
    $data['category'] = $this->Admin_model->category();
    $data['products'] = $products->edit_product($id);
    $this->load->view('../admin/template/admin_topnav');
    $this->load->view('../admin/template/admin_sidebar');
    $this->load->view('../admin/products/manage_product', $data);
    $this->load->view('../admin/template/admin_footer');
}

还有我的模特

public function edit_product($id)
{
    $query = $this->db->get_where("products", array('id' => $id));
    return $query->row();
}

public function category()
{
    $response = array();

    $this->search(array(), 'date_created');

    $this->db->select('*');
    $query = $this->db->get('categories');
    $response = $query->result_array();

    return $response;
}

对于您在 select 框中打印的每个类别,检查类别 ID 是否与产品的类别 ID 相同。如果相同,则在选项中添加'selected':

           <?php
               foreach ($category as $row) {
                   echo '<option value="' . $row['id'] . '"';
                   if ($row['id'] == $products->category_id) {
                        echo ' selected'
                   }
                   echo '>' . $row['category'] . '</option>';
             }
           ?>
           

这 select 是产品的类别名称。


如果您的子类别有 parent_id,获取特定类别的所有子类别的方法将如下所示(模型):

public function sub_category($parent_id) {
    return $this->db->get_where('subcategories', array('parent_id' => $parent_id)->result_array();
}

控制器:

 public function edit_product($id)
{
    $data['title'] = 'Update Product';

    $this->load->view('../admin/template/admin_header');
    $products = new Admin_model;
    $data['products'] = $products->edit_product($id);
    $data['category'] = $this->Admin_model->category();
    $data['subcategory'] = $this->Admin_model->sub_category($data['products']->category_id);
    $this->load->view('../admin/template/admin_topnav');
    $this->load->view('../admin/template/admin_sidebar');
    $this->load->view('../admin/products/manage_product', $data);
    $this->load->view('../admin/template/admin_footer');
}

并且 select 视图中的产品子类别:

       <?php
           foreach ($subcategory as $row) {
               echo '<option value="' . $row['id'] . '"';
               if ($row['id'] == $products->sub_category_id) {
                    echo ' selected'
               }
               echo '>' . $row['sub_category'] . '</option>';
         }
       ?>
       

您可能还需要在视图中添加一些 Javascript 以在类别 select 的值更改时加载相应的子类别,但这离这个问题的主题太远了在这里解释一下。


插入和更新使用同一个表单时,先检查$products是否存在:

       <?php
           foreach ($category as $row) {
               echo '<option value="' . $row['id'] . '"';
               if (isset($products) && $row['id'] == $products->category_id) {
                    echo ' selected'
               }
               echo '>' . $row['category'] . '</option>';
         }
       ?>