将jason数据对象从php传递给javascript

时间:2017-11-15 23:45:55

标签: javascript php mysql

我已经被问了很多次,并且我已经阅读了很多关于它的线索,但是简单的无法弄清楚如何将json数据pbject从php传递给java脚本。

我正在尝试从数据库中获取两个单维json数据对象数组(?)并将它们合并到一个两个dinemsional json数据对象数组中,然后进一步传递给sigma.js进行进一步处理。

从数据库中读取数据:

<?php
require 'sort.php';
$db = db();
$rawNodes = $db->select('node', '*', ['ico_id' => $_GET['id']]);
$rawEdges = $db->select("edge", "*", ["ico_id" => $_GET['id']]);

$nodes = array();
foreach ($rawNodes as $node) {
    array_push($out, $node);
}

$edges = array();
foreach ($rawEdges as $edge) {
    array_push($out, $edge);
}

?>

现在用Java Script访问它:

<html>
    <head>
        <title>Dynamic Transaction Visualization </title>
        <style type="text/css">
            #container {
                max-width: 1200px;
                height: 800px;
                margin: auto;
            }
        </style>
        <script src="build/sigma.min.js"></script>
        <script src="build/plugins/sigma.parsers.json.min.js"></script>
        <script src="build/plugins/sigma.renderers.edgeLabels.min.js"></script>
        <script>
            function getData () {
                var gnodes = <?php echo json_encode($nodes); ?>;
                var gedges = <?php echo json_encode($edges); ?>;

                alert(gnodes[0].adress); // <- Doesnt work? Why?

                var g = {nodes: [], edges: []}; //<- i think this sytanx is correct for making a 2 dim json data object array- is it?

                var xpos = 0;
                var ypos = 0;

                var n;

                for (n in gnodes) { //<- Does not iterate through any Elements
                    alert(gnodes[n].adress); //<- Doest work either? Why?
                    xpos = n.block_number - 4400000;
                    ypos += 100;
                    if (ypos > 10000) { ypos = 0 }
                    g.nodes.push(
                        {
                            "id": n.adress,
                            "label": n.adress,
                            "size": 1, //n.size,
                            "x": xpos,
                            "y": ypos
                        }
                    )
                }

                var edgecount = 0;

                var m;
                for (m in gedges) {
                    g.edges.push(
                        {
                            "id": edgecount++,
                            "source": m.node_adress1,
                            "target": m.node_adress2,
                            "label": m.erc20_value,
                            "type": "arrow"
                        }
                    )
                }
                //g.nodes = gnodes; geht nicht benennung nicht passend
                //g.edges = gedges;

                return g;
            }

            function dispGraph() {
                var gdata = getData();
                s = new sigma({
                    graph: gdata,
                    container: 'container',
                    renderer: {
                        container: "container",
                        type: "canvas"
                    },
                    settings: {
                        edgeLabelSize: 'fixed',
                        nodeLabelSize: 'fixed',
                        defaultNodeColor: '#ec5148',
                        maxNodeSize: 15,
                        minNodeSize: 5,
                        minEdgeSize: 4,
                        maxEdgeSize: 4,
                        minArrowSize: 4,
                        //edgeLabelSize: 'fixed',
                        // {string} The opposite power ratio between the font size of the label and
                        // the edge size:
                        // Math.pow(size, -1 / edgeLabelSizePowRatio) * size * defaultEdgeLabelSize
                        edgeLabelSizePowRatio: 1,
                        // {number} The minimum size an edge must have to see its label displayed.
                        edgeLabelThreshold: 1,
                    }
                });
            }
        </script>
    </head>
    <body>
    <div id="container"></div>
        <script>dispGraph()</script>
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

我认为这很容易。在html文件中(扩展名为.php):

<?php $json = json_encode($data); ?>
<script>
    var json = <?=$json?>;
</script>

答案 1 :(得分:0)

Paul Zheng帮助我做了以下纠正

1)

Change $out to $nodes in array_push()

2)

<?php $json = json_encode($data); ?>
<script>
    var json = <?=$json?>;
</script>

感谢大家的贡献!