【问题标题】:Complex multidimensional loop into a table复杂的多维循环成表
【发布时间】:2012-12-19 14:02:24
【问题描述】:

我在从复杂循环中填充表时遇到问题。

该表由 7 天日历表组成,每天应包含其下的当天事件。

Array
(
    [2012-12-16] => Array
        (
        )

    [2012-12-17] => Array
        (
            [0] => Array
                (
                    [date] => 2012-12-17
                    [time_booked] => 00:09:00
                    [time_start] => 00:00:00
                    [time_end] => 00:00:00
                    [name] => maha mostafa elrashed
                    [first_name] => marwan
                    [title] => root cannal
                )

            [1] => Array
                (
                    [date] => 2012-12-17
                    [time_booked] => 09:00:00
                    [time_start] => 00:00:00
                    [time_end] => 00:00:00
                    [name] => demo demo eldemo
                    [first_name] => marwan
                    [title] => ultrasound
                )

        )

    [2012-12-18] => Array
        (
            [0] => Array
                (
                    [date] => 2012-12-18
                    [time_booked] => 09:00:00
                    [time_start] => 00:00:00
                    [time_end] => 00:00:00
                    [name] => demo demo eldemo
                    [first_name] => marwan
                    [title] => root cannal
                )

        )

    [2012-12-19] => Array
        (
        )

    [2012-12-20] => Array
        (
        )

    [2012-12-21] => Array
        (
        )

    [2012-12-22] => Array
        (
        )

)

我想将这个数组示例转换成表格。目前我发现最简单的方法是将每个 day 打印为 ul 并浮动离开它们,但我真正想要的是用它们制作一个表格,以便将来能够使用 jQuery 对该表格进行排序。

foreach($ztable as $t){
    echo"\n<ul style='float:left;width:140px'>";
    foreach($t as $r){
        echo "<li class='{$r['first_name']}'>{$r['name']}</li>";
    }
        echo "</ul>\n";
}

有什么建议吗? 顺便说一句,我正在使用 Codeigniter 2

编辑:

上面的表格来自建议的答案,下面的表格是我想要的。

【问题讨论】:

标签: php codeigniter multidimensional-array


【解决方案1】:

你的问题很模糊。你是说你想建立一个&lt;ul&gt;,同时有工作代码,而你实际上想建立一个表。嗯。

以下是制作表格的方法:

// list of fields that will be rows
$fields = array(
    'date' => 'Date', 
    'time_booked' => 'Time of booking', 
    'time_start' => 'Start time',
    'time_end' => 'End time',
    'name' => 'Name',
    'first_name' => 'First name',
    'title' => 'Title'
    );

// echo table header
echo '<table border="1"><tr><th>Day</th>';
foreach ($fields as $field)
{
    echo '<th>' . $field . '</th>';
}
echo '</tr>';

// echo table rows
foreach($ztable as $day => $data)
{
    echo '<tr><td>' . $day . '</td>';
    // if some field is not present for current day, we show '-'
    foreach ($fields as $field_name => $field)
    {
        echo '<td>' . (isset($data[$field_name]) ? $data[$field_name] : '-') . '</td>';
    }
    echo '</tr>';
}
echo '</table>';

【讨论】:

  • 我认为这不会完全有效;需要遍历$data,因为它将是一个数组。 ` if(!empty($data)){ foreach($data as $k => $v){ echo $v[$field_name]."
    "; } }else{ 回声“-”; }`
  • @stormdrain 从提供的数组来看,他可能有几天没有数据,我假设有些会有不完整的数据集。这就是为什么循环通过$data 可能会破坏表格布局,而循环通过预定义的字段集将始终产生相同的布局,有数据或没有数据的'-'。
  • 在我评论的循环中,它首先检查数组是否为空,是否在其中放置“-”:if(! empty($data))
  • 好吧,我希望它在一个表中,但我希望日期是在 以便一天可以包含多于 1 行(行将仅显示 [first_name]
【解决方案2】:

既然你添加了一张你想要的图片,我把一些看起来很粗糙的代码放在一起得到结果:

//provided $a is the array
//table header with dates
echo "<table class='table table-bordered'>";
echo "<tr>";

foreach($a as $date => $data){
    echo "<th>$date</th>";
}

echo "</tr>";

//table row for appointment dates
echo "<tr>";

foreach($a as $k=>$v){

    //if the array value isn't empty, we have data
    if(! empty($v)){

        echo "<td>";
        echo "<ul>";

        //loop through the array to get all the names
        foreach($v as $k => $v){

            echo "<li>".$v['name']."</li>";

        }
        echo "</ul>";
        echo "</td>";

    }else{
            //there was no data for the date; empty td
        echo "<td></td>";

    }

}

echo "</tr></table>";

应该足以让您走上正轨。

【讨论】:

    【解决方案3】:

    我认为您的数组格式不正确。请尝试这种格式。

    Array('2012-12-16' => Array(),
                '2012-12-17' => Array
                    (
                    '0' => Array
                        (
                        'date' => '2012-12-17',
                        'time_booked' => '00:09:00',
                        'time_start' => '00:00:00',
                        'time_end' => '00:00:00',
                        'name' => 'maha mostafa elrashed',
                        'first_name' => 'marwan',
                        'title' => 'root cannal'
                    ),
                    '1' => Array
                        (
                        'date' => '2012-12-17',
                        'time_booked' => '09:00:00',
                        'time_start' => '00:00:00',
                        'time_end' => '00:00:00',
                        'name' => 'demo demo eldemo',
                        'first_name' => 'marwan',
                        'title' => 'ultrasound'
                    )
                ),
                '2012-12-18' => Array
                    (
                    '0' => Array
                        (
                        'date' => '2012-12-18',
                        'time_booked' => '09:00:00',
                        'time_start' => '00:00:00',
                        'time_end' => '00:00:00',
                        'name' => 'demo demo eldemo',
                        'first_name' => 'marwan',
                        'title' => 'root cannal'
                    )
                ),
                '2012 - 12 - 19' => Array
                (
                ),
                '2012 - 12 - 20' => Array
                (
                ),
                '2012 - 12 - 21' => Array
                (
                ),
                '2012 - 12 - 22' => Array
                (
                )
            );
    

    【讨论】:

    • 我很确定您在 OP 中看到的是数组的转储,而不是它的设置方式。
    • @stormdrain yp 它是数组的转储
    猜你喜欢
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多