Search code examples
codeigniter-2associative-arrayquery-builder

tricky associate array insertion in codeigniter


My table looks like this

+---------+-------------+----------+
|  day    | start_time  | end_time |
+---------+-------------+----------+
|  monday | 10:00:00    | 12:00:00 |
+---------+-------------+----------+

The associate array i am getting from form is looking like this

  [timeslot] => Array
        (
            [1] => Array
                (
                    [weekday] => 1
                    [from] => Array
                        (
                            [0] => 1:15 PM
                            [1] => 4:15 PM
                        )

                    [to] => Array
                        (
                            [0] => 2:15 PM
                            [1] => 5:15 PM
                        )

                )

            [2] => Array
                (
                    [weekday] => 2
                    [from] => Array
                        (
                            [0] => 
                        )

                    [to] => Array
                        (
                            [0] => 
                        )

                )

            [3] => Array
                (
                    [weekday] => 3
                    [from] => Array
                        (
                            [0] => 
                        )

                    [to] => Array
                        (
                            [0] => 
                        )

                )

            [4] => Array
                (
                    [weekday] => 4
                    [from] => Array
                        (
                            [0] => 
                        )

                    [to] => Array
                        (
                            [0] => 
                        )

                )

            [5] => Array
                (
                    [weekday] => 5
                    [from] => Array
                        (
                            [0] => 
                        )

                    [to] => Array
                        (
                            [0] => 
                        )

                )

            [6] => Array
                (
                    [weekday] => 6
                    [from] => Array
                        (
                            [0] => 
                        )

                    [to] => Array
                        (
                            [0] => 
                        )

                )

            [7] => Array
                (
                    [weekday] => 7
                    [from] => Array
                        (
                            [0] => 1:15 PM
                        )

                    [to] => Array
                        (
                            [0] => 3:15 PM
                        )

                )

        )

normal array insert in Codeigniter doesn't work for this. any work around for this.

I need to store this like

+---------+-------------+----------+
|  day    | start_time  | end_time |
+---------+-------------+----------+
|  monday | 12:30:00    | 01:30:00 |
|  monday | 04:30:00    | 06:30:00 |
+---------+-------------+----------+

I tried to insert it using

$this->db->insert('time_slots', $data);

but this is failing of the associate format i have.

I also thought to loop through my array if from and to are arrays

and insert the data using the above command but i doubt a performance issue.


Solution

  • Make array like this and pass to model

      $weekDay = array('1'=>'Sunday','2'=>'Monday','3'=>'Tuesday','4'=>'Wednesday','5'=>'Thurday','6'=>'Friday','7'=>'Saturday');
        foreach($_POST['timeslot'] as $key=>$val){
            foreach($val['from'] as $k=>$v){
                $day = $val['weekday'];
                $data[] = array(
                        'day'=>$weekDay[$day],
                        'start_time'=>$v['from'],
                        'end_time'=>$val['to'][$k]
                );
            }
        }
    
    
    $this->db->insert_batch('time_slots', $data);