2016-02-26 64 views
1

我正在從許多表中獲取數據。我想在不同的地方顯示很多對象。我從數據庫中獲取數據,但是我想將數據放入數組中以達到有用的目的,但不起作用。將數據庫值放入數組顯示codeigniter中的錯誤

這是我的控制器代碼:

public function compare_by_business_sectors() { 
    //print_r($this->input->post());exit; 
    if ($this->input->post()) 
    { 
     $solution_array = array(); 
     //print_r (json_encode($business_sectors)); exit; 
     $business_sectors=$this->home_model->compare_business_sectors_data($this->input->post()); 
     $tab_child_id = ""; 
     $id=""; 
     foreach ($business_sectors as $key=>$sectors) { 
      $solution_array[1]=$sectors->solution_name; 
      $solution_array[2]=$sectors->description; 
      $solution_array[3]=$sectors->vendor_name; 
      $solution_array[4]=$sectors->video_presentation; 
      $solution_array[5]=$sectors->start_free_trail; 
      $solution_array[6]=$sectors->hardware_package; 
      $solution_array[7]=$sectors->pos_market_rating; 

      //$solution_array[$sectors->field_id] = $sectors->value; 
      $id = "solution".$sectors->tab_child_id; 
      if ($tab_child_id != $sectors->tab_child_id) { 
       $id = array(); 
       $id[$sectors->field_id] = $sectors->title; 
      } 
      else if ($tab_child_id == $sectors->tab_child_id) { 
       $id[$sectors->field_id] = $sectors->title; 
      } 
     } 
     //$solution_array[$id]= $id; 
    } 

    print_r($id); 
    //$this->load->view('compare_by_business_sectors.php'); 
} 

這是我的模型代碼:

public function compare_business_sectors_data($sectorid) { 
    $query = $this->db->select('solutions.*,solution_tabs_child_fields.field_id,solution_tabs_child_fields.tab_child_id,solution_tabs_child_fields.title') 
      ->from('solutions') 
      //->join('solutions', 'business_sector.sector_id = solutions.business_sector_id',"left") 
      ->join('solution_features','solutions.entry_id = solution_features.entry_id',"left") 
      ->join('solution_tabs_child_fields','solution_features.field_id = solution_tabs_child_fields.field_id') 
      ->where('solutions.business_sector_id', $sectorid['id']) 
      ->get(); 
    return $query->result(); 
    //print_r($query->result());exit; 
} 
+1

你能發佈你得到的錯誤嗎? – jonhopkins

+0

你可以使用row_array();在codeigniter上的結果應該更簡單,就像你自己做的那樣。 – pietro

+0

嚴重性:警告 消息:非法偏移類型 文件名:控制器/ home.php 行號:131 – mahesh

回答

0

改變它的跟隨和嘗試。

$id_string = ""; 
    $id_array = array(); 
     foreach ($business_sectors as $key=>$sectors) { 
      $solution_array[1]=$sectors->solution_name; 
      $solution_array[2]=$sectors->description; 
      $solution_array[3]=$sectors->vendor_name; 
      $solution_array[4]=$sectors->video_presentation; 
      $solution_array[5]=$sectors->start_free_trail; 
      $solution_array[6]=$sectors->hardware_package; 
      $solution_array[7]=$sectors->pos_market_rating; 


     //$solution_array[$sectors->field_id] = $sectors->value; 
     $id_string = "solution".$sectors->tab_child_id; 
     if ($tab_child_id != $sectors->tab_child_id) { 
      $id[$sectors->field_id] = $sectors->title; 
     } 
     else if ($tab_child_id == $sectors->tab_child_id) { 
      $id[$sectors->field_id] = $sectors->title; 
     } 
    } 

print_r($id_string); 
print_r($id_array); 

首先要指定字符串值$ ID,然後,$ id將轉換爲數組只有第一個if()語句執行其他明智它不會是一個字符串。因此,爲了克服這個問題,可以在for循環之前保留$ id_array,並且可以在另一個變量中捕獲字符串。

相關問題