1

I want to pass an array in codeigniter query. Below is my code.

Controller

 foreach($array as $values) {
    $array_values = $values['download_subcategory_name'];
    }
$this->data['get_downloads_content'] = $this->my_model->get_downloads_content($array_values );

Model

public function get_downloads_content($array_values ){
        $this->db->select('*');
        $this->db->from('my_table');
        $this->db->where_in('download_subcategory_name', $array_values );
        $this->db->order_by('download_id', 'ASC');
        $query = $this->db->get();
        return $query->result();
    }

My problem is query showing results for only last value of array instead of all array values. Please help.

1 Answer 1

1

Just need little changes within foreach

$array_values = array();
foreach($array as $values) {
    $array_values[] = $values['download_subcategory_name'];
                 ^^^
}

OR

$array_values = array();
foreach($array as $key => $values) {
    $array_values[$key] = $values['download_subcategory_name'];
                 ^^^^^^
}

Within your code it's storing only the last value from the foreach loop thats why it only gets the single value stored within it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.