IF/THEN Statement (PHP) for custom fields

3 posts by 2 authors in: Forums > CMS Builder
Last Post: December 22, 2007   (RSS)

By rasbro - December 21, 2007

I was wondering if anyone knew how I could use an if/then statement with PHP to say that if a custom field, such as product sale_price is empty, then display only the price, else display price and sale_price fields.

I know how to do the if/then statements in PHP but I am unsure of what value to specify for the empty field. Here is an example I am working with.

<?php if ($record['sale_price'] == "empty"): ?>
Price: $<?php echo $record['price'] ?><br/>
<?php else: ?>
Price: $<?php echo $record['price'] ?><br/>
Sale Price: $<?php echo $record['sale_price'] ?><br/>
<?php endif ?>

Thanks,
Brian

Re: [rasbro] IF/THEN Statement (PHP) for custom fields

By Dave - December 22, 2007

Looks pretty close, generally you can just specify "" to match an empty fields. So you could write it like this:

<?php if ($record['sale_price'] == ""): ?>
Price: $<?php echo $record['price'] ?><br/>
<?php else: ?>
Price: $<?php echo $record['price'] ?><br/>
Sale Price: $<?php echo $record['sale_price'] ?><br/>
<?php endif ?>


or another way (maybe simpler) would be like this:

Price: $<?php echo $record['price'] ?><br/>

<?php if ($record['sale_price'] != ""): ?>
Sale Price: $<?php echo $record['sale_price'] ?><br/>
<?php endif ?>


With != to mean "sale price is not empty". Hope that helps! :)
Dave Edis - Senior Developer
interactivetools.com