Forum Moderators: coopster

Message Too Old, No Replies

From english to PHP

Can anyone give me a translation

         

Webber

2:49 pm on Jan 18, 2008 (gmt 0)

10+ Year Member



I'm trying to get my form to calculate a price, but haven't got a clue how to write the code. Can anyone translate a bit into PHP? Or at least gives me the syntax that I need to use.

It should work like this:
When someone orders a 'blue' 'large' widget, use the price from the price table in the column for blue and row for large.
If they said 'yes' to "this-and-this-question": add up 10% to this price. Else display the original price.
If they said 'yes' to "this-and-this-question" And "that-and-that-question": add up 20% to the price and display the total price. Else display the previous price.

I just do not understand how to get started, which syntax to use to get the original price from the table. And then I get lost.

jatar_k

3:08 pm on Jan 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> When someone orders a 'blue' 'large' widget, use the price from the price table in the column for blue and row for large.

The value of large is present in a column called? Let's assume it is called 'size'. I am also assuming the table is actually called 'price'.

Select blue from price where size='large';

your query would be something like that. I am not sure if you need all the explanation of the Basics of extracting data from MySQL using PHP [webmasterworld.com]

>> If they said 'yes' to "this-and-this-question": add up 10% to this price. Else display the original price.

once you put the price into a variable, we'll call it $price, you need to compare it. With out a bunch of more specific info I can't give you the exact code. Let's assume your form element is called 'question1', that it's possible values are yes and no and also that it was posted to this script

if ($_POST['question1'] == 'yes') { 
$price = $price + ($price * 0.1);
}

>> If they said 'yes' to "this-and-this-question" And "that-and-that-question": add up 20% to the price and display the total price. Else display the previous price.

we can now add to our above code because there are 2 requirements. Let's call the second form element question2

if ($_POST['question1'] == 'yes') { 
if ($_POST['question2'] == 'yes') {
$price = $price + ($price * 0.2);
} else {
$price = $price + ($price * 0.1);
}
}

this assumes that if the answer to question2 is not 'yes' then you only add 10%

Webber

4:09 pm on Jan 19, 2008 (gmt 0)

10+ Year Member



Thank you sooooo much for the explanation.
Yeah, I can figure out what to do with my table name and columns and rows. It's just the 'select' part that I didn't manage. And I wasn't sure about calculations. I thought that I needed to add the percentages in some table to do the calculation.
I read my way through loads of tutorials and didn't know how to put all the information together.

Thank you very much. Your answer will save me a lot of time.