gpt4 book ai didi

javascript - 将 json 数据对象从 php 传递到 javascript

转载 作者:行者123 更新时间:2023-11-29 07:39:08 26 4
gpt4 key购买 nike

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

我正在尝试从数据库中获取两个一维 json 数据对象数组(?)并将它们合并为一个二维 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 脚本中访问它:

<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>

最佳答案

我觉得很简单。在 html 文件中(扩展名为 .php):

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

关于javascript - 将 json 数据对象从 php 传递到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47319231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com