PHP &Tree Control
I was looking for the PHP script for dynamical creation of tree control, but I was not succesful, so I made one on my own. The script I'm giving you for review is not quite nice, cause I spent some hours on it and first I thought it is built like:
Node, Item, Item, Node, Item, Item, Item... etc.
I was bid confused when I found it is first getting all nodes, and then all items :D
Ignote \n and \t if you want to, it is just to make the final code readable for debuging :D
Here it is:
Any comments and improvements are welcome ;-)
Node, Item, Item, Node, Item, Item, Item... etc.
I was bid confused when I found it is first getting all nodes, and then all items :D
Ignote \n and \t if you want to, it is just to make the final code readable for debuging :D
Here it is:
<?
include('../Engine/conf.inc.php');
$connection = mysql_pconnect($SQL_Server,$SQL_User,$SQL_PSWD);
mysql_select_db($SQL_DB);
function tree_setup($main_query)
{
$node_dataset = mysql_query($main_query);
$num_nodes = mysql_num_rows($node_dataset);
$node_no = 0;
$item_no = 1;
$result_text = "\t[\t\"\",\n\t\t\t\t";
$result_value = "\t\t\t{\n\t\t\t\t".$node_no.": [";
while ($row = @mysql_fetch_array($node_dataset))
{
$result_text .= "\"".$row['ReportGroup']."\"";
$result_value .= $item_no;
if($node_no+1 < $num_nodes)
{
$result_text .= ",";
$result_value .= ",";
}
else
{
$result_value .= "],\n";
}
$node_no++;
$item_no++;
}
$node_dataset = mysql_query($main_query);
$num_nodes = mysql_num_rows($node_dataset);
$node_no = 1;
while ($row = @mysql_fetch_array($node_dataset))
{
$node_name = $row['ReportGroup'];
$result_text .= ",\n\t\t\t\t";
$result_value .="\t\t\t\t".$node_no.": [";
//echo "Select ReportName From WAIS_Reports WHERE ReportGroup = '".$node_name."' <br><br>";
$item_dataset = mysql_query("Select ReportName From WAIS_Reports WHERE ReportGroup = '".$node_name."'");
$num_items = mysql_num_rows($item_dataset);
$i = 1;
while ($row = @mysql_fetch_array($item_dataset))
{
$result_text .= '"'.$row['ReportName'].'"';
$result_value .= $item_no;
if($i < $num_items)
{
$result_text .= ',';
$result_value .= ',';
$i++;
}
$item_no++;
}
if($node_no < $num_nodes)
{
$result_value .= "],\n";
}
else
{
$result_value .= "]\n";
}
$node_no++;
}
echo "treeNodeTexts = ".$result_text."\n\t\t\t];\n\n";
echo "tree = ".$result_value."\t\t\t};\n\n";
}
$SQL_tree_query = "Select DISTINCT ReportGroup From WAIS_Reports";
tree_setup($SQL_tree_query);
?>
Any comments and improvements are welcome ;-)
ASJ
October 7,