I am having trouble with a simple peice of php code.
I am working with 2 product pricing tiers. These are based on whether a user has logged in, or not.
If a user is not logged in, and the first price is empty; then the price is price1. If not, it is price1.
This works perfectly fine.
If a user is logged in, and the first price is empty; then the price is price1. If not, it is price2.
This is the way it should work, but what actually happens is this:
If a user is logged in, and the first price is empty; then the price is 0. If not, it is price2.
Why is my code producing this effect?
if (!userIsLoggedIn())
{
if (empty($prPrice2))
{
$prPrice = $prPrice1;
}
else
{
$prPrice = $prPrice1;
}
} else if (userIsLoggedIn())
{
if (empty($prPrice2))
{
$prPrice = $prPrice1;
}
else
{
$prPrice = $prPrice2;
}
} else
{
$prPrice = $prPrice1;
}
If anyone has any suggestions that could help me to resolve this issue, it would be greatly appreciated.
Thank you!
@Pekka, it is fairly complicated. I simply would like this to happen:
product 1 -> price 1 = 1.00
product 1 -> price 2 = 0.00
product 2 -> price 1 = 1.00
product 2 -> price 2 = 0.80
If a user is logged in but the price2 field is empty, then the price variable will be price1. if not, then it will be price 2.
On the other hand, if a user is not logged in but the price2 field is empty, then the price variable will be price1. if not, then it will be price 1.
You code is overly complicated and contains some odd constructions. This is an equivalent but simplified version, but check the comment I added on the first else….
a slightly fancier way of expressing the same condition with the ternary statement (example is equivalent to else block of the outer if:
EDIT
Assuming that there is a way to differentiate between accountholders that haven’t logged in and users without accounts, your need to handle that in an outer condition, like this: