2014-04-08 3 views
0

Codeigniter 프레임 워크에서 ActiveRecord 시스템으로 json 문자열을 출력하려고합니다. 내 JSON 문자열에 대한 올바른 구문은해야ActiveRecord는 3 개의 테이블을 가진 배열 안에 배열을 가져옵니다.

{ 
"data": [ 
    [ 
     { 
      "name": "xxxx", 
      "city": "xxx", 
      "address": "xxx", 
      "image": "xxx", 
      "marketId": "1", 
      "products": [ 
       "Id": "36", 
       "productId": "36", 
       "price": "120", 
       "discounts": "1", 
       "title": "xxx", 
       "category": "2", 
       "weight": "12.5", 
       "code": "EA123", 
       "isUnitized": "0", 
       "description": "xxxx", 
       "changed": "2014-04-08 15:09:16", 
       "units": "xxx" 
      ] 
     } 
    ] 
] 

에주의를 "제품"배열. 제품 배열이 배열처럼 만으로 보여주는 일 아닌가요 것을

{ 
"data": [ 
    [ 
     { 
      "name": "xxx", 
      "city": "xxx", 
      "address": "xx x", 
      "image": "xxx", 
      "marketId": "1", 
      "Id": "36", 
      "productId": "36", 
      "price": "120", 
      "discounts": "1", 
      "title": "xxx", 
      "category": "2", 
      "weight": "12.5", 
      "code": "EA123", 
      "isUnitized": "0", 
      "description": "xxx", 
      "changed": "2014-04-08 15:09:16", 
      "units": "xxx" 
     } 
    ] 
] 

당신은 볼 수 있습니다

하지만 난 코드에서지고있어 캐릭터를 잘 작성하지, 여기에 잘못된 문자열 메인 어레이 내부의 정규 문자열.

$this->db->select('*'); 
$this->db->from('markets'); 
$this->db->where("markets.marketId", $marketId); 
$this->db->join('linkedPrices', 'linkedPrices.marketId = markets.marketId'); 
$this->db->join('products', 'products.Id = linkedPrices.productId'); 
$this->db->order_by("linkedPrices.price", "DESC"); 
$output[] = $this->db->get()->result(); 

그래서 당신은 테이블 사이에 조인을 여기에서 볼 수 있습니다 여기에

내가 내장 한 코드입니다. 목표는 json 문자열의 맨 위 예제에서 볼 수 있듯이 제품 배열을 시장 배열 내에 개별 배열로 표시하는 것입니다.

답변

0

당신이 거 제품의 배열을이 방법은이 관련 제품 데이터

$result=new stdClass(); 
$this->db->select('*'); 
$this->db->from('markets'); 
$this->db->where("markets.marketId", $marketId); 
$this->db->join('linkedPrices', 'linkedPrices.marketId = markets.marketId'); 
$this->db->order_by("linkedPrices.price", "DESC"); 
$result = $this->db->get()->result(); 

foreach($result as $r){ 
$result->products=$this->db->select('*') 
     ->from('products') 
     ->where('id',$r->productId) 
     ->get() 
     ->result(); 
} 
$output[]=json_encode($result); 
결과를 통해 루핑 및 가져 오기 수행 할 수있는 데이터 집합을 foreach는 아니오하지
관련 문제