【问题标题】:Passing Data From Database into CodeIgniter Calendar Library将数据从数据库传递到 CodeIgniter 日历库
【发布时间】:2011-12-01 10:14:32
【问题描述】:

免责声明:我是 Web 开发新手

到目前为止:我一直在将 MySQL 数据库表中的主键数据传递到生成时显示在日历上的日历类库列表项的 id="" 字段中,并且我还传递了另一个与列表项的相应主键的行关联的值也是如此。

场景:我需要将另一个值从主键的同一行传递到列表项的 class="" 中,但我碰壁了。我可能需要在我的模式中创建一个新功能,或者在生成所述日历的控制器中做一些事情,或者我可能完全忽略了一些完全简单的事情。无论如何,我可以使用一些非常感谢的帮助。非常感谢!

附:如果您需要更多信息来帮助我,请告诉我。再次感谢!你们都很棒!

型号:

class Events_model extends crud_model {

    public function __construct()
    {
        parent::__construct();

        $this->pk = 'id';
        $this->table_name = 'events';
    }

    public function get_events_for_calendar($user_id, $year = FALSE, $month = FALSE)
    {

        if ( ! $year )
        {
            $year = date("Y");
        }

        if ( ! $month )
        {
            $month = date("m");
        }

        $this->db->where("user_id", $user_id);
        $events = parent::get_all();

        $cal_events = array();

        foreach ($events as $e)
        {
            if ( date("Y", $e->date) == $year && date("m", $e->date) == $month)
            {       
                $day = intval( date("d", $e->date));

                //Swap out to change between displaying type or title of events listed -lrussell810

                //$cal_events[$day][$e->id] = $e->type;
                $cal_events[$day][$e->id] = $e->title;
            }
        }
        return $cal_events;
    }

控制器:

$this->load->library('calendar', $config);

        $title['title'] = 'Planner';

        $data = $this->events->get_events_for_calendar($this->end_user->id, $this->uri->segment(4), $this->uri->segment(5));

        $calendar['calendar'] = $this->calendar->generate($this->uri->segment(4), $this->uri->segment(5), $data);

        $this->load->view('public/head_view', $title);
        $this->load->view('user/header_view');
        $this->load->view('user/planner_view', $calendar);
        $this->load->view('user/footer_view');

日历库:

// If more than one event on the same day
                      if (is_array($data[$day]))
                      {
                        $several_events = '';
                        foreach ($data[$day] as $key => $value)
                        {
                          $several_events .= '<li class="" id="'.$key.'">'.$value.'</li>';
                        }
                        $out .= str_replace('{day}', $day, str_replace('{content}', $several_events, $temp));
                      }

其他日历库信息:

// --------------------------------------------------------------------

    /**
     * Initialize the user preferences
     *
     * Accepts an associative array as input, containing display preferences
     *
     * @access  public
     * @param   array   config preferences
     * @return  void
     */
    function initialize($config = array())
    {
        foreach ($config as $key => $val)
        {
            if (isset($this->$key))
            {
                $this->$key = $val;
            }
        }
    }

【问题讨论】:

  • 你能澄清你的问题吗?你到底想做什么?向'&lt;li class="" id="'.$key.'"&gt;'.$value.'&lt;/li&gt;' 类添加值?此外,您可能应该将您的 id 称为 event_'.$key.',因为数字 ID 不正确(就像在编程中说 1 = "cat"
  • 是的,我正在尝试在课程引用中添加另一个值,我一定会在那里更改我的 id :) 感谢您的提示!
  • 嗨@Jakub想知道你把这段代码放在哪里
  • // 如果同一天有多个事件 if (is_array($data[$day])) { $several_events = ''; foreach ($data[$day] as $key => $value) { $several_events .= '
  • '.$value.'
  • ' ; } $out .= str_replace('{day}', $day, str_replace('{content}', $several_events, $temp)); }

标签: php mysql database codeigniter


【解决方案1】:

如果我理解正确,您想在事件以 html 的形式输出时添加“更多”,因此您将引用 pk 作为 ID 添加(因此我在上面的评论)。

您可以像这样为每个事件创建一个关联数组:

    $cal_events = array();

    foreach ($events as $e)
    {
        if ( date("Y", $e->date) == $year && date("m", $e->date) == $month)
        {       
            $day = intval( date("d", $e->date));

            //just made up examples below
            $cal_events[$day][$e->id]['class'] = $e->class;
            $cal_events[$day][$e->id]['type'] = $e->type;
            $cal_events[$day][$e->id]['id'] = $e->title; 
        }
    }
    return $cal_events;

您稍后将在日历库中引用该数组,如下所示:

// If more than one event on the same day
if (is_array($data[$day])){
    $several_events = '';
    foreach ($data[$day] as $key => $value)
    {
        $several_events .= '<li class="'.$value['class'].'" id="'.$value['id'].'">'.$value['type'].'</li>';
    }
    $out .= str_replace('{day}', $day, str_replace('{content}', $several_events, $temp));
}

希望这就是您正在寻找的。​​p>

【讨论】:

  • 你是个天才。非常感谢您的所有帮助!
  • @lrussell810,嗯,不是天才,只是一个简单的关联数组;)享受吧!
猜你喜欢
相关资源
最近更新 更多
热门标签