我正在使用CodeIgniter
Calendar库.
>事件表
> extra_events表
问题我有2个foreach循环与第二个foreach循环$query2-> result_array()它不让我每天从数据库表获取第一个事件extra_events我也试图能够设置额外的限制事件表但没有工作.
来自extra_events表
例
Test 2 | Example 2 | Example 3
它应该产生
Test 1 | Example 1 <!-- Missing Test 2 | Example 2 | Example 3
Question how can I make sure I get all the results correct for $query2->result_array().
获得功能
public function get_events($year,$month) { $calendar = array(); $this->db->select('*'); $this->db->from('events'); $this->db->where('year',$year); $this->db->where('month',$month); $query1 = $this->db->get(); foreach ($query1->result_array() as $event) { $this->db->select('*'); $this->db->from('extra_events'); $this->db->where('events_id',$event['events_id']); $this->db->where('year',$year); $this->db->where('month',$month); //$this->db->limit(2); $query2 = $this->db->get(); foreach ($query2->result_array() as $result) { if (array_key_exists($event['day'],$calendar)) { $calendar[$event['day']] = $calendar[$event['day']] . '<br/>' . $result['event']; } else { $calendar[$event['day']] = '<br/><b>From Events Table</b> <i class="fa fa-long-arrow-down"></i><br/>' . $event['event'] . '<hr><b>From Extra Events Table</b> <i class="fa fa-long-arrow-down"></i>' ; } } } return $calendar; }
现在解决了
$calendar [$event [‘day’]] =’< br />‘ . $事件[ ‘事件’];
下面是主事件的第一个foreach,然后是任何其他额外事件,使用存在数组键的第二个foreach.
This now also lets me control how many rows are now displayed in extra
events foreach loop.
public function get_calendar_events($year,$month) { $calendar = array(); $events = $this->get_events($year,$month); foreach($events as $event) { // Added line for main calendar event $calendar[$event['day']] = '<br/>' . $event['event']; // Passes main event id to extra_events get function $extra_events = $this->get_extra_events($event['events_id']); // checks if array key exists for extra event in side foreach loop foreach($extra_events as $extra_event) { if (array_key_exists($extra_event['day'],$calendar)) { $calendar[$extra_event['day']] = $calendar[$extra_event['day']] . '<br/>' . $extra_event['event']; } else { $calendar[$extra_event['day']] = $extra_event['event']; } } } return $calendar; }
日历模型
<?PHP class Model_calendar extends CI_Model { public function add_event() { $data = array( 'year' => $this->input->post('year'),'month' => ($this->input->post('month') > 10) ? $this->input->post('month') : '0' . $this->input->post('month'),'day' => $this->input->post('day'),'date' => $this->input->post('year') .'-'. $this->input->post('month') .'-'. $this->input->post('day'),'event' => $this->input->post('event'),'date_added' => mdate('%Y-%m-%d %H:%i:%s',now()) ); $this->db->insert($this->db->dbprefix . 'events',$data); } public function add_extra_event() { $data = array( 'events_id' => (int)$this->get_event_id(),'year' => $this->input->post('year'),now()) ); $this->db->insert($this->db->dbprefix . 'extra_events',$data); } public function get_event_id() { $this->db->where('year',$this->input->post('year')); $this->db->where('month',($this->input->post('month') > 10) ? $this->input->post('month') : '0' . $this->input->post('month')); $this->db->where('day',$this->input->post('day')); $query = $this->db->get($this->db->dbprefix . 'events'); if ($query->num_rows() > 0) { $row = $query->row(); return $row->events_id; } else { return false; } } public function check_event() { $this->db->where('year',$this->input->post('day')); $query = $this->db->get($this->db->dbprefix . 'events'); if ($query->num_rows() > 0) { return TRUE; } else { return FALSE; } } public function delete_events() { $this->db->where("date <",date('Y-m-d')); $this->db->delete($this->db->dbprefix . 'events'); } public function delete_extra_events() { $this->db->where("date <",date('Y-m-d')); $this->db->delete($this->db->dbprefix . 'extra_events'); } public function get_calendar_events($year,$month); foreach($events as $event) { $calendar[$event['day']] = '<br/>' . $event['event']; $extra_events = $this->get_extra_events($event['events_id']); foreach($extra_events as $extra_event) { if (array_key_exists($extra_event['day'],$calendar)) { $calendar[$extra_event['day']] = $calendar[$extra_event['day']] . '<br/>' . $extra_event['event']; } else { $calendar[$extra_event['day']] = $extra_event['event']; } } } return $calendar; } public function get_events($year,$month) { $this->db->where('year',$month); $events = $this->db->get($this->db->dbprefix . 'events'); return $events->result_array(); } public function get_extra_events($events_id) { $this->db->limit(5); $this->db->where('events_id',$events_id); $extra_events = $this->db->get($this->db->dbprefix . 'extra_events'); return $extra_events->result_array(); } }
日历控制器
<?PHP class Calendar extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('dashboard/model_calendar'); $this->load->library('calendar'); $this->load->library('form_validation'); } public function index() { if ($this->uri->segment(3) == FALSE) { $year = date('Y'); } else { $year = $this->uri->segment(3); } if ($this->uri->segment(4) == FALSE) { $month = date('m'); } else { $month = $this->uri->segment(4); } $prefs = array( 'start_day' => 'monday','show_next_prev' => true,'month_type' => 'long','day_type' => 'long','next_prev_url' => base_url('dashboard/calendar') ); $prefs['template'] = ' {table_open}<div class="table-responsive"><table border="0" cellpadding="0" cellspacing="0" class="table table-hover table-striped table-bordered calendar">{/table_open} {heading_row_start}<tr>{/heading_row_start} {heading_prevIoUs_cell}<th><a href="{prevIoUs_url}"><i class="fa fa-chevron-left fa-2x "></i></a></th>{/heading_prevIoUs_cell} {heading_title_cell}<th class="text-center" colspan="{colspan}">{heading}</th>{/heading_title_cell} {heading_next_cell}<th class="text-right "><a href="{next_url}"><i class="fa fa-chevron-right fa-2x"></i></a></th>{/heading_next_cell} {heading_row_end}</tr>{/heading_row_end} {week_row_start}<tr >{/week_row_start} {week_day_cell}<td class="text-center" style="height: 5rem;">{week_day}</td>{/week_day_cell} {week_row_end}</tr>{/week_row_end} {cal_row_start}<tr class="days">{/cal_row_start} {cal_cell_start}<td class="day">{/cal_cell_start} {cal_cell_content} <div class="day_number">{day}</div> <div class="content" style="margin-top: 0;">{content}</div> {/cal_cell_content} {cal_cell_content_today} <div class="day_number highlight">{day}</div> <div class="content" style="margin-top: 0;">{content}</div> {/cal_cell_content_today} {cal_cell_no_content} <div class="day_number">{day}</div> {/cal_cell_no_content} {cal_cell_no_content_today} <div class="day_number highlight">{day}</div> {/cal_cell_no_content_today} {cal_cell_blank} {/cal_cell_blank} {cal_cell_end}</td>{/cal_cell_end} {cal_row_end}</tr>{/cal_row_end} {table_close}</table></div>{/table_close} '; $this->calendar->initialize($prefs); $this->model_calendar->delete_events(); $this->model_calendar->delete_extra_events(); $events = $this->model_calendar->get_calendar_events($year,$month); $data['calendar'] = $this->calendar->generate($year,$month,$events); if ($this->uri->segment(3) == TRUE) { $data['view_more'] = site_url('report/events/'. $year .'/'. $month); } else { $data['view_more'] = site_url('report/events'); } $this->form_validation->set_rules('event','Calendar Event','trim|required'); if ($this->form_validation->run() == FALSE) { $this->load->view('dashboard/calender_view',$data); } else { if ($this->model_calendar->check_event() == TRUE) { $this->model_calendar->add_extra_event(); } else { $this->model_calendar->add_event(); } $this->session->set_flashdata('added_event','<i class="fa fa-check-circle"></i> You have added event to your calendar!'); redirect('common/dashboard'); } } }