Forum Moderators: coopster

Message Too Old, No Replies

function problem. please help

         

Flolondon

4:18 pm on May 9, 2006 (gmt 0)

10+ Year Member



function add_tax ($amount) {
$total = $amount * 1.09;
return $total;

}
$price = vehicle($car)
echo "price after tax "{
echo add_tax ($price);
}

please what have i done wrong. it does not seem to be echoing the overal tax price to the table.please help.

jatar_k

4:24 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



those bottom echo lines have some syntax problems, I would do it this way

function add_tax ($amount) {
$total = $amount * 1.09;
return $total;
}

$price = vehicle($car)
$taxprice = add_tax($price);
echo "price after tax " . $taxprice;

Flolondon

4:58 pm on May 9, 2006 (gmt 0)

10+ Year Member



vehicle($car)

Thank you for that jatar but i have tried it and the comment i get get is unexpected T variable.

Vehicle ($car) is from another function. When a user clicks from a drop down menu, it shows a message and a price. So i wanted to use the information from another function to another function in stead of putting the figures in for about 6 items

instead of putting $price = 16.00

i wanted to put $price = vehicle($car) which stems from another function

jatar_k

5:04 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ah, I didn't notice your original code was also missing a semi colon on that line

function add_tax ($amount) {
$total = $amount * 1.09;
return $total;
}

$price = vehicle($car);
$taxprice = add_tax($price);
echo "price after tax " . $taxprice;

Flolondon

5:30 pm on May 9, 2006 (gmt 0)

10+ Year Member



i have tried it and apparently it only shows O as a price total in the html.

Flolondon

5:35 pm on May 9, 2006 (gmt 0)

10+ Year Member



function vehicle($cars) {

switch ($cars) {
case "white":
$pricestr = "5.00.\n" ;
break;

case "brown":
$pricestr = "6.00\n" ;
break;

case "green":
$pricestr = "7.00\n" ;
break;

case "blue":
$pricestr = " 8.00\n" ;
break;

case "orange":
$pricestr = " 7.50\n" ;
break;
}
return $pricestr;
}

?>

<?php
function add_tax ($amount) {
$total = $amount * 5.09;
return $total;
}

$price = vehicle($cars);
$taxprice = add_tax($price);
?>

Flolondon

5:37 pm on May 9, 2006 (gmt 0)

10+ Year Member



its like i am using a function bit within another function bit

jatar_k

5:43 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you're trying to do math with strings, that's the problem

$pricestr = "5.00.\n" ;

that means you have a string in there and then you do

$total = $amount * 5.09;

in the next function, that just won't work. You need to have ints in the first function. something like this

function vehicle($cars) {
switch ($cars) {
case "white":
$pricestr = "5.00" ;
break;

case "brown":
$pricestr = "6.00" ;
break;

case "green":
$pricestr = "7.00" ;
break;

case "blue":
$pricestr = " 8.00" ;
break;

case "orange":
$pricestr = " 7.50" ;
break;
}
return $pricestr;
}

function add_tax ($amount) {
$total = $amount * 5.09;
return $total;
}

$cars = 'brown';
$price = vehicle($cars);
$taxprice = add_tax($price);
echo '<p>price before tax ' . $price;
echo "<p>price after tax " . $taxprice;
?>

Flolondon

6:05 pm on May 9, 2006 (gmt 0)

10+ Year Member



thanks jatar..

but when i do echo $taxprice i dont get any figure of price and add_tax. I put the figures as int

Flolondon

6:08 pm on May 9, 2006 (gmt 0)

10+ Year Member



its a drop down menu using switch/case function and when they pick the item they get a price echoed.
The price and add tax i want also to be echoed

jatar_k

6:09 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the code I pasted above works, it gives me this as output

price before tax 6.00

price after tax 30.54

Flolondon

6:14 pm on May 9, 2006 (gmt 0)

10+ Year Member



ok, the selection is based on a drop down menu... its not what i would put in myself.

how can it work from a drop down menu.. thats is how i have done it rather than putting brown myself

jatar_k

6:22 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if it is posted then you need to get the value from the $_POST array

$cars = $_POST['mydropdownname'];

assuming the values in the drop down are the same as the values in your switch

NOTE: this isn't very secure as all posted values should betested/validated before being used

Flolondon

6:28 pm on May 9, 2006 (gmt 0)

10+ Year Member



echo " <td bgcolor=\'#CCFF99'>".$taxprice. "</td>\n";

ok it worked but i want it to echo within the html table.. please help. It does not seem to be working in the html table

jatar_k

6:30 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what do you mean by "does not seem to be working"

bad output, no output, error?

by the way you don't need to escape the single quote if you are using double quotes around the string

should be just
echo " <td bgcolor='#CCFF99'>" . $taxprice . "</td>\n";

Flolondon

6:34 pm on May 9, 2006 (gmt 0)

10+ Year Member



ok thanks jatar.. i tried that and it does not seem to be working within the hmtl... the output shows nothing..

Flolondon

6:37 pm on May 9, 2006 (gmt 0)

10+ Year Member



ok thanks jatar... I moved the function to the top of the page and then it worked... thanks for your help and support. I really do appreciate it. thanks

jatar_k

6:38 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well it is very difficult for me to say

if the value from the dropdown matches the values in your switch and we already know those functions are working with the changes we made then something else is messing up.

could be as simple as bad html

have you looked at the source of the page?

Flolondon

6:43 pm on May 9, 2006 (gmt 0)

10+ Year Member



its working now jatar.... I moved the function(s) to the top of the page of the .php and now its working fine. Thanks...

jatar_k

6:50 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice work