1

这是我的问题,

我需要显示所选月份所有生日的人,我已经了解如何将数据放在特定的表格单元格上,唯一的问题是数据也出现在其他不打算出现的表格单元格上。上图Ori Hendricks只能在下图上,2但它会出现在其他单元格上。

在此处输入图像描述

这是日历的数据

Array
(
    [2] => Array
        (
            [0] => Nomlanga Bentley**81
            [1] => Ori Hendricks**325
        )

    [5] => Array
        (
            [0] => Rina Nash**161
        )

    [10] => Array
        (
            [0] => Gary Mullins**252
        )

    [12] => Array
        (
            [0] => Nissim Donovan**207
        )

    [13] => Array
        (
            [0] => Harper Trevino**380
        )

    [14] => Array
        (
            [0] => Sacha Arnold**167
            [1] => Wade Hughes**322
            [2] => Brennan Long**3
        )

    [15] => Array
        (
            [0] => Gary Hart**290
        )

    [21] => Array
        (
            [0] => Alisa Bowman**201
        )

    [22] => Array
        (
            [0] => Dustin Meyers**330
        )

    [26] => Array
        (
            [0] => Ifeoma Swanson**331
        )

    [28] => Array
        (
            [0] => McKenzie Norton**327
            [1] => Asher Rasmussen**47
        )

    [29] => Array
        (
            [0] => Branden Conway**7
            [1] => Luke Sullivan**176
        )

    [30] => Array
        (
            [0] => Slade Moon**16
        )

)

我创建了一个MY_calendar类并创建了我自己的生成器,这是在单元格上显示数据的相关代码

if (isset($data[$day]))
{
    // Cells with content
    $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
    if(is_array($data[$day]))
    {       
        foreach($data[$day] as $k => $v)
        {
            $name = substr($v,0,strpos($v,'**'));
            $student_enrollment_id = substr($v,strpos($v,'**')+2);
            $data_formatted[$k] = '<a href="'.site_url('profile/student/'.$student_enrollment_id).'" target="_blank" class="btn btn-mini">'.trim($name).'</a>';
        }
        $data_from_array = implode('',$data_formatted);

        $out .= str_replace('{day}', $day, str_replace('{content}', $data_from_array, $temp));
    }else{
        $out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp));
    }
}
else
{
    // Cells with no content
    $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
    $out .= str_replace('{day}', $day, $temp);
}

我没有任何其他想法如何做到这一点,请帮忙。

4

1 回答 1

2

问题正在发生,因为数组索引仍然保存上一个循环中的值。

只需在上面粘贴的 MY_calendar 类代码片段中稍作更改即可:

<?php

if (isset($data[$day]))
{
    // Cells with content
    $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
    if(is_array($data[$day]))
    {       
        foreach($data[$day] as $k => $v)
        {
            $name = substr($v,0,strpos($v,'**'));
            $student_enrollment_id = substr($v,strpos($v,'**')+2);
            $data_formatted[$k] = '<a href="'.site_url('profile/student/'.$student_enrollment_id).'" target="_blank" class="btn btn-mini">'.trim($name).'</a>';
        }
        $data_from_array = implode('',$data_formatted);

        $out .= str_replace('{day}', $day, str_replace('{content}', $data_from_array, $temp));
        $data_formatted = array_fill(0,count($data_formatted), null);  // line added to reset the values in previous loop
    }else{
        $out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp));
    }
}
else
{
    // Cells with no content
    $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
    $out .= str_replace('{day}', $day, $temp);
}

这应该可以解决问题。

于 2013-07-29T08:26:49.817 回答