javascript 将数据从 PHP 带入 D3.JS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14794722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:35:36  来源:igfitidea点击:

Bringing data from PHP into D3.JS

phpjavascriptvariablesd3.js

提问by Ryan

I have this:

我有这个:

<script type="text/javascript">
var dataset = [ 5, 10, 15, 20, 25 ];

        d3.select("body").selectAll("p")
            .data(dataset)
            .enter()
            .append("p")
            .text("New paragraph!");
</script>

It works. Then I've been learning MySQL/PHP and have some data in a tiny database.

有用。然后我一直在学习 MySQL/PHP 并在一个小数据库中保存一些数据。

<?php
if ( $results ) {
foreach($results as $row) {
    echo $row->eventName . "<br>";
}
} else {
echo 'No rows found.';
}
?>

That works with the rest of it and displays some random event names like Movies, Shopping, Work.

这与它的其余部分一起工作,并显示一些随机事件名称,如电影、购物、工作。

I found this tutorialon how to bring PHP variables into Javascript but can't figure it out. To simplify instead of trying to figure out the array I even switched to just trying to figure out the data itself but couldn't even get that. This is what my last attempt was:

我找到了有关如何将 PHP 变量引入 Javascript 的教程,但无法弄清楚。为了简化而不是试图找出数组,我什至转而只是试图找出数据本身,但甚至无法得到。这是我最后一次尝试的结果:

    <?php
$php_var = 1;
?>

<script type="text/javascript">
var dataset = <?php echo $php_var; ?>

        d3.select("body").selectAll("p")
            .data(dataset)
            .enter()
            .append("p")
            .text("New paragraph!");
</script>

And then I thought maybe an array would work so tried this

然后我想也许一个数组会起作用所以尝试了这个

<?php
$php_var = array(
    5, 10, 15, 20, 25);
?>

<script type="text/javascript">
var dataset = $php_var

        d3.select("body").selectAll("p")
            .data(dataset)
            .enter()
            .append("p")
            .text("New paragraph!");
</script>

But also had no luck. Could anyone offer me some advice or point me towards a tutorial on how to get data from PHP into Javascript?

但也没有运气。谁能给我一些建议或指导我学习如何从 PHP 获取数据到 Javascript 的教程?

采纳答案by icktoofay

Try using PHP's json_encodefunction, and then embedding the data:

尝试使用PHP 的json_encodefunction,然后嵌入数据:

<?php
    $dataset = array(5, 10, 15, 20, 25);
?>
<script>
    var dataset = <?php echo json_encode($dataset); ?>;
    // ...
</script>

回答by Sugarcaen

I realize this is a little late to the party, but you can't include php on .html files and have it successfully parsed unless your server is set up to do so.

我意识到这对派对来说有点晚了,但是您不能在 .html 文件中包含 php 并成功解析它,除非您的服务器设置为这样做。

You need to add one of the following snippets to your .htaccess:

您需要将以下片段之一添加到您的 .htaccess 中:

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

or

或者

<FilesMatch "\.html$">
ForceType application/x-httpd-php
</FilesMatch>