简体   繁体   中英

Calculate the amount from the database given

Sorry if the title confuses you, explaining further this is what I've wanted to say:

I would like to calculate the total amount based on the choices of the user. At first I did code the amount in JS, but this time the price could be updated by the admin. So the price amount is stored in database.

This is my first code :

 function Amt() { var price = 0; function dress() { if(document.getElementById('d1').checked){ price += 40; } else if(document.getElementById('d2').checked){ price += 50; } } function pants() { if(document.getElementById('p1').checked) { price += 60; } else if(document.getElementById('p2').checked) { price += 80; } } dress(); pants(); var totalPrice = price; document.getElementById('totalPrice').innerHTML = "Amount: $ " + totalPrice; }
 <br><input type="radio" id="d1" name="d" value="blouse" onchange="Amt()"/> Blouse </br> <br><input type="radio" id="d2" name="d" value="tshirt" onchange="Amt()"/> T-shirt </br> <br><input type="radio" id="p1" name="p" value="thights" onchange="Amt()"/> Thights </br> <br><input type="radio" id="p2" name="p" value="jeans" onchange="Amt()"/> Jeans </br> <div id="totalPrice"> Amount: $ </div>

So right now I've update my code and removed the " var price = 0; " in my JS. I already made a table in my database for the price list each of my choices, but the only thing I could do is Echo them. I have no idea how to start coding to calculate them...

What I've got so far:

<?php 
session_start();

$link = mysql_connect('localhost', 'root', ''); //this is to connect in the database
mysql_select_db("shopgo", $link);
$query="SELECT * FROM pricelist";
$myData = @mysql_query($query, $link);
while($record=mysql_fetch_array($myData)) { 
//loop to get data in my Pricelist Table   ?>

    <br><input type="radio" id="d1" name="d" value="blouse" />  Blouse   <?php echo "$".$record[blouse_price'].""; ?> </br>
    <br><input type="radio" id="d2" name="d" value="tshirt" />  T-shirt  <?php echo "$".$record['tshirt_price'].""; ?> </br>

    <br><input type="radio" id="p1" name="p" value="thights" />  Thights  <?php echo "$".$record['thights_price'].""; ?> </br>
    <br><input type="radio" id="p2" name="p" value="jeans" />  Jeans  <?php echo "$".$record['jeans_price'].""; ?> </br>
<?php  
} 
?> //close the loop
<div id="totalPrice"> Amount: $  </div>

And simply it displays

Blouse - $40

T-shirt - $50

Thights - $60

Jeans - $80

In my admin part the prices are available to update so my calculation part is that I'm not sure how to make it. Thank you so much for the help in advance...

You can try something using hidden input type

while($record=mysql_fetch_array($myData)) {

  <br><input type="radio" id="d1" name="d" value="blouse" onchange="Amt()"/>  Blouse </br>
  <br><input type="radio" id="d2" name="d" value="tshirt" onchange="Amt()"/>  T-shirt </br>

  <br><input type="hidden" id="blouseDbPrice"  value="<?php echo $record[blouse_price']" ?>/> 
  <br><input type="hidden" id="tShirtPrice "  value="<?php echo $record[tshirt']" ?> />   
}  

Now fetch the hidden text value in your js

function dress(){

   var blousePrice = document.getElementById('blouseDbPrice').value;
   var tShirtPrice = document.getElementById('tShirtPrice').value;

   if(document.getElementById('d1').checked)
   { 
      price += blousePrice;
   }
   else if(document.getElementById('d2').checked)
   { 
      price += tShirtPrice ; 
   }  
}

I would do it using custom attributes. You can echo your php var into html tag as data-whatever=""

<br><input type="radio" id="d1" name="d" value="blouse" data-price="<?=$record[blouse_price']?>" />  Blouse   <?php echo "$".$record[blouse_price'].""; ?> </br>

and in your js part you can get this attribute and do whatever you need with that. If you use jq it looks like:

var price = $('#d1').attr('data-price')

Cheers!

Try to use this


Blouse
T-shirt


Thights
Jeans //close the loop

Amount: $TotalPrice

If you get more than one row from database then you need to show Price it inside while loop.

I would use a custom attribute on each of the radio buttons setting the price. On selection of a radio button I would use javascript to calculate the total price.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM