我的数据库列如下所示:
`matDes1` varchar(255) DEFAULT NULL,
`matCost1` int(255) DEFAULT NULL,
`matDes2` varchar(255) DEFAULT NULL,
`matCost2` int(255) DEFAULT NULL,
`matDes3` varchar(255) DEFAULT NULL,
`matCost3` int(255) DEFAULT NULL,
`matDes4` varchar(255) DEFAULT NULL,
`matCost4` int(255) DEFAULT NULL,
`matDes5` varchar(255) DEFAULT NULL,
`matCost5` int(255) DEFAULT NULL,
我有一个动态表,其中包含我的HTML文件中的添加和删除行。
在此表中,我有文本区域和输入内容,每当我开始键入内容时,就会向我显示建议,如果选择一个,则会自动填充其他字段。
在我的文本区域下方输入以下内容:
<textarea id='codeANCILLARY_1' class='codeANCILLARY' name="codeANCILLARY[]"></textarea>
<input type="text" id="mat50ANCILLARY_1" class="mat50ANCILLARY" name="mat50ANCILLARY[]" />
textarea搜索从matDes1到matDes5。所以这里没问题。
问题是选择来填充两个场时,只接受matDes1和matCost1不其余部分。
是否可以使用诸如if语句之类的说法:
if (matDes1 selected) {
show matDes1 with matCost1
} else if (matDes2 selected){
show matDes2 with matCost2
}
and
so
on
我真的不知道该怎么办。 :(
完整的jQuery和PHP文件是:
$(document).on('keydown', '.codeANCILLARY', function () {
var id = this.id;
var splitid = id.split('_');
var count = splitid[1];
$('#' + id).autocomplete({
source: function (request, response) {
$.ajax({
url: "../../MY_PHP_PAGE",
type: 'post',
dataType: "json",
data: {
search: request.term,
request: 1
},
success: function (data) {
response(data);
}
});
},
select: function (event, ui) {
$(this).val(ui.item.label);
var id = ui.item.value;
// AJAX
$.ajax({
url: '../../MY_PHP_PAGE',
type: 'post',
data: {
id: id,
request: 2
},
dataType: 'json',
success: function (takesAnyVaribale) {
var len = takesAnyVaribale.length;
if (len > 0) {
var codeANCILLARY = takesAnyVaribale[0]['codeANCILLARY'];
var mat50ANCILLARY = takesAnyVaribale[0]['mat50ANCILLARY'];
var unitsANCILLARY = takesAnyVaribale[0]['unitsANCILLARY'];
$('#codeANCILLARY_' + count).val(codeANCILLARY);
$('#mat50ANCILLARY_' + count).val(mat50ANCILLARY);
$('#unitsANCILLARY_' + count).val(unitsANCILLARY);
}
}
});
return false;
}
});
});
"MY_PHP_PAGE"
include "config.php";
$request = $_POST['request'];
if ($request == 1) {
$search = $_POST['search'];
$query1 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes1 like'%".$search."%'";
$query2 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes2 like'%".$search."%'";
$query3 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes3 like'%".$search."%'";
$query4 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes4 like'%".$search."%'";
$query5 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes5 like'%".$search."%'";
$result1 = mysqli_query($con, $query1);
$result2 = mysqli_query($con, $query2);
$result3 = mysqli_query($con, $query3);
$result4 = mysqli_query($con, $query4);
$result5 = mysqli_query($con, $query5);
if ($result1 || $result2 || $result3 || $result4 || $result5) {
while ($row = mysqli_fetch_array($result1)) {
$response[] = array("value"=>$row['id'],"label"=>$row['matDes1']);
}
while ($row = mysqli_fetch_array($result2)) {
$response[] = array("value"=>$row['id'],"label"=>$row['matDes2']);
}
while ($row = mysqli_fetch_array($result3)) {
$response[] = array("value"=>$row['id'],"label"=>$row['matDes3']);
}
while ($row = mysqli_fetch_array($result4)) {
$response[] = array("value"=>$row['id'],"label"=>$row['matDes4']);
}
while ($row = mysqli_fetch_array($result5)) {
$response[] = array("value"=>$row['id'],"label"=>$row['matDes5']);
}
}
echo json_encode($response);
exit;
}
if ($request == 2) {
$id = $_POST['id'];
$sql = "SELECT * FROM MY_COMPONENTLIST WHERE id=".$id;
$result = mysqli_query($con, $sql);
$AncillaryPricing_arr = array();
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$codeANCILLARY = $row['matDes1'];
$mat50ANCILLARY = $row['matCost1'];
//***************************
//***************************
$codeANCILLARY = $row['matDes2'];
$mat50ANCILLARY = $row['matCost2'];
.and
.so on
.until 5
//***************************
//***************************
$unitsANCILLARY = $row['units'];
$AncillaryPricing_arr[] = array(
"id" => $id,
"codeANCILLARY" => $codeANCILLARY,
"mat50ANCILLARY" => $mat50ANCILLARY,
"unitsANCILLARY" => $unitsANCILLARY
);
}
echo json_encode($AncillaryPricing_arr);
exit;
}
答案 0 :(得分:0)
正是出于这个原因,您确实确实需要更好的数据库布局。将matdes
和matcost
列拆分到一个单独的表中,该表由组件ID链接。然后,您可以拥有任意数量的内容,并且搜索起来要容易得多。而不是所有单独的查询,您将拥有类似的
SELECT * FROM MY_COMPONENTLIST_MAT WHERE matDes like'%".$search."%' and component_id = " . $id
(作为准备好的语句除外),它将找到其中的任何一个。您可以使用JOIN从主要组件列表中获取其余信息。