在下拉列表中获取值,Codeigniter

c7rzv4ha  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(133)

我想编辑/更新某个项目的值,但无法从数据库中获取它的值。我的数据库中有类似的内容

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

在我的表单中,我有类似this的内容。如果products表中的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;
}
yjghlzjz

yjghlzjz1#

对于您在选择框中打印的每个类别,检查类别ID是否与产品的类别ID相同。如果相同,请将“已选择”添加到选项中:

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

这将选择产品的类别名称。
如果您的子类别有parent_id,则获取某个类别的所有子类别的方法将类似于(model):

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');
}

并在视图中选择产品的子类别:

<?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,以便在categoresselect的值发生变化时加载相应的子类别,但这与本问题的主题偏离得太远,无法在此进行解释。
使用相同的表单进行插入和更新时,请先检查$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>';
         }
       ?>

相关问题