Solved

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

Posted on 2011-02-21
5
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34946140
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
ID: 34946391
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
ID: 34946791
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
ID: 34947190
Awesome!! thanks! This really helps me learn as well.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34947222
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

751 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