2017-10-11 124 views
0

中的最後一個值,我使用笨,我只得到來自從模型來數組的最後一個值。我需要所有在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 
} 

我知道我會得到最後的值,但是否有任何其他選項,讓所有的值,並傳遞給控制器?

+1

請更新您的標題以使您所要求的內容有意義。 – andersfylling

+0

如果是我,我會忘記這一切很大程度上是多餘的PHP代碼,而是會看到:[?我爲什麼要爲了什麼,在我看來是一個非常簡單的SQL查詢提供MCVE(https://開頭元。 stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry

回答

0

,就應該替換該行:

$row1 = $fetch_query->result(); 

與此:

$row1[] = $fetch_query->result(); 

附: 我想你可以通過使用mysql INFIND_IN_SET運算符來避免foreach循環。

+0

@ brevis.What I我正在做的是我在第一個查詢中獲取輸出並檢查輸出是否可用在第二個表中 –

+0

我的意思是這樣的http://sqlfiddle.com/#!9/c962d5/1 – brevis

+0

但是如果我誤解了什麼你這樣做 - 只是不介意:) – brevis

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 
} 
+0

那麼它不是我怎麼會做:-( – Strawberry

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; 
} 

編輯爲您的完整代碼

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 
    } 
+0

A JOIN似乎是一個更優雅的解決方案 – Strawberry

+0

是的,我同意,但他只想顯示值,所以我只是工作解決方案。 –

+0

我不知道這意味着什麼,但玩得開心。 – Strawberry