【发布时间】:2015-03-09 22:45:35
【问题描述】:
我有一个包含 id、text 和 type 字段的数据库表。在 type 中,我可以有值 1 和 2。如果返回类型为 1,我想显示一个 html 输出,如果类型为 2,我想显示另一个输出。
表:
id text type
1 Some text 1
2 Blah Blah 2
所以我的简单查询SELECT * from table 将返回值数组。在我的 HTML 中,我想像这样输出它:
<?php foreach ($model as $data): ?>
<h4>Text where status is 1:</h4>
echo $data['text'];
<h4>Text where status is 2:</h4>
echo $data['text'];
<?php endforeach ?>
我尝试过在 foreach 中使用 if,如下所示:
<?php if ($data['type'] == "1"): ?>
<h4>Text where status is 1:</h4>
echo $data['text'];
<?php else: ?>
<h4>Text where status is 2:</h4>
echo $data['text'];
<?php endif ?>
但这并没有真正起作用。我的解决方案是让一个查询返回类型为 1 的文本,另一个返回类型为 2 的文本吗?还是有更好的解决方案?
我想要这样的输出:
<h4> Text where status is 1 </h4>
First text.
Second text.
Third text.
<h4> Text where status is 2 </h4>
Fourth Text.
Fifth Text.
【问题讨论】:
-
是
$model包含SQL查询结果的变量吗? -
是的,它包含查询结果
-
$offer 是什么?我的意思是,你需要什么条件?如果 $offer 为 "1" 则打印状态为 1 和 $data['text'] 的文本?
-
抱歉,$offer 是 $data,这里发的时候忘记替换名字了。