我需要一些帮助来分隔用逗号分隔的id,然后在下拉列表中显示它们。
这是我正在使用的代码,如果clothing_type_id中只有 1 个数字,它可以正常工作,如果我添加更多数字使其被逗号分割,即 1,2,3,4 它将不再工作,我确定我需要使用 explode 但我无法让它工作。任何帮助将不胜感激。
<? $clothing=intval($_GET['clothing_var']);
include 'classes.php';
mysql_select_db('database');
$query="SELECT ID,Qty_Values FROM Quantity WHERE Clothing_Type_ID='$clothing'";
$result=mysql_query($query);
$test=explode(',', '$result');
?>
<select name="state" onchange="getCity(<?=$clothing?>,this.value)">
<option>Select State</option>
<? while($row=mysql_fetch_array($test)) { ?>
<option value=<?=$row['ID']?>><?=$row['Qty_Values']?></option>
<? } ?>
</select>
更新:
这就是我要做的。
嗨,粘贴了您的代码,虽然它可以工作,但它只显示第一个选择,如果我单击第二个 id,则不会显示任何内容。我的数据库表是这样的,名为 Clothing_Type 的第一块选择有 3 列,ID 服装和价格,第二个表有 4 列,ID、Clothing_Type_ID、Qty_Values和价格。基本上,我要做的是制作定价指南,因此,如果我在第一个选择框中选择一件T恤,它将在表格末尾显示4个价格,每个项目1个,总金额为2个(如果选择更高的数量)3个增值税和4个总金额。每次选择时,它都会自动更新价格。在我选择了 3 或 4 个选择框后,我想要再次使用几个复选框来更新价格(如果它们被勾选)。
更新2:这是js和html
.JS
<script language="javascript" type="text/javascript">
function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getQty(countryId) {
var strURL="findQty.php?clothing_var="+countryId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('qtydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getCity(countryId,stateId) {
var strURL="findCity.php?clothing_var="+countryId+"&qty_var="+stateId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
.HTML
<body>
<form method="post" action="" name="form1">
<?
include 'classes.php';
$query="SELECT ID,Garment FROM Clothing_Type";
$result=mysql_query($query);
?>
<select name="clothing_type" onChange="getQty(this.value)">
<option>Select Clothing Type</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['ID']?>><?=$row['Garment']?></option>
<? } ?>
</select>
<p id="qtydiv">
<select style="width: 150px;" name="qty" disabled='disabled'>
<option>Select Country First</option>
</select>
</p>
<p id="citydiv">
<select style="width: 150px;" name="city" disabled='disabled'>
<option>Select State First</option>
</select>
</p>
<input type="checkbox" name="vehicle" value="Bike" /> I want to live here<br />
<form>
<input type='checkbox' name='1' value='1'id='checkbox_1' />1<br>
<input type='checkbox' name='2' value='2'id='checkbox_2' />2<br>
<input type='checkbox' name='3' value='3'id='checkbox_3' />3<br>
</form>
</body>
试试这个,
$clothing = "1,2,3,4";
$query="从 ($clothing) 中Clothing_Type_ID的数量中选择 ID,Qty_Values";
我认为您应该使用 IN()
运算符,而不仅仅是与逗号分隔的字符串进行比较:
$query = "SELECT ID, Qty_Values FROM Quantity WHERE Clothing_Type_ID IN($clothing)";
更新你的 SQL 到这个。
$query="SELECT ID,Qty_Values FROM Quantity WHERE Clothing_Type_ID in ($clothing)";
摆脱爆炸。这将返回服装类型为 ID 的服装的结果集,该服装类型在$clothing列表中列出。
我的一部分认为你在倒退。我假设数据库中的每个项目只有 1 个服装类型 ID,并且 ID 列表在您的$clothing字符串中。
如果不是这种情况,请给我一个示例,看看您的数据库表。