0

I want to edit/update the value of an item but I can't get its value from database. I have something like this in my database

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

In my form, I have something like this. What should I do if the category_id in my products table exist, it should display its corresponding category name? I tried to use something like this in my form

<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> 

Here's my controller

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

And my model

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

1 Answer 1

1

For each category that you're printing in the select box, check if the category id is the same as the product's category id. If they're the same, add 'selected' to the option:

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

This selects the product's category name.


If your subcategory has a parent_id, the method to get all the subcategories for a certain category would look something like this (model):

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

Controller:

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

And select the product's subcategory in the view:

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

You would probably also need to add some Javascript to your view to load the corresponding subcategories when the value of the category select is changed, but that's going too far off topic for this question to explain here.


When using the same form for insert and update, check if $products exists first:

 <?php foreach ($category as $row) { echo '<option value="' . $row['id'] . '"'; if (isset($products) && $row['id'] == $products->category_id) { echo ' selected' } echo '>' . $row['category'] . '</option>'; } ?> 
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @Marleen, but how should I get the value of the subcategory?
I tried to add this in my Controller $data['subcategory'] = $this->Admin_model->sub_category(); And tried this in the form <label for="<?php if($subcategory->parent_id == $category->id && $products->sub_category_id == $subcategory->id){ echo 'selected'; } $subcategory['sub_category']; ?>" class="control-label">Sub Category</label> Where the parent_id = category->id but still won't work
Sorry for asking late but I'm having error in my form when I'm going to use it for inserting new data. On the category dropdown, it says A PHP Error was encountered Severity: Notice Message: Undefined variable: products Filename: products/manage_product.php Line Number: 33 so I tried to add something like this on controller $data['products'] = $this->Admin_model->products(); but it gave me an error Trying to get property 'id' of non-object
When using the same form for insert and update, you need to check if $products exists before doing the comparision in the loop, see my updated answer above.
PHP can't react to user actions without re-loading the page. If you want to update the subcategories select when you change the category select, you need to use JavaScript. That's going too far off topic to explain here, search for "dependant dropdown" and "ajax".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.