Jump to content

Automatic change price to match amount


rec0il

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.

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.