douju6542 2014-09-04 12:01
浏览 48
已采纳

可以合并这些,这个代码也可以显示小计吗? [重复]

This question is an exact duplicate of:

So currently I have 2 files that pretty much does the same thing but connects to 2 different tables. I sort of want to merge it together to save file space and make everything more efficient:

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

also how can I use all this to display a sub total on my page when the radio buttons are pressed? I have it so the price shows I just cant seem to add them both up:

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>

as you can see, the price is displaying separately. Can I somehow calculate it as a subtotal instead of separate totals?

Thanks for any help.

</div>
  • 写回答

2条回答 默认 最新

  • dongpengyu1363 2014-09-04 13:47
    关注

    It looks to me like the problem is this: When you send a price request to the server, you're telling it EITHER which case the user selected OR which light. So the server cannot directly give the total price because it only knows one thing or the other. You then write the price in the same field either way. So when the client gets back a price from the server, it overrides the other price, and it has no way to calculate the total.

    Possible solutions that come to mind:

    1. Instead of using Ajax calls, do a Submit that sends both the case and the light to the server. The server than replies with 3 numbers: the case price, the light price, and the total.

    2. When you send the Ajax request to the server, send both selections. Then the server replies with three numbers: both prices and the total. Send both regardless of which the user just updated. Just let the onchange for both call the same JavaScript function, that picks up both selections and creates the parameter string.

    3. Write the price in two different fields. Actually I'd do this anyway. It seems to me it would be confusing to the user to click a case and have the "price" field show the value of the case, then select a light and have that same price field change to show the price of the light. If I'm distracted for a moment and don't remember which I clicked last, I have no idea what the number means. But anyway, write them to two different fields. Then have the client update whichever one just changed, then pick up both fields and add the two together.

    4. Create two session variables on the server: one for case price and one for light price. When a price is sent, fill in the session variable. Then use that in combination with whatever is in the other session variable to compute a total price. Send back two or three numbers: either the price of the thing just requested and the total, or both prices and the total. I'd lean against this option because session variables are difficult to manage, but it's possible.

    Just by the way, it looks to me like you are creating a table end tag but no table start tag. And why are you creating a table anyway? Won't the server just send back one number for each request? Each item should only have one price, right? Or are you making a table because you're thinking that you want one row to show the price and the second row to show the total?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条