Search code examples
phpmysqlcodeignitercodeigniter-3

Update data from given ID to another tables with same ID CodeIgniter 3


I want to make function that update client.client_status data from given $id parameters from controller end function that fetched from booking.booking_id

Here my controller

    function end($book_id = null) {
    if (!empty($book_id)) {
        // Booking Table
        $table = 'booking';
        $where = [
            'book_id' => $book_id,
        ];
        $data = [
            'room_status' => 1,
        ];
        $this->Model_Data->booking_update($table, $where, $data);
        
        // Client Table
        $table = 'client';
        $client_id = $???????; // How to get booking.client_id from given id parameters
        $where = [
            'client_id' => $client_id,
        ];
        $data = [
            'room_status' => 1,
        ];
        $this->Model_Data->booking_update($table, $where, $data);

        $this->session->set_flashdata('book_ended', 'Book Ended');
        redirect('book');
    }
    else {
        $this->session->set_flashdata('book_end_error', 'Book End Error');
        redirect('book');
    }
}

Here my SQL Tables

enter image description here


Solution

  • According to your comment in Question , book_by_client_id is equal to client_id then for :

    Accessing a single row:

    (1) Result as an Object

    $data_result = $this->db->get_where('booking',array('book_id'=>$book_id ))->row();
    
    $book_by_client_id =$data_result->book_by_client_id;
    
    $client_id = $book_by_client_id;     // client_id 
    

    (2) Result as an Array

    $data_result = $this->db->get_where('booking',array('book_id'=>$book_id ))->row_array();
    
    $book_by_client_id =$data_result['book_by_client_id'];
    
    $client_id = $book_by_client_id;     // client_id 
    

    Note : for more info about row(); && row_array();

    https://codeigniter.com/userguide3/database/results.html#result-rows