2013-01-05 2 views
0

데이터베이스에서 결과 배열로 데이터를 가져 와서 데이터를 Json으로 변환하고 결과를 표시하는 Codeigniter 응용 프로그램이 있습니다. 그러나 나는 디스플레이 결과로 계속 정의되지 않고있다.Jquery를 사용하여 JSON 원본 데이터베이스 결과를 표시 할 수 없습니다.

모델

$this->db->where('product_name',$sid); 
     // $this->db->select('product_title'); 
     $res = $this->db->get('products'); 
     $data = $res->result_array(); 
     return $data; 

보기 : 배열을 결과

<script language="javascript"> 
    $('#findsubmit').click(function() { 
     $.get("/products/index.php/find/lookup",{id : $('#id').val() },function(data) { 

      $('#result').html('Product name: ' + data.product_name); 
      //+ ' Last name: ' + data.lastname); 
     },"json"); 
     return false; 
}); 
</script> 

내가 방화범가 나타납니다

[{"category":"camera","product_name":"Sony HX20"},{"category":"camera","product_name":"Canon SZ220"}] 

나는이 모든 같은 간단한 배열을 만들 경우 잘 작동 :

return array('category' => 'camera','product_name' => 'Sony HX20'); 
나는 방화범받을 16,

결과 배열 : data.product_name가 정의하지만, data[0].product_namedata[1].production_name이되지 않은 : 당신의 JSON 데이터에

{"category":"camera","product_name":"Sony HX20"} 

답변

1

, 당신은 몇 가지 결과의 배열을받을 수 있습니다.

하나의 결과 (예 : 수동으로 작성한 결과) 만 반환하거나 data에서 반복해야합니다.

편집 :

var names = 'Product name: '; 
$.each(data, function(index, element) { 
    if (index > 0) {names += '; ';} 
    names += element.product_name; 
}); 
$('#result').html(names); 
+0

덕분에 지금 작동 : 여기 $.each를 사용하는 방법에 대한 샘플입니다. 데이터를 반복하는 방법에 대한 제안? for 루프 만 사용 하시겠습니까? – GuyWhoReadsStockoverflow

+0

루프 나 jQuery 함수'$ .each'를 사용할 수 있습니다. – tmuguet

+0

감사합니다. 내가 Jquery를 처음 접했을 때 어떤 식 으로든 그것을 내 코드에 추가하는 방법. – GuyWhoReadsStockoverflow

관련 문제