![](/img/trans.png)
[英]Select onChange Ajax get list of data from mysql database Laravel 5.4
[英]Get the data from database and put it in an select list
我正在使用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>
每次在PHP中运行foreach行时,它都会用最新值覆盖数组的相同部分。 例如,如果从数据库返回了10行,则循环运行10次。 因此,将$mainarray[0]
替换$mainarray[0]
$temp->id;
的最新值10次$temp->id;
,所有其他字段都相同。 这意味着您只会将最后一个数据库行中的值获取到PHP数组中。
像这样尝试(未经测试,对于任何小错误,我们深表歉意):
模型:
foreach($rows as $temp)
{
$returnarray[] = $temp;
}
这会将整个$ temp对象放入$mainarray
的下一个可用索引中。
其次,您的JavaScript代码未设置为处理响应中的多个项目,因此即使要返回许多项目,也只会打印一个数据项目。
视图
1) 删除此行:
var obj=jQuery.parseJSON(data);
不需$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>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.