2

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

该表由 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
        (
        )

)

我想将这个数组示例转换为表格。目前我发现最简单的方法是将每天打印为 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

编辑:

上表来自建议的答案,下一张是我想要的

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

4

3 回答 3

2

你的问题很模糊。您是说您想构建一个<ul>,同时拥有工作代码,而您实际上想要构建一个表。唔。

以下是你可以做一张桌子的方法:

// 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>';
于 2012-12-19T14:17:10.833 回答
1

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

//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>";

应该足以让你走上正轨。

于 2012-12-19T16:53:16.130 回答
0

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

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
            (
            )
        );
于 2012-12-19T14:34:32.117 回答