2017-03-31 50 views
1

我想更新我的表使用引導對話框,但我當我從服務器端檢索我的數據表,並嵌入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> 

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

回答

0

我懷疑您的查詢不是針對您打算訪問的單個行。 如果您不在桌面上查詢主鍵/唯一鍵,您應該添加LIMIT 1 - 但實際上您應該使用PK。

此外,您似乎循環查詢的所有結果。

您需要發佈更多代碼,因爲我只能根據您提供的內容做出假設。

爲什麼要將Warning元素放在頂部?

你從哪裏複製/粘貼腳本?


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

我沒有複製/粘貼我scrips,他們是從我自己的想法,我用在上面,因爲它是更新 –

+0

@smithsmith看到我更新的警告元素,你需要至少一個WHERE子句爲您的SELECT查詢。 – mickmackusa

+0

@smithsmith如果我的答案解決了您的問題,請授予它綠色的勾號,以便從未解決的問題列表中刪除此問題。 – mickmackusa

-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') 
+0

這個答案假設太多了,你必須複製我之前發佈的答案。沒有理由相信'select()'函數允許將where子句附加到字符串。事實上,它是沒有意義的,因爲有一個單獨的函數來處理來自部分 - 而且該字符串將在SELECT和WHERE之間。 – mickmackusa

+0

我該怎麼辦?一旦我將我的項目插入數據庫,我更新了hash_id列 –

+0

@smithsmith基本上,你只想顯示一個項目,對不對?如果是的話,你需要更新你的代碼,以便你只顯示你想要的特定產品。你可以顯示你的數據庫功能? –