The below image is the pictorial representation of what I am trying to get.
我的代码:
application / controllers / DatePic_Controller.php
在控制器中,以月和年为参数调用build_display()函数。这个月和一年传递给模型文件的generate函数,在这里我们称为codeigniter日历库的generate函数。
public function index() {
$year = date('Y');
$month = date('m');
$month2_year = date("Y",strtotime("+1 month"));
//echo 'month2_year:'.$month2_year;
$month2_month = date("m",strtotime("+1 month"));
//echo 'month2_month:'.$month2_month;
$month3_year = date("Y",strtotime("+2 month"));
//echo 'month2_year:'.$month2_year;
$month3_month = date("m",strtotime("+2 month"));
$cal_data['month1'] = $this->build_display($year,$month);
//print_r($cal_data['month1']);
$cal_data['month2'] = $this->build_display($month2_year, $month2_month);
//print_r($cal_data['month2']);
$cal_data['month3'] = $this->build_display($month3_year, $month3_month);
$this->load->view('calendar_view', $cal_data);
}
public function build_display($year,$month){
//ini_set('display_errors', '1');
$this->load->model("DatePic_model");
if($day = $this->input->post('day')){
$this->DatePic_model->add_calendar_data("$year-$month-$day",$this->input->post('data'));
}
$calendar = $this->DatePic_model->generate($year,$month);
//print_r($calendar);
//echo $calendar_view = $this->load->view("datepic_view",$data,true);
return $calendar;
}
application / models / DatePic_model.php
在model中,我使用了conf变量来存储日历首选项和模板。
此外,我使用了get_calendar_data()来在日历本身中显示相应日期的假期。但是它不起作用.....
public function __construct(){
$this->conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => 'http://localhost/STUDY/CI_Mini_Projects/DatePic_Controller/display',
'day_type' => 'short'
);
$this->conf['template'] = '
{table_open}<table border="0" cellpadding="0" cellspacing="0" class="calendar">{/table_open}
{heading_row_start}<tr class="heading">{/heading_row_start}
{heading_previous_cell}<th><a href="{previous_url}"><<</a></th>{/heading_previous_cell}
{heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
{heading_next_cell}<th><a href="{next_url}">>></a></th>{/heading_next_cell}
{heading_row_end}</tr>{/heading_row_end}
{week_row_start}<tr class="weekdays">{/week_row_start}
{week_day_cell}<td>{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_start_today}<td>{/cal_cell_start_today}
{cal_cell_start_other}<td class="other-month">{/cal_cell_start_other}
{cal_cell_content}
<div class="day_num">{day}</div>
<div class="content">{content}</div>
{/cal_cell_content}
{cal_cell_content_today}
<div class="day_num highlight">{day}</div>
<div class="content">{content}</div>
{/cal_cell_content_today}
{cal_cell_no_content}
<div class="day_num">{day}</div>
{/cal_cell_no_content}
{cal_cell_no_content_today}
<div class="day_num highlight">{day}</div>
{/cal_cell_no_content_today}
{cal_cell_blank} {/cal_cell_blank}
{cal_cell_other}
<div class="day_num">{day}</div>
{/cal_cel_other}
{cal_cell_end}</td>{/cal_cell_end}
{cal_cell_end_today}</td>{/cal_cell_end_today}
{cal_cell_end_other}</td>{/cal_cell_end_other}
{cal_row_end}</tr>{/cal_row_end}
{table_close}</table>{/table_close}
';
}
public function get_calendar_data($year,$month){
$query = $this->db->select('date,data')->from('holiday')->like('date',"$year-$month",'after')->get();
//SELECT date,data FROM holiday WHERE date LIKE '2019-04%'; ==> 2019-04-19 Nineteen
$cal_data = array();
foreach($query->result() as $row){
$cal_data[substr($row->date,8,2)] = $row->data;
//2019-04-19 : 19
}
print_r($cal_data);
//return $cal_data;
/* If you want to control where the wildcard (%) is placed, you can use an optional third argument. Your options are ‘before’, ‘after’, ‘none’ and ‘both’ (which is the default).
$this->db->like('title', 'match', 'before'); // Produces: WHERE `title` LIKE '%match' ESCAPE '!'
$this->db->like('title', 'match', 'after'); // Produces: WHERE `title` LIKE 'match%' ESCAPE '!'
$this->db->like('title', 'match', 'none'); // Produces: WHERE `title` LIKE 'match' ESCAPE '!'
$this->db->like('title', 'match', 'both'); // Produces: WHERE `title` LIKE '%match%' ESCAPE '!' */
}
public function add_calendar_data($date,$data){
if($this->db->select('date')->from('holiday')->where('date',$date)->count_all_results()){
$this->db->where('date',$date)
->update('holiday',array(
'date'=>$date,
'data'=>$data));
echo "update";
}else{
$this->db->insert('holiday',array(
'date'=>$date,
'data'=>$data));
}
echo "insert";
}
public function generate($year,$month){
$this->load->library("calendar",$this->conf);
/* $cal_data = array(
15 => base_url().'User_Controller/create_user',
17 => base_url().'role'
); */
//$cal_data = $this->add_calendar_data('2019-03-04','foo');
$cal_data = $this->get_calendar_data($year,$month);
//print_r($cal_data);
return $this->calendar->generate($year,$month,$cal_data);
}
application / views / calendar_view.php
这是视图文件
<script src= base_url()."/assets/jquery/jquery-3.3.1.min.js"></script>
$(document).ready(function(){
$('.calendar .day').click(function(){
day_num = $(this).find('.day_num').html();
//alert(day_num);
day_data = prompt('Enter Stuff',$(this).find('.content').html());
if(day_data !=null){
$.ajax({
url:window.location,
type:'POST',
data:{
day:day_num,
data:day_data
},
success:function(msg){
location.reload();
}
});
}
});
});
.calendar{
font-family:Arial;
font-size:12px;
}
table.calendar{
margin:auto;
border-collapse:collapse;
}
.calendar .days td{
width:50px;
height:50px;
border:2px solid black;
padding:4px;
vertical-align:top;
background-color:#DEF;
}
.calendar .days td:hover{
background-color:#FFF;
}
.calendar .weekdays td{
border:2px solid black;
padding:2px;
text-align:center;
background-color : #C0C0C0;
}
.calendar .heading th{
text-transform:uppercase;
font-size:16px;
padding:10px;
}
.calendar .highlight{
font-weight:bold;
color:blue;
}
.calendar .content{
float: left;
display: inline-block;
width: 40px;
background-color: gray;
}
<div id="month1">
<?=$month1; ?>
</div>
<div id="month2">
<?=$month2; ?>
</div>
<div id="month3">
<?=$month3; ?>
</div>