2016-10-04 133 views
2

我正在使用JavaScript和HTML進行更新。我想把這些值放在一個下拉列表中。但我無法將所有其他數據循環到下拉列表中。我想用所有的值在其中創建一個HTML下拉菜單,以便我可以從下拉菜單中選擇其他選項。我從數據庫中獲取以前的值。如何在選項中獲得其餘類別?從數據庫中獲取數據並將其放入選擇列表

控制器

public function productlist($product_id) 
{ 
    $this->load->model('Productmodel'); 
    $response=array(); 
    $response = $this->Productmodel->getproductlistbyid($product_id); 
    echo json_encode($response); 
} 

型號

public function getproductlistbyid($product_id) 
{ 
    $returnarray = array(); 

    $sql = "select id, product_name, image, b.name as bname, c.name as cname, a.category_id as acategory_id, a.subcat_id as asubcat_id, description, qty, color, orginal_price, offer_price from tbl_product as a inner JOIN category as b on a.category_id = b.category_id inner JOIN category as c on a.category_id = c.fk_parent_id where id = ?"; 

    $query = $this->db->query($sql, array($product_id)); 

    if($query->num_rows()) 
    { 
     $rows = $query->result(); 

     foreach($rows as $temp) 
     { 
      $returnarray[0] = $temp->id; 
      $returnarray[1] = $temp->product_name; 
      $returnarray[2] = $temp->image; 
      $returnarray[3] = $temp->bname; 
      $returnarray[4] = $temp->cname; 
      $returnarray[5] = $temp->description; 
      $returnarray[6] = $temp->qty; 
      $returnarray[7] = $temp->color; 
      $returnarray[8] = $temp->orginal_price; 
      $returnarray[9] = $temp->offer_price; 
      $returnarray[10] = $temp->acategory_id; 
      $returnarray[11] = $temp->asubcat_id; 
     } 

    } 

    return $returnarray; 

} 

視圖(腳本)

<script> 
function editproduct(product_id) 
{ 
var myurl="<?php echo base_url();?>index.php?/Product/productlist/"+product_id; 

//alert(myurl); 

    $.post(myurl).done(function (data) 
    { 
    //alert(data); 

     var obj=jQuery.parseJSON(data); 
    //alert(obj); 

     //alert(myhtml); 
     var myhtml='<form role="form" id="formedit" action="#">'+ 
       '<div class="box-body">'+ 
       ' <div class="form-group">'+ 
       ' <label for="exampleInputName">Product Name</label>'+ 
       ' <input type="hidden" id="upmyid" name="editid" value="'+obj[0]+'">'+ 
       ' <input type="text" class="form-control" id="upname" placeholder="Product Name" name="editpname" value="'+obj[1]+'">'+ 
       ' </div>'+ 
       '</div>'+ 
     '<div class="box-body">'+ 
     '<div class="form-group">'+ 
     '<label for="exampleInputImage">Image</label>'+ 
     '<input type="file" class="form-control" id="upimage" placeholder="Image" name="editpimage" value="'+obj[2]+'">'+ 
     '</div>'+ 
     '</div>'+ 
     '<div class="box-body">'+ 
     '<div class="form-group">'+ 
     '<label for="exampleInputCategory">Category</label>'+ 
     '<select class="form-control select category" style="width: 100%;" name="option2" id="option2">'+ 
     '<option value="0">Main Menu </option>'+ 
     '<option value="'+obj[9]+'" selected>'+obj[3]+'</option>'+ 
     '</div>'+ 
     '</div>'+ 
       '</form>'; 

       swal({ title: "Edit <small>this</small>!", 
         text: myhtml, 
         html: true, 
         type: "", showCancelButton: true, confirmButtonColor: "#3c8dbc", 
         confirmButtonText: "Update Now", 
         closeOnConfirm: false }, 
         function() 
         { 
          var id=$('#upmyid').val(); 
          var name=encodeURIComponent($('#upname').val()); 
          var myurl="index.php/Product/updateProduct/"+id+"/"+name; 

          $.post(myurl).done(function(data) 
          { 
           //alert(data); 

           var obj = jQuery.parseJSON(data); 

           //alert(obj); 

           if(obj[0]==100) 
           { 

            swal({  title: "Updated Successfully", 
            text: "Click on ok now!", 
            type: "success", showCancelButton: false, confirmButtonColor: "#3c8dbc", 
            confirmButtonText: "OK", 
            closeOnConfirm: false }, 

            function() 
            { 
             location.reload(); 
            }); 
           } 

           else 
           { 
            swal("Sorry!", 
            "Unable to Update now.", 
            "warning"); 
           } 

          }); 

         } 
        ); 
    }); 
    } 
    </script> 
