来自数组的html输出逻辑

时间:2013-03-04 18:01:40

标签: php html foreach logic output

我的数据来自数据库,并且已grouped by day_id,因此数组按day_id排序。

代码+提供的示例输出,很难用文字表达。

我从this accepted answer到目前为止得到的灵感,但我需要稍作调整。

$array = array(
            0 => array(
                'id' => '5',
                'day_id' => '1',
                'Foo' => array(
                    'id' => '28',
                    'name' => 'My day_id is 1'
                )
            ),
            1 => array(
                'id' => '6',
                'day_id' => '1',
                'Foo' => array(
                        'id' => '29',
                        'name' => 'My day_id is 1'
                )
            ),
            2 => array(
                'id' => '7',
                'day_id' => '2',
                'Foo' => array(
                     'id' => '30',
                     'name' => 'My day_id is 2'
                )
            ),    
            3 => array(
                'id' => '8',
                'day_id' => '3',
                'Foo' => array(
                       'id' => '31',
                      'name' => 'My day_is 3'
                )
            )
);

我的代码输出它:

$day = 0;
foreach($array as $a) {

     // $a['day_id'] = 1, condition is true. output header once.
    if($day != $a['day_id']) {
        echo '<h3>The Day is ' . $a['day_id'] . '</h3>' . "\n";
    }

    // output the elements Foo key content.
    echo $a['Foo']['name'] . "\n";

    // store the current value to compare on the next iteration
    // $a['day_id'] will eventually be 2, causing the condition to fire again.
    $day = $a['day_id']; 
}

输出:

<h3>The Day is 1</h3>
My day_id is 1
My day_id is 1

<h3>The Day is 2</h3>
My day_id is 2

<h3>The Day is 3</h3>
My day_is 3

我现在想在DIV中包装每一个“Day”,但我无法弄清楚逻辑。如果我用open div替换<h3>,我不知道在哪里放置结束</div>。到目前为止,我的尝试只是导致每天都有一个open div,而不是一个封闭的div,所以它们是嵌套的,而不是分开的。

我无法放置</div><div class="day-X">,因为在开始时会有一个额外的</div>会破坏布局。

期望的输出:

<div class="day-1">
  <h3>The Day is 1</h3>
  My day_id is 1
  My day_id is 1
</div>
<div class="day-2">
  <h3>The Day is 2</h3>
  My day_id is 2
</div>

<div class="day-3">
  <h3>The Day is 3</h3>
  My day_is 3
</div>

希望这是有道理的 - 谢谢

1 个答案:

答案 0 :(得分:2)

您需要循环两次,一次为天,一次为当天的项目。在不使用任何其他SQL查询的情况下,您需要先将数据存储在数组中。然后你可以循环播放这些日子,每天你都会回显<div>和日期标题,最后你会回复</div>

在foreach内部,您可以使用相同的数组再次循环,以检查每个项目是否有相同的一天。如果是,则回显项目的名称,否则不做任何事情。

我已经测试了这段代码,您可以查看它,如果您遇到任何问题,请告诉我。

foreach($array as $day){
    $days[] = $day['day_id']; // store all the days in an array
}
    // now days[] has [1,1,2,3]

$days = array_unique($days); // get unique days only no duplicated 

    //now days[] has [1,2,3]


foreach($days as $d){ // for each day

    echo '<div class = "day-'.$d.'">';
     echo '<h3>The Day is ' . $d . '</h3>' . "\n";

    foreach($array as $a) { // check all other items if the item has the same day then echo 

        if($a['day_id'] == $d){

            echo $a['Foo']['name'] . "\n";
        }
    }

    echo '</div>';

}

输出:

<div class = "day-1">
    <h3>The Day is 1</h3>
        My day_id is 1
        My day_id is 1
</div>

<div class = "day-2">
    <h3>The Day is 2</h3>
        My day_id is 2
</div>

<div class = "day-3">
    <h3>The Day is 3</h3>
        My day_is 3
</div>