Citra45Abadi Citra45Abadi - 5 months ago 10
MySQL Question

show different statement, depends if the variable for price is empty or not

hi this is quite simple but I don't know why this does not work

so I want to show a price for a product, but some products does not have price yet, so instead of price, I want to show text like "please call our CS"

here is the code I made :

<?php $harga = number_format(($r[harga]),0,",",".");

$hargatetap= if (!empty($harga)){
"<div class='prod_price'><span class='price'> <br /></span>&nbsp;<span class='price'>Harga Rp.$harga </span><br /></div>
</div>";
} else {
"<div class='prod_price'><span class='price'> <br /></span>&nbsp;<span class='price'>Harga Rp.$harga </span><br /></div>
<span class='price2'> <br /></span><span class='stok2'>(Hub CS) </span></div>";
}


$divharga=$hargatetap;
?>


$divharga will be called in the other php file (I'm sure this is obvious)

Answer

Syntax like $hargatetap= if (!empty($harga)){ is an invalid syntax.

Simplest solution is:

if (!empty($harga)) {  
    $divharga = "<div class='prod_price'><span class='price'> <br /></span>&nbsp;<span class='price'>Harga Rp.$harga </span><br /></div></div>";
} else {
    $divharga = "<div class='prod_price'><span class='price'> <br /></span>&nbsp;<span class='price'>Harga Rp.$harga </span><br /></div><span class='price2'> <br /></span><span class='stok2'>(Hub CS) </span></div>";
}

echo $divharga;