有没有可能用这个代码把这些加起来作为一个总数


is it possible to add these up as a grand total using this code?

是否可以使用此代码计算总计。按下按钮后,我想让compcase和case light给出一个总数。任何帮助都将不胜感激,因为我无法找到一种可能的方法来计算这一切。

index.php

  function casePrice(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
} 
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","caseprice.php?q="+str,true);
xmlhttp.send();
}
  function caselightPrice(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
} 
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","caselightprice.php?q="+str,true);
xmlhttp.send();
}
</script>
<Form name ="pc" Method ="Post" ACTION ="radiobutton.php">
<Input type = 'Radio' Name ='compcase' onchange="casePrice(this.value)" value= '1' />NZXT Phantom Enthusiast USB3.0 Full Tower Case - White <br />
<Input type = 'Radio' Name ='compcase' onchange="showPrice(this.value)" value= '2' />Corsair Obsidian 750D Large Tower Case Black <br />
<Input type = 'Radio' Name ='compcase' onchange="showPrice(this.value)" value= '3' />Cooler Master CM Storm Trooper Black Full Tower Gaming Case <br /><br />
<Input type = 'Radio' Name ='caselight' onchange="caselightPrice(this.value)" value= '1' />Red<br />
<Input type = 'Radio' Name ='caselight' onchange="caselightPrice(this.value)" value= '2' />Green <br /><br />
<div id="txtHint"><b>Show price here</b></div>

caseprice.php

<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','test');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"compcase");
$sql="SELECT * FROM compcase WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['case_price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

您想要案例的总数吗?如果是这样,请将您的选择查询设置为:

SELECT SUM('case_price') AS grand_total FROM compcase WHERE id = '".$q."'";

然后,当您完成foreach循环时,使用$row['grand_total']。

相关文章: