在Codeigniter中的从属选择框中显示数据库中的特定数据

时间:2018-01-02 13:43:03

标签: php jquery mysql codeigniter

如何将数据库中的数据加载到选择框中?此选择框依赖于另一个选择框。

我需要更新我的产品的详细信息。当我单击表中的特定产品时,产品详细信息将加载到我的更新视图中。一切都很好,除了一个选择框,即size选择框,数据没有显示。

我尝试了很多解决方案,但没有一个能够解决问题。

这是我的产品表视图:

<div class="col-sm-10" id="main">

    <div class="table-wrapper">
       <div id="content">
<legend class="text-danger"><h1>Product List</h1></legend>
            <?php   $tableAttr = array(
                    'table_open' => '<table class="table table-responsive table-striped table-hover" id="item_tbl">',
                    );
                $item_table = $this->table->set_heading('No.','PRODUCT CODE','PRODUCT DESCRIPTION','BRAND', 'CATEGORY', 'SIZE','ORIGINAL PRICE','ACTION');
                $item_table = $this->table->set_template($tableAttr);
                $num = 0;
                foreach ($items as $item) {
                    $itemName = urlencode($item->prod_desc);
                    $num++;
                    $item_table = $this->table->add_row($num, $item->prod_code, $item->prod_desc, $item->brand_name,$item->category_name,$item->size,'₱'. $item->original_price,"

                        <a href='".base_url("item/update/$itemName")."'><button class='btn btn-info btn-sm'>EDIT</button></a> 
                        ");
                }
                echo $this->table->generate($item_table);  ?>
                </div>

</div>                            
</div>  

因此,当我点击编辑按钮时,另一个形式为product_update_view.php

<div class="col-sm-10" id="main" style="padding: 20px;">
    <?php echo form_open("item/item_update/$item->prod_id");
     echo form_fieldset('<h3 class="text-info">Update Item</h3>');  ?>
    <input type="hidden" name="cur_code" value="<?php echo $item->prod_code ?>">
    <input type="hidden" name="cur_name" value="<?php echo $item->prod_desc ?>">
<input type="hidden" name="cur_category" value="<?php echo $item->category_name ?>">
<input type="hidden" name="cur_brand" value="<?php echo $item->brand_name ?>">
<input type="hidden" name="cur_size" value="<?php echo $item->size ?>">
<input type="hidden" name="cur_price" value="<?php echo $item->original_price ?>">

    <div class="form-group">
    <label for='prod_code'>Product Code:</label>
    <input type="text" name="up_code" class="form-control" value="<?php echo $item->prod_code; ?>">
</div>
<div class="form-group">
    <label for='prod_name'>Product Description:</label>
    <input type="text" name=up_name" class="form-control" value="<?php echo $item->prod_desc; ?>">
</div>
    <div class="form-group">
    <label for='prod_name'>Brand:</label>
<select name="brand" class="form-control">
<option value="Select Brand" selected="selected">Select Brand</option>


    <?php foreach($brand as $br):?>
<option value="<?php echo $br->brand_id; ?>" <?php if ($br->brand_id == $item->brand_id) {echo "selected='selected'";} ?>><?php echo $br->brand_name?></option>
<?php endforeach;?>
</select>
    </div>      

      <div class="form-group">
                    <label for="category">Category</label>
                    <select class="form-control" name="category" id="category">
                        <option value="Select Brand"> Select Category</option>
                        <?php foreach($category as $cat): ?>
                            <option value="<?php echo $cat->category_id; ?>" <?php if ($cat->category_id == $item->category_id) {echo "selected='selected'";} ?>><?php echo $cat->category_name; ?></option>
                        <?php endforeach; ?>
                    </select>
                  </div>
                  <div class="form-group">
                    <label for="sizes">Size:</label>
                     <select class="form-control" name="category" id="sizes">
                        <option value="Select size"> Select Size</option>

                    </select>
                  </div>

<div class="form-group">

    <label for='prod_price'>Price:</label>
    <input type="text" name="price" class="form-control" value="<?php echo $item->original_price; ?>">
</div>
<div class="form-group">
        <input type="submit" name="submit_account" class="btn btn-primary" value="Save" >
    </div>
</div>


<script src="<?php echo base_url() ?>dropdown/js/jquery.js"></script>


    <script src="<?php echo base_url() ?>dropdown/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
           $('#category').on('change', function(){
                var category_id = $(this).val();
                if(category_id === '')

                    $.ajax({
                        url:"<?php echo base_url() ?>welcome/get_sizes",
                        type: "POST",
                        data: {'category_id' : category_id},
                        dataType: 'json',
                        success: function(data){
                           $('#sizes').html(data);
                        },
                        error: function(){
                            alert('Error occur...!!');
                        }
                    });
                }
           });

        });
    </script>
</div>

<?php echo form_close(); ?>

控制器(item.php):

public function update($name) {
                 $this->load->model('Dependent_model', 'dep_model', TRUE);
    $this->load->model('brand_model');
            $data['category'] = $this->dep_model->get_category_query();
    $data['brand'] = $this->brand_model->getBrandName();
    $this->load->model('item_model');
    $data['item'] = $this->item_model->item_info($name);
    $this->load->view('header');
    $this->load->view('side_menu');
    $this->load->view('product_update_view.php',$data);
    $this->load->view('footer');
}

从属选择框的控制器(welcome.php):

class Welcome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Dependent_model', 'dep_model', TRUE);
    }
    public function index()
    {
        $data['category'] = $this->dep_model->get_category_query();
        $this->load->view('new_product', $data);
    }
    public function get_sizes()
    {
        $category_id = $this->input->post('category_id');
        $sizes = $this->dep_model->get_sizes_query($category_id);
        if(count($sizes)>0)
        {
            $pro_select_box = '';
            $pro_select_box .= '<option value="Select Size">Select Size</option>';
            foreach ($sizes as $sz) {
                $pro_select_box .='<option value="'.$sz->size_id.'">'.$sz->size.'</option>';
            }
            echo json_encode($pro_select_box);
        }
    }
}

模型(item_model.php):

public function item_info ($itemName) {
        $this->load->database();

        $this->db->select('products.*,category.category_name,sizes.size,brand.brand_name')
                        ->from('products')
                        ->from('category');
                      $this->db->from('sizes');
                        $this->db->from('brand');
                        $this->db->where('products.category_id = category.category_id');
                   $this->db->where('products.size_id = sizes.size_id');
                   $this->db->where('products.brand_id = brand.brand_id');
                  $this->db->where('prod_desc', urldecode($itemName));  
                $result=$this->db->get();


        return $result->row();
}

从属选择框模型(dependent_model):

<?php
class Dependent_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    public function get_category_query()
    {
        $query = $this->db->get('category');
        return $query->result();
    }
    public function get_sizes_query($category_id)
    {
        $query = $this->db->get_where('sizes', array('category_id' => $category_id));
        return $query->result();
    }

}

问题是它没有在product_update视图中显示特定产品的大小。

1 个答案:

答案 0 :(得分:0)

替换此jquery。

middleware_home_dir