Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

660 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