2017-03-13 159 views
0

誰能幫助我如何在PHP運行此代碼。我複製谷歌圖碼再加入while條件,以顯示我的查詢。但運行此代碼時不會發生任何情況。谷歌圖表

<?php 
    include("includes/mssqlconn.php"); 
?> 

<html> 
    <head> 
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
    <script type="text/javascript"> 

     google.charts.load('current', {'packages':['corechart']}); 
     google.charts.setOnLoadCallback(drawChart); 

     function drawChart() { 
     var data = google.visualization.arrayToDataTable([ 
      ['AgentName', 'Transaction', 'Amount'],      

    <?php                       
     $conn->open($connStr); //Open the connection to the database 
     $conn->CommandTimeout = 1000; 

     //declare the SQL statement that will query the database      
     $query = "select top 10 a.Account 
        ,COUNT(txn) as TxnCount 
        ,sum([amountloc]) as Amount 

        from table a 

        where (Dateloc between '20170201' and '20170205') 

        group by a.Account 
        order by TxnCount desc"; 

     $rs = $conn->execute($query); 

     while (!$rs->EOF) //carry on looping through while there are records 
      { 
     ?>  
      ['<?php echo $rs->Fields["a.account"]->Value;?>', '<?php echo $rs->Fields["txncount"]->Value;?>', '<?php echo $rs->Fields["amount"]->Value;?>'], 

      ]); 

     <?php 

      $rs->MoveNext(); 
      } 
      $rs->Close(); 
      $conn->Close(); 

      $rs = null; 
      $conn = null; 
     ?> 

     var options = { 
      title: 'Performance', 
      curveType: 'function', 
      legend: { position: 'bottom' } 
     }; 

     var chart = new google.visualization.LineChart(document.getElementById('curve_chart')); 

     chart.draw(data, options); 
     } 

    </script> 
    </head> 
    <body> 
    <div id="curve_chart" style="width: 900px; height: 500px"></div> 
    </body> 
</html> 

我的連接是ado.connection,我使用MSSQL 2008.任何幫助都可以。

回答

0

,我發現我的錯誤。只需將上面的代碼替換成這個。

while (!$rs->EOF) //carry on looping through while there are records 
       { 
      ?>  
       ['<?php echo $rs->Fields["a.account"]->Value;?>', '<?php echo $rs->Fields["txncount"]->Value;?>', '<?php echo $rs->Fields["amount"]->Value;?>'], 



      <?php 
       $rs->MoveNext(); 
       } 
       ?> 
        ]); //the problem is here 
      <?php 
       $rs->Close(); 
       $conn->Close(); 

       $rs = null; 
       $conn = null; 
      ?>