我正在使用CodeIgniter Calendar库。
我有两个表,我从我的数据库中获取日历数据。
问题我有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
问题如何确保我在$ query2-&gt; 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;
}
数据库
答案 0 :(得分:5)
我不得不创建两个表,然后在模型函数中添加了这一行
$calendar[$event['day']] = '<br/>' . $event['event'];
主要事件的第一个foreach,然后是任何其他额外事件,使用存在数组键的第二个foreach。
现在这也让我可以控制现在显示多少行 事件foreach循环。
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'),
'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 . '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('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) {
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) {
$calendar = array();
$events = $this->get_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', $year);
$this->db->where('month', $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');
}
}
}
答案 1 :(得分:3)
你可以限制foreach只获得2个事件。
在get_events()函数中限制foreach循环。
public function get_events($year, $month) {
$calendar = array();
$this->db->where('year', $year);
$this->db->where('month', $month);
$query = $this->db->get('events');
$results = $query->result_array();
$i = 0;
foreach ($results as $event) {
if ($i < 2) { // getting / fetching / extracting value from foreach for 2 times only
if (array_key_exists($event['day'], $calendar)) {
$calendar[$event['day']] = $calendar[$event['day']] . '
<hr/>
<div class="text-center">
<span class="label label-info">New</span>
</div>
<br/>
<div class="clearfix">
<div class="pull-left">
<ul class="list-unstyled text-center">
<li >'
. word_limiter(anchor(base_url('report/events/' . $year . '/' . $month . '/?event_id=' . $event['events_id']), $event['event']), 4) . '
</li>
</ul>
</div>
<div class="pull-right">
<form id="form-event" action="' . base_url('dashboard/calendar') . '" class="form-inline" method="post" enctype="multipart/form-data" onsubmit="return confirm(\'Are you sure you want to remove this!\')";>
<input type="hidden" name="delete_id" value="' . $event['events_id'] . '">
<a href="' . base_url('report/events/edit' . '/?event_id=' . $event['events_id']) . '" role="button" class="btn btn-primary"><i class="fa fa-pencil"></i></a>
<button type"submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
</div>
</div>';
} else {
$calendar[$event['day']] = '
<div class="text-center">
<span class="label label-info">New</span>
</div>
<br/>
<div class="clearfix">
<div class="pull-left">
<ul class="list-unstyled text-center">
<li >'
. word_limiter(anchor(base_url('report/events/' . $year . '/' . $month . '/?event_id=' . $event['events_id']), $event['event']), 4) . '
</li>
</ul>
</div>
<div class="pull-right">
<form id="form-event" action="' . base_url('dashboard/calendar') . '" class="form-inline" method="post" enctype="multipart/form-data" onsubmit="return confirm(\'Are you sure you want to remove this!\')";>
<input type="hidden" name="delete_id" value="' . $event['events_id'] . '">
<a href="' . base_url('report/events/edit' . '/?event_id=' . $event['events_id']) . '" role="button" class="btn btn-primary"><i class="fa fa-pencil"></i></a>
<button type"submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>
</div>
</div>
';
}
$i++;
}
}
return $calendar;
}