2014-07-23 65 views
0

我對編程世界相當陌生,所以請儘管對於我過時的SQL連接有憐憫,儘管wpdp類確實存在。我知道這一點,但由於某些原因,它不工作,這是另一個話題...無法使用json表格顯示谷歌圖表

我嘗試寫我自己的插件連接到我的sql數據庫,查詢一些東西,然後提供一個谷歌圖表與jsontable。代碼有效如果我在一個獨立的.php文件中使用它。但是,只要我激活do_action,什麼都不起作用,我的整個頁面都是白色的。

<?php 
/* 
Plugin Name: mm_gchart 
Description: - 
Version: 1.0.0 
Author: MM 
*/ 

function mm_gct_data() { 
$con=mysqli_connect("localhost","xx","xx","xx"); 
// Check connection 
if (mysqli_connect_errno()) { 
//echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
//echo "Hello"; 
$sql="SELECT gewicht, wdh FROM testtable"; 
$query=mysqli_query($con, $sql); 
$rows=array(); 
$table=array(); 
$table['cols'] = array(
    array('label' => 'Gewicht', 'type' => 'number'), 
    array('label' => 'Wiederholungen', 'type' => 'number') 
); 
while ($r = mysqli_fetch_assoc($query)) 
{ 
$temp=array(); 
$temp[]=array('v' => (int) $r['gewicht']); 
$temp[]=array('v' => (int) $r['wdh']); 
$rows[]=array('c' => $temp); 
} 
global $jsonTable; 
$table['rows'] = $rows; 
$jsonTable = json_encode($table); 
} 

add_action('init', 'mm_gct_data'); 
//do_action('init','mm_gct_data'); 
?> 

我會非常高興,如果有人可以在我的代碼上工作一些黑魔法,然後提供一個簡單的描述。

在此先感謝!

回覆後編輯:

非常感謝您的快速回復!如果do_action()命令不是我空白頁面的原因,那麼是什麼?

<html> 
    <head> 
    <!--Load the AJAX API--> 
    <script type="text/javascript" src="https://www.google.com/jsapi"></script> 
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script type="text/javascript"> 
    // Load the Visualization API and the piechart package. 
    google.load('visualization', '1', {'packages':['corechart']}); 
    // Set a callback to run when the Google Visualization API is loaded. 
    google.setOnLoadCallback(drawChart); 
    function drawChart() { 
    // Create our data table out of JSON data loaded from server. 
     var data = new google.visualization.DataTable(<?=$jsonTable?>); 
     var options = { 
     title: 'Testing', 
     width: 600, 
     height: 400 
     }; 
     // Instantiate and draw our chart, passing in some options. 
     var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 
     chart.draw(data, options); 
    } 
    </script> 
    </head> 
    <body> 
    <!--Div that will hold the pie chart--> 
    <div id="chart_div"></div> 

    <p>Everything went well..</p> 
    </body> 
</html> 
+0

更新代碼到'

<?=「如果你看到這一行,那麼short_open_tag已經被啓用。如果沒有,你需要啓用它」?>'來測試。 – stealthyninja

回答

0

,你在你的代碼有它你不應該做出do_action()通話。這個動作鉤子內置於WordPress中,並將作爲其運行的一部分自動調用。 WordPress網站在其網站上有一個Actions Run During a Typical Request的列表。

當您想要爲其他開發人員實現自定義操作掛鉤時,可以調用do_action()。致電add_action()可以添加事件到你的鉤子。

+0

要添加到@ doublesharp的答案,這裏是一個你可以運行'add_action()'的鉤子列表。 [插件API掛鉤](http://codex.wordpress.org/Plugin_API/Hooks_2.0.x)。 –