0

When fetching DB I am getting results and everything works to the point where I am getting a price from DB, which is also works, but what I really need to have is if price returned 0 than that "0" should be replace with "P.O.D" instead.

Any help is appreciated.

here is my query code:

 $samples = "SELECT * FROM materials, category_price WHERE materials.type = :cat AND materials.supplier = '$supplier' AND materials.category_id = category_price.category_id";
$res = $db->prepare($samples);
$res->execute(array(':cat' => $category));
$count = $res->rowCount();
if($count > 0)
echo "
<section class=\"border mar_t_40\">
"; 
while ($row = $res -> fetch()){
    $postimggranite = $row[image];
    $postidgranite = $row[id];
    $postname = $row[mat_name];
    $folder = $row[type];
    $folder = strtolower($folder);
    $supplier = strtolower($supplier);
    $category_id = $row[category_id];
    $price = ("£ ".$row[price]);


print<<<END
<span class="grid white_back mar_l_30">
<h3>$price</h3>
<a class="fancybox" href="$img_path/$folder/$supplier/large/$postimggranite" rel="group[$postidgranite]" title="$postname"><img alt="$row[name]" src="$img_path/$folder/$supplier/small/$postimggranite" width="100" height="100">$postname</a>
</span>
END;

}
echo "<div class=\"clearfloat\"></div></section>";
AlexB
  • 2,164
  • 6
  • 27
  • 61

4 Answers4

3

See this question.

This is the expression you are looking for.

$price = ($row[price] === 0) ? "P.O.D." : ("£ ".$row[price]);

Edit: Side note: I use === 0 because of the problem that is described in this question.

Community
  • 1
  • 1
Sumurai8
  • 20,333
  • 11
  • 66
  • 100
2

You can change your query to something like:

SELECT some_fields, IF(price=0, 'P.O.D', price) AS price FROM materials, category_price WHERE materials.type = :cat AND materials.supplier = '$supplier' AND materials.category_id = category_price.category_id

But why not handle the condition within PHP?

Ronen Yacobi
  • 844
  • 9
  • 16
0

several ideas come to mind, one is the ternary operator:

$price = ($row[price] == 0 ? 'P.O.D' : '£ ' . $row[price]);
michi
  • 6,565
  • 4
  • 33
  • 56
0

Try:

$price = (empty($row[price]) ? "P.O.D" : "£ " . $row[price]);
FMB89
  • 1