Solved

Creating variable tree in php for javascript to read (NOOB)

Posted on 2011-02-21
5
306 Views
Last Modified: 2012-05-11
 I am trying to duplicate the variable tree that starts at line 52 via php rather than hard coding it. I am able to generate the top level (school), now I figured I could put another loop in the do while statement, but I am getting an infinite loop. I have tried a million ways to do this, but to no avail.
I also have a feeling there is a way I can do this with one db query.

Any help would be appreciated.
Scott

This worked:
<br /><script language="JavaScript">
 totalR = 9;
 var schools = new Array();
aa:{
ahs:{
biz:{
dent:{
edu:{
eng:{
las:{
n:{
up:{
</script>

Adding the loop went awry:

<br /><script language="JavaScript">
totalR = 9;
 var schools = new Array();
aa:{
Health Information Mgmt.:
Health Information Mgmt.:
Health Information Mgmt.:
Health Information Mgmt.:
Health Information Mgmt.:
Health Information Mgmt.:

<?php require_once('../Connections/ids499.php'); ?>
<?php
//query selects departments for header of each major
mysql_select_db($database_ids499, $ids499); //db, connection
$query_Recordset1 = "select distinct deptName_id from gradByMaj"; //query string
$Recordset1 = mysql_query($query_Recordset1, $ids499) or die(mysql_error()); //
$row_Recordset1 = mysql_fetch_assoc($Recordset1); //query exe?
$totalRows_Recordset1 = mysql_num_rows($Recordset1); //count rows

//query selects majors and quantities
mysql_select_db($database_ids499, $ids499); //db, connection
$query_Recordset2 = "select major_id, qGrad, deptName_id, sYear from gradByMaj"; //query string
$Recordset2 = mysql_query($query_Recordset2, $ids499) or die(mysql_error()); //
$row_Recordset2 = mysql_fetch_assoc($Recordset2); //query exe?
$totalRows_Recordset2 = mysql_num_rows($Recordset2); //count rows
?>
<html>
<head>
<script type="text/javascript" src="../protovis-r3.2.js"></script>
<link href="/ids499/ex.css" rel="stylesheet" type="text/css">
<style type="text/css">
#fig {  width: 1000px;}

</style>
<title>Treemap: 2010 Graduation by School, Degree</title>
</head>
<body>

<div id = "title">Treemap of Degrees in 2010 by Department</div><br />

<?php echo "<br />" ?>
<?php $colors = array("orange", "green", "blue", "red");
echo "<script language=\"JavaScript\">\n";
echo " totalR = $totalRows_Recordset1;\n";
echo " var schools = new Array();\n";
do {
echo $row_Recordset1['deptName_id'];
echo ":{\n"; 

/* pulls second level, with majors and numbers. Not working */
do {
echo $row_Recordset2['major_id'];
echo ":\n"; 
} while ($row_Recordset2['deptName_id'] = $row_Recordset1['deptName_id']);


} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
echo "</script>\n"; ?>
<div id="fig">
<script type="text/javascript+protovis">

var tree = {
ahs:{
health:{
	"Health Information Mgmt":20,
  	"Kinesiology":144,
  	"Nutrition":27,
  			},
nurse:{
		"Nursing":158,
			},
upa:{
		"Urban & Public Affairs":22,
			},
 	},

Open in new window

0
Comment
Question by:sukotto100
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
do {
....
do {
...
} while ($row_Recordset2['deptName_id'] = $row_Recordset1['deptName_id']);
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));

I think this is equal all the time
$row_Recordset2['deptName_id'] = $row_Recordset1['deptName_id']
and so the infinite loop

I'm not yet sure about this (still trying to decode the snippet :P) but this seems to be the problem.
0
 

Author Comment

by:sukotto100
Comment Utility
Sorry if it is a mess(>.<); Here is a sample of the table data:

deptName_id| major_id| sYear| qGrad
ahs|      Health Information Mgmt.|      2004|      17
ahs|      Kinesiology|      2004|      78
ahs|      Nutrition|      2004|      18
ahs|      Phys. Ed.|      2004|      0
aa|      Architectural Studies|      2004|      92
aa|      Art Education|      2004|      11
aa|      Art History|      2004|      25
aa|      Electronic Visualization|      2004|      0
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
Comment Utility
I had to change the paths but try this:

<?php require_once('connections/ids499.php'); ?>
<?php
//query selects departments for header of each major
mysql_select_db($database_ids499, $ids499); //db, connection
$query_Recordset1 = "select distinct deptName_id from gradByMaj"; //query string
$Recordset1 = mysql_query($query_Recordset1, $ids499) or die(mysql_error()); //
$totalRows_Recordset1 = mysql_num_rows($Recordset1); //count rows

?>
<html>
<head>
<script type="text/javascript" src="protovis-r3.2.js"></script>
<link href="/ids499/ex.css" rel="stylesheet" type="text/css">
<style type="text/css">
#fig {  width: 1000px;}

</style>
<title>Treemap: 2010 Graduation by School, Degree</title>
</head>
<body>

<div id = "title">Treemap of Degrees in 2010 by Department</div><br />

<?php echo "<br />" ?>
<?php $colors = array("orange", "green", "blue", "red");
echo "<script language=\"JavaScript\">\n";
echo " totalR = $totalRows_Recordset1;\n";
echo " var schools = new Array();\n";

while($row_recordset1 = mysql_fetch_assoc($Recordset1))
{
    echo $row_recordset1['deptName_id'];
    echo ":{\n"; 
    
    //query selects majors and quantities
    $query_Recordset2 = "select major_id, qGrad, deptName_id, sYear from gradByMaj where deptName_id = '".$row_recordset1['deptName_id']."'"; //query string
    $Recordset2 = mysql_query($query_Recordset2, $ids499) or die(mysql_error()); //    
    
    while($row_recordset2 = mysql_fetch_assoc($Recordset2)){
        echo $row_recordset2['major_id'];
        echo ":\n";    
    }
}

echo "</script>\n"; ?>
<div id="fig">
<script type="text/javascript+protovis">

var tree = {
ahs:{
health:{
	"Health Information Mgmt":20,
  	"Kinesiology":144,
  	"Nutrition":27,
  			},
nurse:{
		"Nursing":158,
			},
upa:{
		"Urban & Public Affairs":22,
			},
 	},

Open in new window

0
 

Author Closing Comment

by:sukotto100
Comment Utility
Awesome!! thanks! This really helps me learn as well.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Thanks. It was a long shot, I was notable to get the visualization with protovis, so I was not sure if that did it. Cheers.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now