所以目前我有兩個文件幾乎做同樣的事情,但連接到2個不同的表。我有點想合併到一起,以節省文件空間,讓一切更高效:可能合併這些,也可以這個代碼顯示小計?
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);
?>
caselightprice.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,"complight");
$sql="SELECT * FROM complight WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['caselight_price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
也哪能當單選按鈕被按下時,使用所有這些在我的頁面上顯示一個小計?我有這樣的價格顯示我只是不能似乎他們兩個加起來:
的index.php
<script>
//CASE PRICE//
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();
}
//CASE PRICE//
//CASE LIGHT PRICE//
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();
}
//CASE LIGHT PRICE//
</script>
<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="casePrice(this.value)" value= '2' />Corsair Obsidian 750D Large Tower Case Black <br />
<Input type = 'Radio' Name ='compcase' onchange="casePrice(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>Processor listed here</b></div>
<div id="txtTest"><b>Processor listed here</b></div>
,你可以看到,價格分別顯示。我能以某種方式計算它作爲小計而不是單獨的總數嗎?
感謝您的任何幫助。
顯示你的努力 – 2014-09-04 12:14:12
嗯,我不知道從哪裏開始,這就是爲什麼我要求幫助 – 2014-09-04 12:25:41
似乎可能是單個MySQL查詢以某種方式「聯合」你的表在一起將有所幫助。但是,我們不知道你的表格是什麼樣的,所以我們不能就如何編寫該查詢給出任何有用的建議。 – 2014-09-04 12:31:04