获取数据库时我得到了结果,一切正常,我从DB获得了价格,这也有效,但我真正需要的是,如果价格返回0而不是“0”应该被替换用“POD”代替。
感谢任何帮助。
这是我的查询代码:
$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>";
答案 0 :(得分:3)
请参阅this question。
这是您正在寻找的表达方式。
$price = ($row[price] === 0) ? "P.O.D." : ("£ ".$row[price]);
修改:旁注:由于this question中描述的问题,我使用=== 0
。
答案 1 :(得分:2)
您可以将查询更改为:
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
但为什么不在PHP中处理这个条件?
答案 2 :(得分:0)
ternary
运算符:
$price = ($row[price] == 0 ? 'P.O.D' : '£ ' . $row[price]);
答案 3 :(得分:0)
尝试:
$price = (empty($row[price]) ? "P.O.D" : "£ " . $row[price]);