1

I have two arrays from my view.I need to insert their data corresponding to each other.

Below is the My view

<?php foreach ($seats as $seat):?>

        <tr>
            <td><input type="checkbox" name="seat_id[]" value=<?php echo $seat->seatNumber;?>></td>
            <td><?php echo $seat->seatLabel;?></td>
            <td><input type="hidden" name="seat_label[]" value="<?php echo $seat->seatLabel;?>"></td>
        </tr>
        <?php endforeach;?>

From above view seat_id and seat_label carries values that have to be stored in database

This is my controller

if($this->form_validation->run()){
            $seat_ids = $this->input->post("seat_id[]");
            $seat_labels = $this->input->post("seat_label[]");
            $busNumber = $this->input->post("busNumber");
            $bookingDate = $this->input->post("bookingDate");
            $reportingTime = $this->input->post("reportingTime");
            $departureTime = $this->input->post("departureTime");
            $this->load->model('Queries');
     $insert = $this->Queries->saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime);

This is My model

public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime){

    foreach($seat_ids as $seat_id )
          foreach($seat_labels as $seat_label ){
    {
        $record = array(
        'seatNumber' => $seat_id, 
        'seatLabel'  => $seat_label,
        'bookingDate' => $bookingDate,
        'reportingTime' => $reportingTime,
        'departureTime' => $departureTime, 
        'busNumber' => $busNumber,
        'seatUse' => 'Enabled',
        'seatStatus' => 'Available',);
        $this->db->insert('schedule', $record);
    }
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Coeng
  • 59
  • 15

5 Answers5

1

Hope this will help you :

Use insert_batch instead of insert like this :

public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime)
{

    foreach($seat_ids as $key => $seat_id )
    {
            $record[] = array(
            'seatNumber' => $seat_id, 
            'seatLabel'  => $seat_labels[$key],
            'bookingDate' => $bookingDate,
            'reportingTime' => $reportingTime,
            'departureTime' => $departureTime, 
            'busNumber' => $busNumber,
            'seatUse' => 'Enabled',
            'seatStatus' => 'Available');
    }
    $this->db->insert_batch('schedule', $record);
}

For more :https://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data

Pradeep
  • 9,667
  • 13
  • 27
  • 34
0

your code should read

 foreach($seat_ids as $seat_id ){
     foreach($seat_labels as $seat_label ){
        $record = array(
        'seatNumber' => $seat_id, 
        'seatLabel'  => $seat_label,
        'bookingDate' => $bookingDate,
        'reportingTime' => $reportingTime,
        'departureTime' => $departureTime, 
        'busNumber' => $busNumber,
        'seatUse' => 'Enabled',
        'seatStatus' => 'Available',);
        $this->db->insert('schedule', $record);
   }
}
Clint
  • 973
  • 7
  • 18
0

if your the keys from $seat_id and $seat_label match you can do it with only one foreach like this:

foreach($seat_ids as $seat_key => $seat_id ) {
    $record = array(
    'seatNumber' => $seat_id, 
    'seatLabel'  => $seat_label[$seat_key],
    'bookingDate' => $bookingDate,
    'reportingTime' => $reportingTime,
    'departureTime' => $departureTime, 
    'busNumber' => $busNumber,
    'seatUse' => 'Enabled',
    'seatStatus' => 'Available',);
    $this->db->insert('schedule', $record);
}
0

Considering that you are using JSON to submit the form data and order among array is maintained.

You may try :

$i = 0;
foreach($seat_ids as $seat_id )
{
    $record = array(
    'seatNumber' => $seat_id, 
    'seatLabel'  => $seat_labels[$i],
    'bookingDate' => $bookingDate,
    'reportingTime' => $reportingTime,
    'departureTime' => $departureTime, 
    'busNumber' => $busNumber,
    'seatUse' => 'Enabled',
    'seatStatus' => 'Available',);
    $this->db->insert('schedule', $record);
    $i++;
}
Tarun Kumar
  • 201
  • 1
  • 4
-2

Currently the form does not have a seat-label relation. if you do not tick all ids, then the labels after that will correspond to the wrong id (i.e. the ids' posted array is smaller than the labels' array)

Hence you will have to introduce the necessary relation into the form names:

<input type="checkbox" name="seat[{index}][id]" value="{id}">
<input type="hidden" name="seat[{index}][label]" value="{label}">

If that submits, you can now check if both boxes were ticked

// using $_POST for simplicity
$seats = array_filter($_POST['seat'], function (array $row) {
    return count($row) === 2;
});

Now you have an array with corresponding ids/labels where only one loop is needed.

EDIT: overlooked that the second input is a hidden field, but that didn't affect the problem at all.

Dormilich
  • 927
  • 5
  • 11