+0

每當您在PHP中運行您的foreach行時,它會使用最新值覆蓋數組的相同部分。每次循環時都需要創建一個新數組(或更好的是一個對象),然後將_that_數組/對象添加到$ mainarray的下一個空索引。實際上,'$ mainarray [] = $ temp;'可能就足夠了,它看起來像$ temp已經是一個對象。 – ADyson

+0

然後在javascript中,您需要遍歷'obj'併爲每個返回的對象打印一個新的'

+0

對不起,但我沒有得到你可以請你編輯腳本 – Akhil

回答

0

你運行你的foreach每次在PHP中使用最新值覆蓋數組的相同部分。例如如果你的DB有10行返回,那麼循環運行10次。因此$mainarray[0]被替換10次,最新值爲$temp->id;,其他所有字段的值相同。這意味着您只能將最後一個DB行的值存入您的PHP數組中。

嘗試像這樣(未經測試,道歉對任何小錯誤):

型號:

foreach($rows as $temp) 
    { 
     $returnarray[] = $temp; 
    } 

這將整個$臨時對象放入的$mainarray下一個可用指標。其次,您的JavaScript代碼沒有設置爲處理響應中的多個項目,因此即使要返回多個項目,也只會打印一個數據項目。

查看

1)刪除這一行:

var obj=jQuery.parseJSON(data); 

應該沒有必要爲此 - 在你的PHP $json_encode()將已經返回一個JSON對象,而不是字符串,所以你不需要解析它。 jQuery將會理解data已成爲JSON對象。

2)像這樣更新您的HTML生成代碼(所做的更改是呈現<select>的地方)。它需要循環訪問data陣列,併爲陣列中的每個對象創建一個<option>

 var myhtml='<form role="form" id="formedit" action="#">'+ 
      '<div class="box-body">'+ 
      ' <div class="form-group">'+ 
      ' <label for="exampleInputName">Product Name</label>'+ 
      ' <input type="hidden" id="upmyid" name="editid" value="'+obj[0]+'">'+ 
      ' <input type="text" class="form-control" id="upname" placeholder="Product Name" name="editpname" value="'+obj[1]+'">'+ 
      ' </div>'+ 
      '</div>'+ 
    '<div class="box-body">'+ 
    '<div class="form-group">'+ 
    '<label for="exampleInputImage">Image</label>'+ 
    '<input type="file" class="form-control" id="upimage" placeholder="Image" name="editpimage" value="'+obj[2]+'">'+ 
    '</div>'+ 
    '</div>'+ 
    '<div class="box-body">'+ 
    '<div class="form-group">'+ 
    '<label for="exampleInputCategory">Category</label>'+ 
    '<select class="form-control select category" style="width: 100%;" name="option2" id="option2">' + 
    '<option value="0">Main Menu </option>'; 
$.each(data, function(index, obj) { 
    myhtml += '<option value="' + obj.acategory_id + '">' + obj.cname + '</option>'; 
}); 
myhtml += '</select>' + 
    '</div>'+ 
    '</div>'+ 
'</form>'; 
相關問題