user4419336
user4419336

Reputation:

Date not been inserted to database in codeigniter

I am trying to insert a date in to my database. But it does not get inserted only the data does.

I am using codeigniter date helper http://www.codeigniter.com/user_guide/helpers/date_helper.html and the nice_date function

Date helper autoloaded.

My add_calendar_data function for some reason just will not insert the $good_date to the date column.

Testing date by echo data below

$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

Question Why does the $good_date not get inserted to the database? And how could I solve this problem?

Update:

For some reason even though I can get the year and month on the index function and echo it fine. Will not let me pass the year and month to another through to another function

Controller

<?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

<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>

Model

<?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);
    }
}

Upvotes: 0

Views: 809

Answers (3)

user4419336
user4419336

Reputation:

Solved

After working on it for a few hours I had found out why it was not getting the uri segments correct.

When I checked fire bug the form script was not loading correct url. And was missing the uri segments.

I can now insert the correct date.

url: 'dashboard/calendar',

Now changed to

url: window.location,

Full script

<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>

Controller

<?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);
    }
} 

Upvotes: 0

Ilanus
Ilanus

Reputation: 6928

It seems like you are trying to work with a function variable which is null try

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);
    }

} 

Upvotes: 1

Dray
Dray

Reputation: 887

change your date type to TEXT in your table and try again.

Upvotes: 0

Related Questions