I want to generate a query inside of for loop, but I only get the first unique id. How can I solve this?
views
for ($i=0; $i < count($id); $i++) {
$id = $id[$i];
$row = $get_cash_card->by_id($id);
echo $row[0]['id'] . "<br/>";
}
model:
public function by_id($id)
{
$query = $query = $this->db->get_where('cash_cards_info', array('id' => $id));
$rows = $query->num_rows();
return $query->result_array();
}
controllers:
public function cash_cards_on_hand_step1()
{
$this->load->model('cash_cards');
$this->form_validation->set_rules('id', 'Cash Card', 'trim|required|xss_clean');
$data['get_cash_card'] = $this->cash_cards;
$data['id'] = $this->input->post('id');
$data['main_content'] = 'cash_cards_on_hand_step1';
$this->load->view('dashboard', $data);
}
It runs in 1 unique ID. Any ideas?
You're modifying your $id
variable inside your for
loop. Rather than do
for ($i=0; $i < count($id); $i++) {
$id = $id[$i];
$row = $get_cash_card->by_id($id);
echo $row[0]['id'] . "<br/>";
}
You need to do
for ($i=0; $i < count($id); $i++) {
$newid = $id[$i]; //Change this line
$row = $get_cash_card->by_id($newid);
echo $row[0]['id'] . "<br/>";
}
Because you are overwriting the value of $id
on the first iteration it means the $i < count($id)
part of your for
loop is no longer true so the loop stops.
Also you shouldn't be accessing your model in the view, that's something you should be doing in the controller
This may work for you
for ($i=0; $i < count($id); $i++) {
$row = $get_cash_card->by_id($id[$i]);
echo $row[0]['id'] . "<br/>";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.