modal不单独显示数据

时间:2017-03-31 23:34:38

标签: php jquery twitter-bootstrap-3

我想使用bootstrap对话框更新我的表,但是当我从服务器端检索我的数据表并将php代码嵌入到我的模态中以将表数据显示到模态中时,我选择用户想要以该形式更新的内容。它将我表格中每一行的数据显示到模态中,我该如何解决?

form_modal

<form id="product_update">
   <div class="row">
   <div class="form-group">
   <?php foreach ($product as $value): ?>
   <div class="col-xs-12">
      <label for="description">Name: </label>
      <input type="text" class="form-control" id="description" name="description" title="product description" value="<?php echo $value['descripcion']; ?>" required>
      <div>
      </div>
   </div>
   <div class="row">
      <div class="form-group">
         <div class="col-xs-8 col-sm-6">
            <label for="cost_price">Cost Price:</label>
            <div class="input-group"> <span class="input-group-addon">$</span>
               <input type="text" class="form-control input-group-lg reg_name" id="cost_price" name="cost_price" title="cost_price"  placeholder="Last name" value="<?php echo $value['precio_compra']; ?>" required>
            </div>
         </div>
         <div class="col-xs-8 col-sm-6">
            <label for="selling_price">Selling price: </label>
            <input type="text" class="form-control input-group-lg reg_name" id="selling_price" name="selling_price" title="selling_price" placeholder="Last name" value="<?php echo $value['precio_venta']; ?>" required>
         </div>
      </div>
   </div>
   <div class="row">
      <div class="form-group">
         <div class="col-xs-8 col-sm-6">
            <label for="wprice">Wholeprice: </label>
            <input type="text" class="form-control" id="wprice" name="wprice" title="wprice" value="<?php echo $value['precio_mayoreo']; ?>" required>
         </div>
         <div class="col-xs-8 col-sm-6">
            <label for="min_stock">Min stock: </label>
            <input type="text" class="form-control" id="min_stock" name="min_stock" title="min_stock" value="<?php echo $value['existencia_minima']; ?>" required>
         </div>
      </div>
   </div>
   <div class="row">
      <div class="form-group">
         <div class="col-xs-8 col-sm-6">
            <label for="stock">Stock: </label>
            <input type="text" class="form-control" id="stock" name="stock" title="stock" value="<?php echo $value['existencia']; ?>" required>
         </div>
         <div class="col-xs-8 col-sm-6">
            <label for="max_stock">Max stock: </label>
            <input type="text" class="form-control" id="max_stock" name="max_stock" title="max_stock" value="<?php echo $value['existencia_maxima']; ?>" required>
         </div>
      </div>
   </div>
  <?php endforeach ?>
   <div class="row">
      <div class="form-group">
         <div class="col-xs-8 col-sm-6">
            <label for="provider">Provider: </label>
            <select name="select-provider" id="select-provider">
                <option value="0">Select a provider</option>
                <?php foreach ($data as $value): ?>
                   <option value="<?php echo $value['id']; ?>"><?php echo $value['first_name'].' '.$value['last_name'] ?></option>
                <?php endforeach ?>
            </select>
         </div>
      </div>
   </div>
</form>

modal js

$('#example tbody').on('click', 'a', function(event) {
    event.preventDefault();
    var data = table.row($(this).parents('tr')).data();
    hash = data[0];
    $("#product_update").validate();
    BootstrapDialog.show({
        type: BootstrapDialog.TYPE_WARNING,
        message: function(dialog) {
            var $message = $('<div></div>');
            var pageToLoad = dialog.getData('pageToLoad');
            $message.load(pageToLoad);

            return $message;
        },
        data: {
            'pageToLoad': URL_GET_VIEW_PRODUCT_UPDATE
        },
        closable: false,
        buttons: [{
            id: 'btn-ok',
            cssClass: 'btn-primary',
            icon: 'glyphicon glyphicon-send',
            label: ' Save',
            action: function(e) {
                var description = $('#description').val();
                var description = $('#description').val();
                var cost_price = $('#cost_price').val();
                var selling_price = $('#selling_price').val();
                var wprice = $('#wprice').val();
                var min_stock = $('#min_stock').val();
                var stock = $('#stock').val();
                var max_stock = $('#max_stock').val();
                var provider_id = $('#select_provider').val();
                if ($("#product_update").valid()) {
                    $.ajax({
                        url: URL_GET_UPDATE_PRODUCT,
                        type: 'POST',
                        data: { hash: hash, provider_id: provider_id, description: description, cost_price: cost_price,  selling_price: selling_price, wprice: wprice, min_stock: min_stock,   stock: stock, max_stock: max_stock },
                        success: function(data) {
                            console.log(data);
                            if (data.msg == 'successfully updated') {
                                $('#product_update')[0].reset();
                                table.ajax.reload();
                            } else if (data.min_stock == 'el stock no puede ser mayor al min') {
                                BootstrapDialog.show({
                                    type: BootstrapDialog.TYPE_DANGER,
                                    message: 'el stock no puede ser mayor al min'
                                });
                            }
                        }
                    });
                }
            }
        },{
            id: 'btn-cancel',
            cssClass: 'btn-danger',
            icon: 'glyphicon glyphicon-remove',
            label: ' Cancel',
            action: function(e) {
                e.close();
            }
        }]
    });
});

模型产品

public function datatable(){
        $this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status');
        $this->db->from('storelte_articulos');
        $query = $this->db->get();
        return $query->result_array();
    }
    public function isExistsProduct($data){
        $this->db->select('descripcion');
        $this->db->from('storelte_articulos');
        $this->db->where('descripcion',$data['descripcion']);
        $query = $this->db->get();
        return $query->num_rows() == 0 ? false : true;
    }

    public function addProduct($data){
        $query = 'UPDATE storelte_articulos SET hash_id = MD5(id) WHERE id = LAST_INSERT_ID()';
        $this->db->insert('storelte_articulos',$data);
        $this->db->query($query);
    }

    public function updateProduct($data) { 
        $this->db->where('md5(id)',$this->input->post('hash'));
        $this->db->update('storelte_articulos',$data); 
    }

     public function get_product() {
    $this->db->select('codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia_maxima,existencia');
    $this->db->from('storelte_articulos');
    $this->db->where('md5(id)', $this->input->post('hash'));
    $query = $this->db->get();
    return $query->result_array();
}

模态img modal_img

2 个答案:

答案 0 :(得分:0)

我怀疑您的查询不是针对您打算访问的单行。 如果您没有查询表主键/唯一键,则应添加LIMIT 1 - 但实际上您应该使用PK。

此外,您似乎正在遍历查询中的所有结果。

您需要发布更多代码,因为我只能根据您提供的内容做出假设。

为什么要将警告元素放在顶部?

您从哪里复制/粘贴脚本?

public function datatable(){
    $this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status');
  

这里需要某种$this->db->where()语句引用相应的行ID。

喜欢:$this->db->where('id',$this->input->post('id'));

    $this->db->from('storelte_articulos');
    $query = $this->db->get();
    return $query->result_array();
}

答案 1 :(得分:-1)

$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status');

您应该在此处添加WHERE子句。基本上,如果您不在该子句中添加任何类型的过滤,则选择所有行。例如:

$this->db->select('hash_id,codigo,descripcion,precio_compra,precio_venta,precio_mayoreo,existencia_minima,existencia,existencia_maxima,storelte_articulos.status' WHERE hash_id='YOURHASHID')