Jump to content

if field empty echo other field


closer12
 Share

Recommended Posts

Hi. i am using the code below for showing price1 field if empty i show other text.

But i need code if price1 is empty show price2 if both is empty show the text.. how i can do that i need help with php code?

 if ($item->price1) { echo 'From: € ' .  $item->price1 ;} else { echo 'please ask'; }
Link to comment
Share on other sites

Well the easiest way seems to simply use an else if:

if ($item->price1) {
  echo "From euros {$item->price1}";
} else if ($item->price2) {
  echo "From euros {$item->price2}";
} else {
  echo 'please ask';
}
  • Like 3
Link to comment
Share on other sites

You can always just store the values to variable and see if it's filled. Also, note that PW provides a native way (shortcut, really) for getting the first non-empty field value: $item->get("field1|field2|field3") etc.

// like this:
$price = $item->get("price1|price2");
if ($price) echo "From: € " . $price;
else echo "please ask";

// this works too:
$price = $item->get("price1|price2");
echo $price ? "From: € " . $price : "please ask";
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...