Reputation: 59
I have an id array in my controller that has checkbox input in a form in views. $toppings = $this->input->get('topping');
Array
(
[0] => 1
[1] => 3
[2] => 4
)
I'm trying to get the information relevant to each id in the array from the database.
$toppings = $this->input->get('topping');
foreach ($toppings as $topping ) {
$id = $topping;
$toppinglist = $this->toppingmodel->find_topping($id);
echo'<pre>'; print_r($toppinglist); die();
}
Model Class -
function find_topping($id)
{
$query = $this->db->get_where('Topping', array('id' => $id));
return $query->row_array(); }
}
The output array i'm getting is only the data for the first id
Array
(
[id] => 1
[slug] => mushroom
[toppingName] => Mushrooms
[price] => 50.00
)
How can i get all the data relevant to each id in an array. (nested array) Thank you.
Upvotes: 0
Views: 2099
Reputation: 1995
Controller:-
$toppings = $this->input->post('topping'); //Array
$toppinglist = $this->toppingmodel->find_topping($toppings);
model:-
function find_topping($idArr = array()){ //Array
$query = $this->db->where_in("Topping", $idArr)->get("tablename");
return $query->result_array();
}
Note:-
$idArr = array()
means
make the function have a default value of array otherwise if something else is passed you'll get an error in your query.
Upvotes: 1
Reputation: 1617
You need to make your SQL query with WHERE IN().
In Codeignitor you can use $this->db->where_in($tableName, $arrayOfVales). Learn more here
Code Example
function find_topping(array $ids)
{
$query = $this->db->where_in('Topping', $ids);
return $query->result_array();
}
Upvotes: 0