我正在使用CodeIgniter,我只得到来自模型的数组中的最后一个值。我需要在foreach条件下运行的所有值。
我的控制器代码是
$inst_id = $this->session->userdata['int_url_id']['int_id'];
$result['data']=$this->formbuilder_Model->check_example_fields($inst_id);
print_r($result['data']);// Here I am getting the last value
$this->load->view('user-example-form',$result);//passing elements to the view page
模型
public function check_example_fields($inst_id)
{
$sql_elements="SELECT inst_id,form_builder_type, form_elements,form_last_modified FROM `form_builder` WHERE inst_id=$inst_id AND form_builder_type='example' ORDER BY form_last_modified DESC LIMIT 1";
$query = $this->db->query($sql_elements);// I am getting the last row
$row = $query->row();
$abc=explode(',',$row->form_elements);//I am getting output
foreach ($abc as $value) {
$sql_elements_get="SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='".$value."'";
$fetch_query = $this->db->query($sql_elements_get);
$row1 = $fetch_query->result();
}
return $row1;// It it return only last value
}
我知道我会得到最后一个值但是还有其他选项来获取所有值并传递给控制器吗?
答案 0 :(得分:0)
你应该替换这一行:
$row1 = $fetch_query->result();
用这个:
$row1[] = $fetch_query->result();
P.S。
我认为您可以通过使用mysql foreach
或IN
运算符来避免FIND_IN_SET
循环。
答案 1 :(得分:0)
你需要循环第二个查询的结果;)
public function check_example_fields($inst_id)
{
$sql_elements="SELECT inst_id,form_builder_type, form_elements,form_last_modified FROM `form_builder` WHERE inst_id=$inst_id AND form_builder_type='example' ORDER BY form_last_modified DESC LIMIT 1";
$query = $this->db->query($sql_elements);// I am getting the last row
$row = $query->row();
$abc=explode(',',$row->form_elements);//I am getting output
$results = [];
foreach ($abc as $value) {
$sql_elements_get="SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='".$value."'";
$fetch_query = $this->db->query($sql_elements_get);
foreach ($fetch_query->result() as $r)
{
$results[] = $r;
}
}
return $results;// It it return all values
}
答案 2 :(得分:0)
public function check_example_fields($inst_id)
{
$sql_elements="SELECT inst_id,form_builder_type, form_elements,form_last_modified FROM `form_builder` WHERE inst_id=$inst_id AND form_builder_type='example' ORDER BY form_last_modified DESC LIMIT 1";
$query = $this->db->query($sql_elements);// I am getting the last row
$row = $query->row();
$abc=explode(',',$row->form_elements);//I am getting output
$datanew = array();
// start your copy code here
foreach ($abc as $value)
{
$sql_elements_get="SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='".$value."'";
$fetch_query = $this->db->query($sql_elements_get);
$datanew[] = $fetch_query->result();
}
// end your copy code here
$last_row = end($datanew);
$data['alldata'] = $datanew;
$data['lastrow'] = $last_row;
return $data;
}
编辑为完整代码
答案 3 :(得分:0)
将row1用作数组并在其中分配结果。
public function check_example_fields($inst_id){
$sql_elements="SELECT inst_id,form_builder_type, form_elements,form_last_modified FROM `form_builder` WHERE inst_id=$inst_id AND form_builder_type='example' ORDER BY form_last_modified DESC LIMIT 1";
$query = $this->db->query($sql_elements);// I am getting the last row
$row = $query->row();
$row1 = array(); //define array here
$abc=explode(',',$row->form_elements);//I am getting output
foreach ($abc as $value) {
$sql_elements_get="SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='".$value."'";
$fetch_query = $this->db->query($sql_elements_get);
$row1[] = $fetch_query->result();
}
print_r($row1);
return $row1;// It it return only last value
}