我想插入一個日期到我的數據庫中。但它不會只插入數據。日期沒有被插入到codeigniter中的數據庫
我使用笨日期幫手http://www.codeigniter.com/user_guide/helpers/date_helper.html和nice_date功能
日期幫手自動加載。
我add_calendar_data功能由於某種原因,只是將不插入$good_date
的日期列。通過回波數據
測試日期下面
$bad_date = '1'.'-'.$calendar_dates['month'].'-'.$calendar_dates['year'];
$good_date = nice_date($bad_date, 'Y-d-m');
echo $good_date;
這顯示2016年1月1日
問題爲什麼
$good_date
不會被插入到數據庫?我怎麼能解決這個問題?
更新:
對於即使我能得到的年份和月份的指數函數和回聲它精細一些原因。不會讓我過過的年份和月份到另一個另一個函數
控制器
<?php
class Calendar extends MX_Controller {
public function index($calendar_dates = null) {
$this->load->model('dashboard/model_calendar');
$bad_date = '1'.'-'.$calendar_dates['month'].'-'.$calendar_dates['year'];
$good_date = nice_date($bad_date, 'Y-d-m');
echo $good_date;
// And that displays 2016-01-01
if ($this->input->post('day')) {
$bad_date = $this->input->post('day').'-'.$calendar_dates['month'].'-'.$calendar_dates['year'];
$this->add_calendar_data($bad_date);
}
$data['calendar'] = $this->model_calendar->generate($calendar_dates['year'], $calendar_dates['month']);
$this->load->view('dashboard/calender_view', $data);
}
public function add_calendar_data($bad_date) {
$good_date = nice_date($bad_date, 'Y-d-m');
$calendar_data = array(
'date'=> $good_date,
'data' => $this->input->post('data')
);
$this->db->insert($this->db->dbprefix . 'calendar', $calendar_data);
}
}
腳本
<script type="text/javascript">
$(document).ready(function() {
$('.calendar .day').click(function() {
day_num = $(this).find('.day_num').html();
day_data = prompt('Enter Stuff', $(this).find('.content').html());
if (day_data != null) {
$.ajax({
url: 'dashboard/calendar',
type: 'POST',
data: {
day: day_num,
data: day_data
},
success: function(msg) {
location.reload();
}
});
}
});
});
</script>
型號
<?php
class Model_calendar extends CI_Model {
var $prefs;
public function __construct() {
parent::__construct();
$this->prefs = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url('common/dashboard')
);
$this->prefs['template'] = '
{table_open}<table border="0" cellpadding="0" cellspacing="0" class="table 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>{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_num">{day}<span class="label label-danger" style="margin-left: 10px;">Notice</span></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} <span class="label label-success">Current Day</span></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>{/table_close}
';
}
public function delete() {
$this->db->where('data', " ");
$this->db->delete($this->db->dbprefix . 'calendar');
}
public function get_calendar_data($year, $month) {
$this->db->select('date, data');
$this->db->from($this->db->dbprefix . 'calendar');
$this->db->like('date', "$year-$month", 'after');
$query = $this->db->get();
$cel_data = array();
foreach ($query->result() as $row) {
$cel_data[substr($row->date, 8,2)] = $row->data;
}
return $cel_data;
}
public function generate($year, $month) {
$cel_data = $this->get_calendar_data($year, $month);
$this->load->library('calendar', $this->prefs);
return $this->calendar->generate($year, $month, $cel_data);
}
}
什麼是$這個 - >輸入 - >後( '數據') ?你可以添加你的jQuery代碼$ this-> db-> insert($ this-> db-> dbprefix。'calendar',$ calendar_data);錯誤嘗試$ this-> db-> insert($ this-> db-> dbprefix('calendar'),$ calendar_data); –
獲取我在日曆中輸入的任何消息的$ this-> input-> post('data')。和$ this-> input-> post('day')獲取選定的日期。 – user4419336