【问题标题】:How to display php results based on different values returned from mysql如何根据mysql返回的不同值显示php结果
【发布时间】:2015-03-09 22:45:35
【问题描述】:

我有一个包含 idtexttype 字段的数据库表。在 type 中,我可以有值 12。如果返回类型为 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,这里发的时候忘记替换名字了。

标签: php html mysql css


【解决方案1】:

在这种情况下,我会这样走,先生:

$outputs = array("1" => array(),"2" => array());

foreach ($model as $data) {
   $outputs[$data['type']][] = $data['text'];
}

foreach ($outputs as $type => $values) {
   echo "<h4>Text where status is {$type}: </h4><br />";
   foreach ($values as $text) {
      echo $text . " <br />";
   }
}

解释:

在 PHP 中,我们不能以这种方式内联打印所有内容,这就是我们这样做的原因:

  1. 声明一个包含两个值的数组,它们是需要检查的“$data['type']”。通过这样做,我们将类型声明为键和对应于每个键的数组。
  2. 当我们循环遍历结果时,我们将结果推送到上面相应键的正确数组中。在具有 'type' 2 的结果上传递 $output[$data['type']] 与执行 $output['2'] 完全相同。下一个 [] 表示“将该元素推入该数组”,因此我们实际上是根据其键填充数组。
  3. 由于数组的结构通过再次循环,我们通过获取 TYPE ("key") 和它的值来循环它,即 $data['text'] 并且我们首先打印类型(在动态上,所以如果明天你想添加一个类型 3,你可以毫无问题地做到这一点),然后是对应于它的类型的值。

输出:

<h4>Text where status is 1: </h4><br />hello <br />ohai dude <br /><h4>Text where status is 2: </h4><br />hello dude <br />

来自这样的输入数组:

$model = array(
0 => array( "id" => "0",
            "type" => "1",
            "text" => "hello"
            ),
1 => array( "id" => "1",
            "type" => "2",
            "text" => "hello dude"
            ),
2 => array( "id" => "2",
            "type" => "1",
            "text" => "ohai dude"
            )
);

【讨论】:

  • 这将输出“状态为 2 的文本”,返回状态为 2 的每一行。我需要将其输出在顶部,然后在其下输出类型为 2 的所有行。
  • 在上面? “在上面”是什么意思?
  • 哦,好吧,我明白了。伙计,你本来可以更清楚的!敬请期待。
  • 我通过提供所需输出的示例来编辑我的问题。
  • 我现在已经编辑了我的问题,应该更清楚了。
【解决方案2】:
// connect to db first
$link = mysqli_connect('host','user','pass','database');
// get something from db
$result = mysqli_query($link, "SELECT * FROM table ORDER BY type");

// loop through result and echo
$flag1 = false;
$flag2 = false;
while($data = mysqli_featch_array($result, MYSQLI_ASSOC)) {
   if($data['type'] == "1") {
      if(!$flag1) {
         echo '<h4>Text where status is 1:</h4>';
         $flag = true;
      }
      echo $data['text'];
   } else {
      if(!$flag2) {
         echo '<h4>Text where status is 2:</h4>';
         $flag = true;
      }
      echo $data['text'];
   }   
}

【讨论】:

  • 我需要这些标题:&lt;h4&gt;Text where status is 1:&lt;/h4&gt;。这适用于所有状态为 1 的文本,然后是:&lt;h4&gt;Text where status is 2:&lt;/h4&gt;,状态为 2 的文本
  • 已编辑查询,因此结果按类型排序。添加标志变量以仅输出一个标题。
  • @black-room-boy 然后而不是 echoing 在循环中,保存到一个数组,然后打印任何你想要的。
猜你喜欢
  • 1970-01-01
  • 2015-02-24
  • 2019-08-11
  • 2017-03-24
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2011-06-07
相关资源
最近更新 更多