modal does not display the data separately

advertisements

I want to update my table using bootstrap dialog , but I when I retrieve my datatable from server side and embed php code into my modal to show the table data into the modal, and choose what the user wants to update in that form. it is showing the data from every row of my table into the modal , how can I fix it?

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();
            }
        }]
    });
});

model product

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

modal img


I suspect your query is not targeting the single row that you intend to access. If you are not querying on your table Primary/Unique Key, you should add LIMIT 1 --but really you should be using the PK.

Also, you seem to be looping through all of the results from the query.

You'll need to post more of your code, because I can only make assumptions from what you've provided.

Why are you putting the Warning element at the top?

Where did you copy/paste your scripts from?


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

You will need some sort of $this->db->where() statement here that references the appropriate row id.

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

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