2015-12-15 70 views
0

我想插入一個日期到我的數據庫中。但它不會只插入數據。日期沒有被插入到codeigniter中的數據庫

我使用笨日期幫手http://www.codeigniter.com/user_guide/helpers/date_helper.htmlnice_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}&nbsp;{/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); 
    } 
} 
+0

什麼是$這個 - >輸入 - >後( '數據') ?你可以添加你的jQuery代碼$ this-> db-> insert($ this-> db-> dbprefix。'calendar',$ calendar_data);錯誤嘗試$ this-> db-> insert($ this-> db-> dbprefix('calendar'),$ calendar_data); –

+0

獲取我在日曆中輸入的任何消息的$ this-> input-> post('data')。和$ this-> input-> post('day')獲取選定的日期。 – user4419336

回答

1

好像你正試圖使用null函數變量試試

class Calendar extends MX_Controller { 

    public function index($calendar_dates = null) { 
     $this->load->library('session'); 
     $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; 
     $this->session->set_flashdata('good_date', $good_date); //Store the variable 

     // 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() { 

     $calendar_data = array(
      'date'=> $this->session->flashdata('good_date'), 
      'data' => $this->input->post('data') 
     ); 

     $this->db->insert($this->db->dbprefix . 'calendar', $calendar_data); 
    } 

} 
+0

這$ this-> input-> post('day')只得到選定的一天不會給出月份或年份的一天。 – user4419336

+0

@ wolfgang1983它從jQuery post中獲取day_num。你還試圖插入什麼? –

+0

會在之前給它。但發現了一些原因,即使我可以得到索引函數的年份和月份並且回顯它很好。不會讓我把年份和月份傳遞給另一個函數 – user4419336

0

將您的日期類型更改爲TEXT並重試。

+0

無效的日期顯示 – user4419336

+0

嘗試使用它鍵入TEXT – Dray

+0

無效的日期仍然 – user4419336

0

解決

它的工作了幾個小時後,我已經發現了它爲什麼沒有得到的URI段正確。

當我檢查了火蟲時,表單腳本沒有加載正確的網址。並錯過了uri細分市場。

我現在可以插入正確的日期。

url: 'dashboard/calendar', 

現在改爲

url: window.location, 

完整劇本

<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: window.location, 
       type: 'POST', 
       data: { 
        day: day_num, 
        data: day_data 
       }, 
       success: function(msg) { 
        location.reload(); 
       }      
      }); 

     } 
    }); 

}); 

</script> 

控制器

<?php 

class Calendar extends MX_Controller { 

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

    public function index() { 
     $this->load->model('dashboard/model_calendar'); 

     if ($this->input->post('day')) { 

      $calendar = array(
       'date' =>$this->uri->segment(3) .'-'. $this->uri->segment(4) .'-'. $this->input->post('day'), 
       'data' => $this->input->post('data') 
      ); 

      $this->db->insert('calendar', $calendar); 

     } 

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

     $this->load->view('dashboard/calender_view', $data); 
    } 
}