Php 检查数组项
在下面的代码中,$rows是数据库中TestPhase表中的MySQL查询结果的数组值Php 检查数组项,php,javascript,mysql,loops,recursion,Php,Javascript,Mysql,Loops,Recursion,在下面的代码中,$rows是数据库中TestPhase表中的MySQL查询结果的数组值 <?php> $rows = array(); $result1 = mysql_query("SELECT * FROM TestPhase where Pid<10", $db) or die("cannot select"); while($row = mysql_fetch_array($result1)) { $rows []= array( 'id' =&g
<?php>
$rows = array();
$result1 = mysql_query("SELECT * FROM TestPhase where Pid<10", $db) or die("cannot select");
while($row = mysql_fetch_array($result1)) {
$rows []= array(
'id' => $row['id'],
'parent' => $row['parent'],
'name' => $row['name'],
);
}
?>
<script type="text/javascript">
var treeData = <?php echo json_encode($rows); ?>;
</script>
变量treeData=;
这段代码是js文件的一个片段,用于使用查询结果和存储在var treeData(即$rows数组)中的值构建树形图
function toTree(treeData) {
var childrenById = {};
var tnodes = {};
var i, row;
// first pass: build child arrays and initial node array
for (i=0; i<treeData.length; i++) {
row = treeData[i];
tnodes[row.id] = {name: row.name, children: []};
if (row.parent == -1) { // assume -1 is used to mark the root's "parent"
root = row.id;
} else if (childrenById[row.parent] === undefined) {
childrenById[row.parent] = [row.id];
} else {
childrenById[row.parent].push(row.id);
}
}
function expand(id) {
if (childrenById[id] !== undefined) {
for (var i=0; i < childrenById[id].length; i ++) {
var childId = childrenById[id][i];
tnodes[id].children.push(expand(childId));
}
}
return tnodes[id];
}
return expand(root);
}
console.log(toTree(treeData));
函数树(treeData){
var childrenById={};
var tnodes={};
var i,世界其他地区;
//第一步:构建子数组和初始节点数组
对于(i=0;我为什么不在SQL中使用JOIN
?我不能使用连接。第一个$rows数组用于构建一个图表,我需要这些数据来构建另一个树。