Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-21
5
Medium Priority
?
318 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
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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

916 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