2017-06-12 4 views
0

に3つのフィールドを返す方法を次のよう:私はアヤックスこの情報の検索を持っている私のHTMLで返すPHPで私がサービスを持っているJSONと人気チャート

[[1,"16846"],[2,"16858"],[3,"16923"],[4,"16891"]] 

$.ajax({ 
     type: 'POST', 
     url: 'getDadosGrafico.php', 
     dataType: 'json', // use this 
     success: function(data) { 
      var dados = data; 

グラフには3つのレコードが必要ですが、すでにデータベースに3つの列があります。 どのように私はアヤックスはあなたがそれぞれ、3つのプロパティを持つオブジェクトを返す必要が3列

enter image description here

私のコードのPHP

$stmt = $con->prepare($sql); 
    $stmt->execute(); 
    $users = $stmt->fetchAll(); 
    $cont=1; 
    $dataset1 = array(); 
    foreach ($users as $row) { 
     $data = $row['data_reenvio']; 
     $data_formatada=date('d/m/Y',strtotime($data)); 

    $dataset1[] = array($cont , $row['total_sucesso'], $row['total_falha'], $row['total']); 
    $cont++; 

    } 
    print (json_encode($dataset1)); 

マイコードHTML

$.ajax({ 
    type: 'POST', 
    url: 'getDadosGrafico.php', 
    dataType: 'json', // use this 
    success: function(data) { 
     var dados = data; 

     // Graph Data ############################################## 
     var graphData = [{ 
      // Visits 
      data: dados, //[ [6, 1300], [7, 1600], [8, 1900], [9, 2100], [10, 2500], [11, 2200], [12, 2000], [13, 1950], [14, 1900], [15, 2000] ], 
      color: '#71c73e' 
     }, { 
      // Returning Visits 
      data: [ [1, 15500], [2, 16000], [3, 16550], [4, 16000], [5, 16800], [6, 16900], [7, 16800], [8, 16850], [9, 16830], [10, 17000] ], 
      color: '#77b7c5', 
      points: { 
       radius: 4, 
       fillColor: '#77b7c5' 
      } 
     }, { 
      // Returning Visits 
      data: [ [1, 17500], [2, 17000], [3, 17550], [4, 18000], [5, 18800], [6, 18900], [7, 18800], [8, 18850], [9, 18830], [10, 17000] ], 
      color: '#77b7c5', 
      points: { 
       radius: 4, 
       fillColor: '#0007c5' 
      } 
     } 
     ]; 

答えて

0

を識別することができます1つは配列の配列を持ち、次のようになります。

{ 
    visits: [ [6, 1300], [7, 1600], [8, 1900], [9, 2100], [10, 2500], [11, 2200], [12, 2000], [13, 1950], [14, 1900], [15, 2000] ], 
    returning_visitors: [ [1, 15500], [2, 16000], [3, 16550], [4, 16000], [5, 16800], [6, 16900], [7, 16800], [8, 16850], [9, 16830], [10, 17000] ], 
    extra: [ [1, 17500], [2, 17000], [3, 17550], [4, 18000], [5, 18800], [6, 18900], [7, 18800], [8, 18850], [9, 18830], [10, 17000] ] 
} 

そして、あなたはこのように、あなたのコードでこれらのオブジェクトの値を使用します:あなたのPHPコードで

 var graphData = [{ 
      // Visits 
      data: dados.visits 
      color: '#71c73e' 
     }, { 
      // Returning Visits 
      data: dados.returning_visitors, 
      color: '#77b7c5', 
      points: { 
       radius: 4, 
       fillColor: '#77b7c5' 
      } 
     }, { 
      // Returning Visits 
      data: dados.extra, 
      color: '#77b7c5', 
      points: { 
       radius: 4, 
       fillColor: '#0007c5' 
      } 
     } 
     ]; 

、私はわからないんだけど、私はそれがこのようなものになるだろうと思います。

$stmt = $con->prepare($sql); 
    $stmt->execute(); 
    $users = $stmt->fetchAll(); 
    $cont=1; 

    $datasetTotalSucesso = array(); 
    $datasetTotalFalha = array(); 
    $datasetTotal = array(); 

    foreach ($users as $row) { 
    $data = $row['data_reenvio']; 
    $data_formatada=date('d/m/Y',strtotime($data)); 

    $datasetdatasetTotalSucesso[] = array($cont , $row['total_sucesso']); 
    $datasetTotalFalha[] = array($cont , $row['total_falha']); 
    $datasetTotal[] = array($cont , $row['total']); 

    $cont++; 

    } 
    $obj = array('visits' => $datasetdatasetTotalSucesso, 'returning_visitors' => $datasetTotalFalha, 'extra' => $datasetTotal); 
    print (json_encode($obj)); 
+0

PHPでこれを行うには – Will

+0

私は自分の答えをPHPの答えと考えて編集しました。 –

+0

パーフェクト!ありがとうございました!!! – Will

関連する問題