我想编辑/更新一个项目的值,但我不能从数据库中获得它的值。我的数据库中有这样的内容
Products table
-id
-category_id
-sub_category_id
-name
Category table
-id
-category
Sub Category table
-id
-sub_category
在我的表格中,我有这样的东西。如果我的产品表中存在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;
}
对于要在选择框中打印的每个类别,检查类别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>';
}
?>
选择产品的类别名称。
如果你的子类别有一个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');
}
并在视图中选择产品的子类别:
<?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来加载相应的子类别,但这离这个问题的主题太远了,在这里解释
当使用相同的表单进行插入和更新时,检查$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>';
}
?>