rec0il Posted July 20, 2014 Share Posted July 20, 2014 Hi again PHPFreaks I'm building on my new local project in which I wish to create a working function for a future e-commerce. But I ran into a problem. Please open following picture in order for me to illustrate my problem. http://i1227.photobucket.com/albums/ee433/rec0ill/example_zpsacd52b0c.jpg The picture represents what I want. My only problem is that I have no idea how to make the "Total price:" value (which is 1.2€ on the picture) to match the quantities above. Lets say a costumer chose 10 quantities - in that case i would like it to automaticly change the total price value to 12€ instead, so that it matches the amount of quantities chosen. How would i be able to do this? Thx in advance - r e c 0 i l Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 20, 2014 Share Posted July 20, 2014 Can't give you a exact code since you didn't provide us with any of your code to compare with, but here is a basic idea. PHP is very good at math, so just use normal mathematic operators and maybe a function depending how you want the output to look. $price_per_item = 1.2; $quantity = 10; $total = $price_per_item * $quantity; echo $total; $formatted_total = number_format($total, 2); // formats the number to have 2 decimal places. echo $formatted_total; Beyond that you'll have to show us your code that deals with those specific areas. Quote Link to comment Share on other sites More sharing options...
rec0il Posted July 20, 2014 Author Share Posted July 20, 2014 Can't give you a exact code since you didn't provide us with any of your code to compare with, but here is a basic idea. PHP is very good at math, so just use normal mathematic operators and maybe a function depending how you want the output to look. $price_per_item = 1.2; $quantity = 10; $total = $price_per_item * $quantity; echo $total; $formatted_total = number_format($total, 2); // formats the number to have 2 decimal places. echo $formatted_total; Beyond that you'll have to show us your code that deals with those specific areas. Thanks for the great and fast answer fastsol, really appreciated. Though I'm not sure if I can use this to function as I want it to. I would like it to automatically change the total price every time the quantity value is changed in the input, so that it doesnt have to depend on a given value. Heres the HTML code of my input. <input type="number" name="quantity" value="1"> So I would like the php code to take the value from the input and put it in where it says X $quantity = X; How would this be possible? Quote Link to comment Share on other sites More sharing options...
Rifts Posted July 21, 2014 Share Posted July 21, 2014 use jquery to change the display Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 21, 2014 Share Posted July 21, 2014 I'm doing more "assuming" cause you still haven't provided any clue as to what your code looks like. But it kind of looks like you're wanting to change the value of $quantity based on a input field being posted. $quantity = (int)$_POST['quantity']; // (int) is used for security to type cast the value to an integer. Could also be $_GET['quantity'] but more likely $_POST. But that also assumes that your inputs name is quantity. Quote Link to comment Share on other sites More sharing options...
rec0il Posted July 21, 2014 Author Share Posted July 21, 2014 use jquery to change the display I'm not very familiar with jQuery, could you direct me in the right direction maybe? Thank you I'm doing more "assuming" cause you still haven't provided any clue as to what your code looks like. But it kind of looks like you're wanting to change the value of $quantity based on a input field being posted. $quantity = (int)$_POST['quantity']; // (int) is used for security to type cast the value to an integer. Could also be $_GET['quantity'] but more likely $_POST. But that also assumes that your inputs name is quantity. I'm not sure what part of the code you would need, but heres the full of the part I'm working on. <div class="part2"> <img src="images/part2.png"><br> <input type="number" style="text-align:center;width:100px;background-color:transparent;color:#fff;border:0px;" name="quantity" value="1"><br> <table style="width:300px;margin-left: auto;margin-right:auto;margin-top:10px;"> <tr> <td> Total price: </td> <td> 1.2€ </td> <td style="font-size:12px;color:#e07b14"> 1.2€ Each </td> </tr> </table> <table style="width:380px;margin-left: auto;margin-right:auto;margin-top:10px;"> <tr> <td style="width:52%;"> Redeeem discount code: </td> <td style=""> <input style="text-align:center;" type="text"> </td> </tr> </table> </div> I've tried adding $quantity = (int)$_POST['quantity']; instead the previous code "$quantity = 10;" but it didn't seem to be working and my input name is set to quantity. I must have misunderstood something Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 21, 2014 Share Posted July 21, 2014 I'm not very familiar with jQuery, could you direct me in the right direction maybe? Thank you Perhaps you could find something here: https://www.google.com/?gws_rd=ssl#q=javascript%20onchange%20calculate%20total The following example seems close to what you're trying to do: http://www.mcfedries.com/javascript/ordertotals.asp Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 21, 2014 Share Posted July 21, 2014 None of this is being output by php, so of course it's not going to change. <td> Total price: </td> <td> 1.2€ </td> <td style="font-size:12px;color:#e07b14"> 1.2€ Each </td> Are you processing the quantity above that code with php when the form is submitted? You should be. So then you could do something like this <td> Total price: </td> <td> <?php echo number_format(($quantity * $price), 2); ?>€ </td> <td style="font-size:12px;color:#e07b14"> 1.2€ Each </td> But honestly there is more to it than that cause $quantity will only have a value AFTER you submit the form. You need to have a base value $price for the product coming from somewhere, then you *(times) that value by the quantity posted to get the new price. Without knowing how or where you are gathering this info for the product and the quantity update, it's really impossible to say how to help you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.