Jump to content

Working out a grand total from multiple mysql tables


Elit3d

Recommended Posts

 

Basically I want to add up multiple tables and display a grand total on my page when the radio button is pressed. The radio button has values that connect to my database and the values link to and ID with a price. How can I use this code in order to work out a grand total? Many thanks. I have searched everywhere but found nothing.
 
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>

 

 
Thanks for any help.

 

 

Link to comment
Share on other sites

first of all, there no code in your code to produce a total or even an element to display the total. where's your attempt at doing this?

 

your code should be general purpose, with the fields you want to total up having a specific class name. you can then simply call a javascript function when any of the data changes, to total up the values of the fields having that class name and display the total in another field. using a javascript library like jquery would make this a simple task and you can probably search the web and find numerous examples.

Link to comment
Share on other sites

first of all, there no code in your code to produce a total or even an element to display the total. where's your attempt at doing this?

 

your code should be general purpose, with the fields you want to total up having a specific class name. you can then simply call a javascript function when any of the data changes, to total up the values of the fields having that class name and display the total in another field. using a javascript library like jquery would make this a simple task and you can probably search the web and find numerous examples.

 

I've been working on something but I am stuck. I don't know how I can connect this javascript to my mysql php file. 

<html>

<script type="text/javascript">

var total = 0;

function test(item){
if(item.checked){
total+= parseInt(item.value);
}else{
total-= parseInt(item.value);
}
//alert(total);
document.getElementById('Totalcost').innerHTML = total;
}



</script>


<input type="radio" name="cpu" value="1" onClick="test(this);" />TEST1<br />
<input type="radio" name="cpu" value="2" onClick="test(this);" />TEST2 <br />
<input type="radio" name="cpu" value="3" onClick="test(this);" />TEST3 <br />
<input type="radio" name="cpu" value="4" onClick="test(this);" />TEST4 <br />
<br>
<input type="radio" name="case" value="1" onClick="test(this);" />TEST1<br />
<input type="radio" name="case" value="2" onClick="test(this);" />TEST2 <br />


<span id="Totalcost"></span>
</html>
Edited by Elit3d
Link to comment
Share on other sites

the purpose of your javascript code is to add up and display the total price of the selected items. in order to do this, you must first have the price of the selected item(s).

 

have you considered what information the user is presented with? he would want to see the prices displayed next to each possible choice as that would influence his selection. unless the use of ajax to retrieve the price was part of your assignment, there's no need to get the price on demand, you should already have the price for everything being listed when you (dynamically, using php code) build the radio-button choices by retrieving the information from your database table.

Link to comment
Share on other sites

the purpose of your javascript code is to add up and display the total price of the selected items. in order to do this, you must first have the price of the selected item(s).

 

have you considered what information the user is presented with? he would want to see the prices displayed next to each possible choice as that would influence his selection. unless the use of ajax to retrieve the price was part of your assignment, there's no need to get the price on demand, you should already have the price for everything being listed when you (dynamically, using php code) build the radio-button choices by retrieving the information from your database table.

 

No I want it to show a sub total of each item added to the cart and not the price of individual 

 

here is a screenshot of what I am going for:

 

http://gyazo.com/077f3a023ffecf1770d8da904b25fc8e

Edited by Elit3d
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.