我在这里的Oracle文章中有以下代码(它使用的是10g;我使用的是11gR2),
http://www.oracle.com/technetwork/articles/bollweg-easytrees-083408.html
效果很好。然后我将其改编为我的其他模式。仍然有效。
但是,我想把它作为JSON,嵌套就像它与标签一样。我尝试了各种各样的东西,但是无法得到它。我想让它变得像,
name:'employee'
children:[
{
name:'employee',
children:[
{
etc.
我试过了:
if ($row['LEVEL']==$top_level)
{
$name['children'][$node]['children'][$node]['children'][]=array('name'=>$_name . ' childLevel->' . $row['LEVEL'] .' rowNum->' . $row['ROWNUM']);
}
else
{
$name['children'][$node]['children'][]=array('name'=>$_name . ' childLevel->' . $row['LEVEL'] .' rowNum->' . $row['ROWNUM']);
}
later on json_encode($name);
...
但每次更深一层(如预期的那样)。我已尝试递归,我将之前的ID发送到新查询并避免下面的整个查询,但我不能正确地使用数组嵌套。我理解如何回显列表的HTML树(我理解代码中发生了什么,而不仅仅是复制和粘贴),而不是嵌套的JSON。
以下是Oracle文章的内容:
<?php
/* file: functional.php
phpversion: 5.1.1
version: 1.0
author: nick bollweg <nickD0TbollwegATgmailD0Tcom>
purpose: demonstrate basic CONNECT BY functionality with Oracle 10g
using a functional approach. */
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>CONNECT BY and array_map » a functional approach</title>
</head>
<body>
<div>
<?php
// open a connection. change these values to fit your situation
$conn = oci_connect( "scott", "tiger", "//localhost/testdb" );
$results = array();
if( !$conn ) {
exit;
}
//build a statement
$stmt = oci_parse( $conn,
" SELECT ENAME, JOB, EMPNO, MGR, LEVEL
FROM EMP
CONNECT BY MGR = PRIOR EMPNO
START WITH MGR IS NULL
ORDER SIBLINGS BY ENAME");
// execute statement
oci_execute( $stmt );
// fetch the results
$nrows = oci_fetch_all ( $stmt, $results, 0, 0,
OCI_FETCHSTATEMENT_BY_ROW );
// we need this value accessible inside the formatting function
global $last;
// set a value not possible in a LEVEL column to allow the
// first row to know it's "firstness"
$last = null;
// add a dummy "row" to cap off formatting
$results[] = array();
$formatted = array_map( "treeFunc", $results );
//output the results
echo implode( "\n",$formatted );
// clean up statement and connection
oci_free_statement( $stmt );
oci_close( $conn );
function treeFunc( $current ) {
// the previous row's level, or null on the first row
global $last;
// structural elements
$openItem = '<li>';
$closeItem = '</li>';
$openChildren = '<ul>';
$closeChildren = '</ul>';
$structure = "";
if( !isset( $current['LEVEL'] ) ) {
// add closing structure(s) equal to the very last
// row's level; this will only fire for the "dummy"
return str_repeat( $closeItem.$closeChildren,
$last );
}
// add the item itself
$item = "{$current['ENAME']} <i>{$current['JOB']}</i>";
if( is_null( $last ) ) {
// add the opening structure in the case of
// the first row
$structure .= $openChildren;
} elseif( $last < $current['LEVEL'] ){
// add the structure to start new branches
$structure .= $openChildren;
} elseif( $last > $current['LEVEL'] ) {
// add the structure to close branches equal to the
// difference between the previous and current levels
$structure .= $closeItem.
str_repeat( $closeChildren.$closeItem,
$last - $current['LEVEL'] );
} else {
$structure .= $closeItem;
}
// add the item structure
$structure .= $openItem;
// update $last so the next row knows whether this row is
// really its parent
$last = $current['LEVEL'];
return $structure.$item;
}
?>
</div>
</body>
</html>