[英]Passing php array for use in javascript using json_encode
我知道這已經被問過很多次了,但是在重新閱讀了該主題的所有其他帖子之后,我仍然遇到問題...在我的PHP代碼和它所位於的javascript之間的某個地方,我的數組異常了。
在所附的代碼中,我對php進行了調試。 當我從javascript中切出php部分並在echo上單獨運行它時,它顯示出它正在正確構建我的json_encoded數組。
在php結束后的javascript中,我將php分配給了javascript變量,因此可以將其用於進一步處理(繪制圖形)。 放入display語句,以顯示將數組放入javascript的php調用結果的內容,顯示數組為空。
如果我剪切並粘貼php echo的輸出並將此文字分配給javascript chartData數組,則一切正常。 為什么JavaScript無法獲取php數組內容?
這是代碼片段:
<script>
...some java script stuff;
<?php
// Define the mySQL db connection
$db = new PDO('mysql:host=localhost;dbname=remets;charset=UTF-8', 'remets', 'remets', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
// Define SQL query to fetch data from mySQL
$stmt = $db->query("SELECT WeekNumber,XAxisCategory,YAxisValue FROM Metric WHERE ReportID = 'Q3' ORDER BY WeekNumber,XAxisCategory ASC");
// declarations
$amData = array();
$amArray = array();
$ctrinner = 0;
$ctrouter = -1;
$prevweek = "9999";
// Fetch data from mySQL and put it in an array in the format we need
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($prevweek !== $row['WeekNumber']) {
$ctrouter++;
$ctrinner = 0;
$amData[$ctrouter]["week"] = "".$row['WeekNumber']; // Prepending (or appending) the empty string makes the json encoding think the week number is a string, which is MUST have for AmCharts
}
$ctrinner++;
$amData[$ctrouter][$row['XAxisCategory']] = $row['YAxisValue'];
$prevweek = $row['WeekNumber'];
}
// Using json_encode puts the data into an array format that we can use in a javascript
$amJSONArray = json_encode($amData);
// Echo is for debugging only.
// echo $amJSONArray;
?>
var chartData = <?php echo $amJSONArray;
?>;
...more javascript stuff;
</script>
@Mahdi:print_r的輸出為:Array([0] => Array([week] => 1301 [Accepted] => 30 [Failed] => 5 [Passed] => 20 [Planned] => 5 [ [跳過] => 5 [未知] => 26)[1] =>數組([周] => 1302 [接受] => 25 [失敗] => 2 [通過] => 25 [計划中] => 2 [ [跳過] => 3 [未知] => 20)[2] =>數組([周] => 1303 [接受] => 26 [失敗] => 26 [通過] => 29 [計划中] => 26 [已跳過] => 26 [未知] => 10))
@Mahdi:這是php之后的jscript代碼(被注釋掉是因為我嘗試了許多其他選項,這些選項在該論壇的其他帖子以及其他文章中都被推薦-它們都不起作用。我可以運行php代碼並且可以如果我在我之前發布的php代碼片段中復制回顯的輸出,然后將其分配給chartData(即:chartData =“”;我的圖表就很好了。問題不在於圖表工具,而是數組)內容對.js文件中位於其正下方的javascript不可見,感謝您的寶貴時間。
//var chartData = "<?php print($amJSONArray); ?>"; // This just returns the literal in the speech marks
//var chartData = '<?php print($amJSONArray); ?>'; // This also returns the literal in the speech marks
//var chartData = "<?php echo($amJSONArray); ?>"; // This just returns the literal in the speech marks
//var chartData = '<?php echo($amJSONArray); ?>'; // This also returns the literal in the speech marks
//var chartData = <?php echo ($amJSONArray) ?>; // This returns empty
//var chartData = <?php echo $amJSONArray ?>; // This returns empty
//var chartData = (<?php echo $amJSONArray ?>); // This returns empty
//alert(chartData); // Returns empty - just showing the contents of the array if I do the json_encode within the php part
//alert(<?php echo $amJSONArray ?>); // Returns empty - just showing the contents of the array if I do the json_encode during the array fetch
更新:我認為我這邊有些根本上的錯誤。 我使用了一個非常簡單的示例,該示例應該在屏幕上寫出“ hello world”,但它什么也不返回。 如果我將“ write”替換為“ alert”,則在警報彈出窗口中仍不顯示任何內容。 有誰知道為什么這不起作用? 代碼是:
<?php
$testvar = "Hello World";
?>
<html>
<head>
<script type="text/javascript">
function hello()
{
// create JavaScript variable, fill it with Php variable
var testvar = "<? print $testvar; ?>";
// output to screen
document.write( testvar );
}
</script>
</head>
<!-- Call JavaScript function to display variable -->
<body onload="hello()" >
</body>
</html>
如果您能夠以字符串形式訪問數據,則可以嘗試使用內置的JSON.parse()
將其轉換為可用的javascript。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.