2014-09-03 35 views
-2

是否可以使用此代碼計算總計。按下按鈕後,我想讓compcase和caselight提供總計。任何幫助將不勝感激,因爲我不能找出一種可能的方式來計算這一切。是否可以使用此代碼將它們總計添加爲總計?

的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); 
?> 

回答

0

你想總計的情況? 如果是這樣,讓你的選擇查詢這樣:

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

然後,當你通過foreach循環使用$行[「grand_total」]。

+0

我希望case_price和caselight_price合計爲總計。這兩個單選按鈕: NZXT幻影愛好者USB3.0全塔式機箱 - 白色
紅色
2014-09-03 13:40:30

+0

當我這樣做時,該值顯示爲0 – 2014-09-03 15:37:40