2010-12-15 6 views
0

json_encode를 사용하여 한 번의 호출로 PHP 파일을 만들었지 만 javascript로 이동하지는 않습니다 ... 일주일 넘게 노력하고 있습니다 ... 아마도 내 얼굴 앞에있을 수 있습니다. 그리고 나는 그것을 보지 못한다. 그래서 나는 그것을 두 번째 세트의 눈으로 만들어서 내가 뭘 보지 않을지를 보게 될 것이다. 아무도 문제를 보지 않는다. 나는 정말로 그러지 않기 때문에 ...Json 인코딩 문제

여기에 3 어레이를 얻는로 json_encode는 자바 스크립트 1 호 ... 여기

$outarr['dayPowerP'] = $dayPowerP; 
$outarr['monthPowerP'] = $monthPowerP; 
$outarr['yearPowerP'] = $yearPowerP; 
echo json_encode($outarr); 

인 PHP 파일의 출력을 만드는 것이다 ... ID 용 및 SQL의 값은 오른쪽있다 ....

012 3,516,
{"dayPowerP":["13.2470"],"monthPowerP":["193.6810"],"yearPowerP":["989.6720"]} 

여기

$(document).ready(function() { 
$('#datepicker').datepicker({maxDate: 0, dateFormat: 'yy-mm-dd', onSelect: function(dateText) { 
      var myDate = $(this).datepicker('getDate'); 
      $('#apDiv1').html($.datepicker.formatDate('DD, d', myDate)); 
      $('#apDiv5').html($.datepicker.formatDate('MM', myDate)); 
      $('#apDiv7').html($.datepicker.formatDate('yy', myDate));   
      $.ajax({ 
      type: "POST", 
      url: "clickdates.php",     
      data: {choice: dateText}, 
      dataType: "json", 
      success: function(json_data) { 
      $('#apDiv2').html(json_data['dayPowerP']).show(); 
      $('#apDiv6').html(json_data['monthPowerP']).show();  
      $('#apDiv8').html(json_data['yearPowerP']).show(); 
      } 
     })   
    }}); 
}); 

은 정말 선택을 ... 여기에 전체 PHP 파일입니다하려면 ... 포스트와 JSON을 보여주는 아약스이다.

<?php 
$choice = (isset($_POST['choice'])) ? date("Y-m-d",strtotime($_POST['choice'])) : date("Y-m-d"); 
$con = mysql_connect("localhost","root","xxxxxx"); 
if (!$con) { die('Could not connect: ' . mysql_error()); } 
mysql_select_db("inverters", $con); 
$sql = "SELECT sum(power/1000) AS choice FROM feed WHERE date = '".$choice."' group by date"; $res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error()); 
$row = mysql_fetch_assoc($res); 
$dayPowerP = array($row['choice']); 
?> 
<?php 
$choice = (isset($_POST['choice'])) ? date("m",strtotime($_POST['choice'])) : date("m"); 
$con = mysql_connect("localhost","root","xxxxxx"); 
if (!$con) { die('Could not connect: ' . mysql_error()); } 
mysql_select_db("inverters", $con); 
$sql = "SELECT sum(power/1000) AS choice FROM feed WHERE month(date) = '".$choice."'"; 
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error()); 
$row = mysql_fetch_assoc($res); 
$monthPowerP = array($row['choice']); 
?> 
<?php 
$choice = (isset($_POST['choice'])) ? date("Y",strtotime($_POST['choice'])) : date("Y"); $con = mysql_connect("localhost","root","xxxxxx"); 
if (!$con) { die('Could not connect: ' . mysql_error()); } 
mysql_select_db("inverters", $con); 
$sql = "SELECT sum(power/1000) AS choice FROM feed WHERE year(date) = '".$choice."'"; 
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error()); 
$row = mysql_fetch_assoc($res); 
$yearPowerP = array($row['choice']); 
?> 
<?php 
$outarr['dayPowerP'] = $dayPowerP; 
$outarr['monthPowerP'] = $monthPowerP; 
$outarr['yearPowerP'] = $yearPowerP; 
echo json_encode($outarr); 
?> 

이것은 복잡한 코드가 아니기 때문에 내가보고 있거나하지 않는 사소한 것이 있어야합니다.

감사

앨런

자신의 배열의 각 요소를 넣을 필요가 없습니다

답변

0

메이트. Json_encode는 연관 배열을 지원하지 않습니다.

"json_data [ 'dayPowerP']"는 작동하지 않습니다. json_data.dayPowerP 가 작동해야합니다. 배열을 반환하는 것처럼 보입니다. 그래서 부동 소수점으로 타입 변환해야합니다. **

특히

success: function(json_data) { 
console.log(json_data) 

       $('#apDiv2').html(json_data['dayPowerP']).show(); 
       $('#apDiv6').html(json_data['monthPowerP']).show();  
       $('#apDiv8').html(json_data['yearPowerP']).show(); 
       } 

내가 옳다 경우,

**을 console.log (json_data) (콘솔 참조)

는이 결과를 게시하시기 바랍니다 수 , json_data [ 'dayPowerP']를 json_data.dayPowerP [0] 으로 바꿔야하며 모두 정상적으로 작동해야합니다.

+0

예상대로 JSON 개체로 변환하여 잘 지원하는 것처럼 보입니다. –

+0

그게 다야 !!!!! json_data.dayPowerP [0] 그 일을 ... 정말 많이 !!!!!! 크리스마스 아침 인 것 같아요 !!! 다시 한 번 감사드립니다! – hkalan2007

+0

나는 나에게 이름 붙여진 프로그래밍 표준을 갖고 싶다. – Keyo

0

; 그냥 직접 삽입하십시오.

0

다음을 사용하십시오 : $ ('# apDiv2') .html (json_data.dayPowerP [0]). show();