2010-12-15 13 views
0

json_encodeを使用してPHPファイルを作成しているが、javascriptには入っていない...私は1週間以上試している...おそらく、私はそれを見ませんので、私は実際に私がしないように誰かが問題を参照してください目を2番目の目を持ってここに投稿します...Jsonエンコードの問題

ここにここで

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

は、PHPファイルの出力です... JavaScriptに1本の電話をかけるには3配列を取得json_encodeです... IDとSQLからの値が右にあります....

{"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();