Jump to content

Recommended Posts

I posted a topic a while ago and can't seem to find it, but i'm having trouble trying to create a PHP / Javascript triple menu.

 

basically i need help finding a bug in the code below...

 

What it has to do is grab values from a database to fill the menu options, but to make it completely self-sustaining (and require no manual update of the code), i have to put in a few while loops to make it repeat the correct number of times.  I'm using the example of Classes, Courses, and Curriculums because it helped me to visualize it a little better. 

 

Here is the javascript code i used to model the script:

http://www.trans4mind.com/personal_development/JavaScript/tripleMenu.js

 

Here is the code inserted into the body of the page:

<form name="formTripleMenu" id="formTripleMenu" action="">
<table bgcolor="#FFFFFF" border="0">
<tr>
<td valign="top" align="center" width="171">
<div align="center">
<small>
<strong>
<font color="#000000">
Class (Subject)
</font></strong></small></td>
<td valign="top" align="center" width="176">
<small>
<strong>
<font color="#000000">
Course
</font>
</strong>
</small>
</td>
<td valign="top" align="center" width="169">
<small>
<strong>
<font color="#000000">
Curriculum (Textbook)
</font></strong></small></td></tr>
<tr align="center">
<td width="171" valign="top" align="center">
<select name="menuTopics" id="menuTopics" onchange="changeSubjects()" size="1">
<option value="none">
Classes appear here
</option>

<?php
$link = mysql_connect("mysql79.secureserver.net", "*******", "******") or die(mysql_error());
mysql_select_db("******") or die(mysql_error());

$result1 = mysql_query("SELECT * FROM search1 ORDER BY classes ASC");
$lim1 = mysql_num_rows($result1);

while ($row1 = mysql_fetch_array($result1)){
$class1 = $row1['classes'];
if ($class1 != $class0){
	echo '<option value="none">';
	echo $class1;
	$class0 = $class1;
	echo '</option>';
	}
}
echo '</select></td>';
?>
<td width="176" valign="top" align="center">
<select name="menuSubjects" id="menuSubjects" onchange="changeFiles()" size="1">
<option value="none">
Courses appear here
</option></select></td>
<td width="169" valign="top" align="center">
<select name="menuFiles" id="menuFiles" onchange="go(this)" size="1">
<option value="none">
Curriculums appear here
</option></select></td></tr></table></form>

 

and here is the PHP code i used to replace the javascript (or you could say to write the javascript for me), which does not work.

 

<?php
echo '<script type="text/javascript">';
$link = mysql_connect("mysql79.secureserver.net", "********", "******") or die(mysql_error());
mysql_select_db("******") or die(mysql_error());

$result1 = mysql_query("SELECT * FROM search1 ORDER BY classes ASC");
$lim1 = mysql_num_rows($result1);

$c = 0;

while ($row1 = mysql_fetch_array($result1)){
$v = 0;
$w = 0;
$y = 0;
$z = 0;
$class0 = $class1;
$class1 = $row1['classes'];
//get all course options into array for this class
$result2 = mysql_query("SELECT * FROM search1 WHERE classes='$class1' ORDER BY courses ASC");
$lim2 = mysql_num_rows($result2);
while ($row2 = mysql_fetch_array($result2)){
	$courseARR[$y] = $row2['courses'];
	$y++;
	}
$y = 0;
//$courseARR now contains every course present for the class
//we somehow have to update the $courseARR[$y] for every time the last curriculum is discovered.  We do this by making a counter variable start at zero and work its way up towards the limit of the curriculums for that class.  The limit must be updated each time too, however, so this is put in at the end of the while loop as well.
while ($y < $lim2){	
	$result3 = mysql_query("SELECT * FROM search1 WHERE classes='$class1' AND courses='$courseARR[$y]' ORDER BY curriculum ASC");
	$lim3 = mysql_num_rows($result3);
	while ($v < $lim3){
		while ($row3 = mysql_fetch_array($result3)){
			$curriculumARR[$w] = $row3['curriculum'];
			$w++;
			}
		$v++;
		}
	$w = 0;
		//we now have everything we need to create an option.  The option counter is $c
	while ($w < $lim3){
		$option.$c = $class1.'.'.$courseARR[$y].'.'.$curriculumARR[$w];
		//$option.$c[0] = $class1;
		//$option.$c[1] = $courseARR[$y];
		//$option.$c[2] = $curriculumARR[$w];
		$c++;
		$w++;
		}
	$y++;
	}
}

/*with the current setup, the list should go in this order (math only)...
math.algebra.cpm
math.algebra2.cpm
math.calculus.other
math.geometry.cpm
math.pre-algebra.cpm
math.pre-algebra.other
math.precalculus.precalculus: a graphing approach
*/

//at this point we have any number of $option.$c's, somewhere around 100 maximum, in the form of Math.Pre-Algebra.CPM.
//old:  echo 'options[$x]=new Option("'.$curriculum.'","http://homeworkslackers.com/members/'.$class.'/'.$course.'/'.$curriculum.'/index.php");';

//**********************************BEGIN TRIPLE MENU CODING************************************//
//new Option("text","value")
//this code changes menus
echo 'function nullOptions(aMenu){';
echo 'var tot=aMenu.options.length';
echo 'for (i=0;i<tot;i++)';
echo '{';
echo 'aMenu.options[i]=null';
echo '}';
echo 'aMenu.options.length=0;';
echo '}';

$i = 1;
$letter = 'a';
$lim4 = $c;
$c = 0;
$d = 1;
$numberofcurriculums = 0;

while ($lim4 >= $c){
explode($option.$c, '.');
$c++;
}
$c = 0;

//*******************************************************************************************************

//the while loop ends when the last output is reached.  Until then, it will do what is inside.  The first do while loop outputs the MySubject() stuff as well as all of the options for the curriculum (in the second do while loop).  It continues to output the curriculum options until we reach a new course, where it will finish the loop, output the note at the bottom (netscape stuff), and then add one to $i, the counter.  It will go back and do the same as long as the next class is the same as the current one.  If it is different, it will end the loop, move down another letter in the alphabet, and do the next class's curriculums.  All that the chaos below does is dump every possible curriculum into the format needed.
while ($c <= $lim4){
do {
	echo '//*************************Begin*****************************//';
	// start of files for main option 1 "computer stuff"
	echo 'function MySubject'.$i.$letter.'(aMenu){';
	echo 'nullOptions(aMenu)';
	echo 'with (aMenu){';
	$x = 1;
	echo '//Rewrites the text and values';
	echo 'options[0]=new Option("Select a Curriculum","none");';
	do {
		echo 'options[$x]=new Option("'.$option.$c[2].'","http://homeworkslackers.com/members/'.$option.$c[0].'/'.$option.$c[1].'/'.$option.$c[2].'/index.php");';
		$x++;
		$c++;
		$d++;
		$numberofcurriculums++;
		}while ($option.$c[1] == $option.$d[1]);
	echo 'options[0].selected=true';
	echo '}';
	echo '//Netscape likes a refresh, but Microsoft doesnt';
	echo '/*if (navigator.appName.indexOf("Netscape")>-1)';
	echo 'history.go(0)*/';
	echo '}';
	$i++;
	echo '//**************************End*******************************//';
}while ($option.$c[0] == $option.$d[0]);
$letterORD = ord($letter);
$letterORD++;
$letter = chr($letterORD);
}
//END WHILE LOOP STUFF

/************************************************************************************************************

at this point we have outputted every curriculum for every course for each class.  Next we need to do the same but only for the courses.

*/



/* ####################### mycourses "mytopics" ####################### */

//we have to reset the primary counters because we are moving to the next section.
$c = 0;
$d = 1;
$i = 1;
$x = 1;
$b = -1;
$numberofcourses = 0;

//while the counter is less than the limit (max number of options), it will display the header information for the "topic".  Sets $x to 1, and then types out the default selection.  next, the if loop ensures that repeated courses aren't displayed (which has a high chance of happening).  This is not necessary for the curriculum because it is the most specific choice and has no repeats.  If it is a repeat (is equal to), then it will not echo the data, but it will add one to all the counters (except $x).  If it is still the same subject, the loop will repeat.  If it is not the same subject, the loop will end, the default action will be set, and the footer stuff will be put in.  In this case, we add one to $i, and begin again if $c is still less than the limit.
while ($c <= $lim4){
echo 'function MyTopics'.$i.'(aMenu){';
echo 'nullOptions(aMenu);';
echo 'with (aMenu){';
$x = 1;
echo '//Rewrites the text and values';
echo '$options[0]=new Option("Select a Course","none");';
do {
	if (($option.$c[1] != $option.$b[1]) && ($b >= 0)){
		echo 'options[$x]=new Option("'.$option.$c[1].'","");';
		$x++;
		$numberofcourses++;
		}
	$c++;
	$d++;
	$b++;
	}while ($option.$c[0] == $option.$d[0]);
echo 'options[0].selected=true';

echo '}';
echo '/* //Netscape likes a refresh, but Microsoft doesnt';
echo 'if (navigator.appName.indexOf("Netscape")>-1)';
echo 'history.go(0) */';
echo '}';
$i++;
}



/* ####################### go() ####################### */

echo 'function go(aMenu){';

echo 'if (aMenu.options.value!="none")';
echo '{';
echo 'location=aMenu.options[aMenu.selectedIndex].value';
echo '}';

echo 'else';
echo 'aMenu.options[0].selected=true;';

echo '}';

/* ####################### setUp ####################### */

echo 'function setUp(){';

echo 'if (navigator.appName.indexOf("Microsoft")>-1)';
echo '{';
echo 'document.formDoubleMenu.menuTopics.options[0].selected=true;';

echo 'document.formDoubleMenu.menuSubjects.options[0].selected=true;';

echo 'document.formDoubleMenu.menuFiles.options[0].selected=true;';


echo '}';
echo '}';

/* ####################### end of setup ####################### */



/* ####################### change Subjects ####################### */
echo 'function changeSubjects(){';
echo 'aMenu=document.formDoubleMenu.menuSubjects';
echo 'aMenu2=document.formDoubleMenu.menuFiles';
echo 'aMenu3=document.formDoubleMenu.menuTopics';

echo 'with (aMenu3){';

//initial case - default
echo 'switch (selectedIndex) {';
echo 'case 0:';

echo 'nullOptions(aMenu)';

echo 'nullOptions(aMenu2)';
echo 'aMenu.options[0]=';

echo 'new Option("Curriculums appear here","none")';
echo 'aMenu.options[0].selected=true;';
echo 'history.go(0)';

echo 'break';

$cc = 1;

while ($lim1 >= $cc){
echo 'case '.$cc.':';
//first class
echo 'nullOptions(aMenu)';
echo 'nullOptions(aMenu2)';
echo 'aMenu2.options[0]=';
echo 'new Option("Curriculums appear here","none")';
echo 'aMenu2.options[0].selected=true;';
echo 'MyTopics'.$cc.'(aMenu)';

if ($cc == $lim1){
	echo 'break;';
	}else{
	echo 'break';
	}
}

echo '}';
echo '}';

echo '}';

/* ####################### end of change Subjects ####################### */



/* ####################### changeFiles ####################### */

echo 'function changeFiles(){';
echo 'aMenu=document.formDoubleMenu.menuSubjects';
echo 'aMenu2=document.formDoubleMenu.menuFiles';
echo 'aMenu3=document.formDoubleMenu.menuTopics;';

/* ####################### set of files for option 1 (JavaScript) ####################### */





$cc = 0;
$m = 0;
$c = 0;
$d = 1;
$letter = 'a';

while ($lim4 >= $c){
do {
	if ($cc == 0){
		echo 'if (aMenu3.selectedIndex==1)';
		echo '{';
		echo 'with (aMenu){';

		echo 'switch (selectedIndex) {';
		echo 'case 0:';
		echo 'aMenu2.options.length=0;';
		echo 'aMenu2.options[0]=';

		echo 'new Option("Curriculums appear here","none")';
		echo 'aMenu2.options[0].selected=true;';
		echo 'history.go(0)';

		echo 'break';
		//Description
		}
	echo 'case '.$cc.':';
	echo 'MySubject'.$m.$letter.'(aMenu2)';
	//course1, course2, course3, etc until new subject is reached... then start over.
	echo 'break';
	$cc++;
	}while ($option.$c[0] == $option.$d[0]);
$m = 1;
$cc = 0;
//add one to the letter
$letterORD = ord($letter);
$letterORD++;
$letter = chr($letterORD);
echo '}';
echo '}';
echo '}';
}

echo '}';
//******************************END TRIPLE MENU CODING*****************************//
echo '</script>';
?>

 

Here is an example which should work fine, but doesn't

 

Any idea what makes this code not work?  If you try it and click the link above, it brings you to a page that looks like its stuck in a while loop forever.  I got firefox to run up to 100%cpu and 1.3gb of RAM until i hit stop.

Link to comment
https://forums.phpfreaks.com/topic/52246-solved-php-triple-menu-help/
Share on other sites

I sounds like one of the many while() loops never reaches its terminating condition. But which one I don't know as I don't know what the variables contain. Without knowing whats in the db tables it's imposible to run your code.

i agree but i'm not really a great javascript person so i was wondering if something was bad with the javascript code or it was doing something "illegal"

 

here is the database, pretty simple..

 

database.jpg

 

Basically the purpose of this is to get $option.$c to hold every curriculum with its corrisponding course and class

 

by the way, class is the subject, such as math, english, history, etc.

course is the rigoure of the class, such as algebra, algebra 2, geometry, calculus, etc

and curriculum is the text book used or the lesson plans, such as CPM.

 

$option.$c will be something like math.algebra.cpm, math.algebra.secondcurriculum, math.algebra 2.cpm, math.algebra 2.ALGEBA 2 - Numbers, etc.

 

$i, $x, $c, $d, $w, $v, etc etc are all counters.  Usually i use one letter variables for counters.

 

i appreciate the help  :)

LOL i figured you just wanted to test it out with an example or something

 

check ur pm's

 

and thanks again

 

 

heres the complete code i've been using... i also updated it a bit since the first post

<html>
<head>
<title>Test Page</title>
<?php
echo '<script type="text/javascript">';
$link = mysql_connect("mysql79.secureserver.net", "******", "******") or die(mysql_error());
mysql_select_db("******") or die(mysql_error());

$result1 = mysql_query("SELECT * FROM search1 ORDER BY classes ASC");
$lim1 = mysql_num_rows($result1);

$c = 0;

while ($row1 = mysql_fetch_array($result1)){
$v = 0;
$w = 0;
$y = 0;
$z = 0;
$class0 = $class1;
$class1 = $row1['classes'];
//get all course options into array for this class
$result2 = mysql_query("SELECT * FROM search1 WHERE classes='$class1' ORDER BY courses ASC");
$lim2 = mysql_num_rows($result2);
while ($row2 = mysql_fetch_array($result2)){
	$courseARR[$y] = $row2['courses'];
	$y++;
	}
$y = 0;
//$courseARR now contains every course present for the class
//we somehow have to update the $courseARR[$y] for every time the last curriculum is discovered.  We do this by making a counter variable start at zero and work its way up towards the limit of the curriculums for that class.  The limit must be updated each time too, however, so this is put in at the end of the while loop as well.
while ($y < $lim2){	
	$result3 = mysql_query("SELECT * FROM search1 WHERE classes='$class1' AND courses='$courseARR[$y]' ORDER BY curriculum ASC");
	$lim3 = mysql_num_rows($result3);
	while ($v < $lim3){
		while ($row3 = mysql_fetch_array($result3)){
			$curriculumARR[$w] = $row3['curriculum'];
			$w++;
			}
		$v++;
		}
	$w = 0;
		//we now have everything we need to create an option.  The option counter is $c
	while ($w < $lim3){
		$option.$c = $class1.'.'.$courseARR[$y].'.'.$curriculumARR[$w];
		//$option.$c[0] = $class1;
		//$option.$c[1] = $courseARR[$y];
		//$option.$c[2] = $curriculumARR[$w];
		$c++;
		$w++;
		}
	$y++;
	}
}

/*with the current setup, the list should go in this order (math only)...
math.algebra.cpm
math.algebra2.cpm
math.calculus.other
math.geometry.cpm
math.pre-algebra.cpm
math.pre-algebra.other
math.precalculus.precalculus: a graphing approach
*/

//at this point we have any number of $option.$c's, somewhere around 100 maximum, in the form of Math.Pre-Algebra.CPM.
//old:  echo 'options[$x]=new Option("'.$curriculum.'","http://homeworkslackers.com/members/'.$class.'/'.$course.'/'.$curriculum.'/index.php");';

//**********************************BEGIN TRIPLE MENU CODING************************************ //
//new Option("text","value")
//this code changes menus
echo 'function nullOptions(aMenu){'."\n";
echo 'var tot=aMenu.options.length'."\n";
echo 'for (i=0;i<tot;i++)'."\n";
echo '{';
echo 'aMenu.options[i]=null'."\n";
echo '}';
echo 'aMenu.options.length=0;'."\n";
echo '}';

$i = 1;
$letter = 'a';
$lim4 = $c;
$c = 0;
$b = -1;
$numberofcurriculums = 0;

while ($lim4 >= $c){
explode($option.$c, '.');
$c++;
}
$c = 0;

//*******************************************************************************************************

//the while loop ends when the last output is reached.  Until then, it will do what is inside.  The first do while loop outputs the MySubject() stuff as well as all of the options for the curriculum (in the second do while loop).  It continues to output the curriculum options until we reach a new course, where it will finish the loop, output the note at the bottom (netscape stuff), and then add one to $i, the counter.  It will go back and do the same as long as the next class is the same as the current one.  If it is different, it will end the loop, move down another letter in the alphabet, and do the next class's curriculums.  All that the chaos below does is dump every possible curriculum into the format needed.
while ($lim4 >= $c){
do {
	echo '//*************************Begin*****************************\\'."\n";
	// start of files for main option 1 "computer stuff"
	echo 'function MySubject'.$i.$letter.'(aMenu){'."\n";
	echo 'nullOptions(aMenu)'."\n";
	echo 'with (aMenu){'."\n";
	$x = 1;
	echo '//Rewrites the text and values'."\n";
	echo 'options[0]=new Option("Select a Curriculum","none");'."\n";
	do {
		echo 'options[$x]=new Option("'.$option.$c[2].'","http://homeworkslackers.com/members/'.$option.$c[0].'/'.$option.$c[1].'/'.$option.$c[2].'/index.php");'."\n";
		$x++;
		$c++;
		$b++;
		$numberofcurriculums++;
		}while ($option.$c[1] == $option.$b[1]);
	echo 'options[0].selected=true';
	echo '}';
	echo '//Netscape likes a refresh, but Microsoft doesnt'."\n";
	echo 'if (navigator.appName.indexOf("Netscape")>-1)'."\n";
	echo 'history.go(0)'."\n";
	echo '}'."\n";
	$i++;
	echo '//**************************End*******************************\\'."\n";
}while ($option.$c[0] == $option.$b[0]);
$letterORD = ord($letter);
$letterORD++;
$letter = chr($letterORD);
}
//END WHILE LOOP STUFF

//\************************************************************************************************************

//at this point we have outputted every curriculum for every course for each class.  Next we need to do the same but only for the courses.



// ####################### mycourses "mytopics" ####################### //

//we have to reset the primary counters because we are moving to the next section.
$c = 0;
$i = 1;
$x = 1;
$b = -1;
$numberofcourses = 0;

//another chaotic while loop... the last of them hopefully.  while the counter is less than the limit (max number of options), it will display the header information for the "topic".  Sets $x to 1, and then types out the default selection.  next, the if loop ensures that repeated courses aren't displayed (which has a high chance of happening).  This is not necessary for the curriculum because it is the most specific choice and has no repeats.  If it is a repeat (is equal to), then it will not echo the data, but it will add one to all the counters (except $x).  If it is still the same subject, the loop will repeat.  If it is not the same subject, the loop will end, the default action will be set, and the footer stuff will be put in.  In this case, we add one to $i, and begin again if $c is still less than the limit.
while ($c <= $lim4){
echo 'function MyTopics'.$i.'(aMenu){';
echo 'nullOptions(aMenu);';
echo 'with (aMenu){';
$x = 1;
echo '//Rewrites the text and values';
echo '$options[0]=new Option("Select a Course","none");';
do {
	if (($option.$c[1] != $option.$b[1]) && ($b >= 0)){
		echo 'options[$x]=new Option("'.$option.$c[1].'","");';
		$x++;
		$numberofcourses++;
		}
	$c++;
	$b++;
	}while ($option.$c[0] == $option.$b[0]);
echo 'options[0].selected=true';

echo '}';
echo '//Netscape likes a refresh, but Microsoft doesnt';
echo 'if (navigator.appName.indexOf("Netscape")>-1)';
echo 'history.go(0)';
echo '}';
$i++;
}



// ####################### go() #######################  - stuff i shouldn't touch :-P //

echo 'function go(aMenu){';

echo 'if (aMenu.options.value!="none")';
echo '{';
echo 'location=aMenu.options[aMenu.selectedIndex].value';
echo '}';

echo 'else';
echo 'aMenu.options[0].selected=true;';

echo '}';

// ####################### setUp ####################### - more stuff i shouldnt touch //

echo 'function setUp(){';

echo 'if (navigator.appName.indexOf("Microsoft")>-1)';
echo '{';
echo 'document.formDoubleMenu.menuTopics.options[0].selected=true;';

echo 'document.formDoubleMenu.menuSubjects.options[0].selected=true;';

echo 'document.formDoubleMenu.menuFiles.options[0].selected=true;';


echo '}';
echo '}';

// ####################### end of setup ####################### //



// ####################### change Subjects ####################### //
echo 'function changeSubjects(){';
echo 'aMenu=document.formDoubleMenu.menuSubjects';
echo 'aMenu2=document.formDoubleMenu.menuFiles';
echo 'aMenu3=document.formDoubleMenu.menuTopics';

echo 'with (aMenu3){';

//initial case - default
echo 'switch (selectedIndex) {';
echo 'case 0:';

echo 'nullOptions(aMenu)';

echo 'nullOptions(aMenu2)';
echo 'aMenu.options[0]=';

echo 'new Option("Curriculums appear here","none")';
echo 'aMenu.options[0].selected=true;';
echo 'history.go(0)';

echo 'break';

$cc = 1;

while ($lim1 >= $cc){
echo 'case '.$cc.':';
//first class
echo 'nullOptions(aMenu)';
echo 'nullOptions(aMenu2)';
echo 'aMenu2.options[0]=';
echo 'new Option("Curriculums appear here","none")';
echo 'aMenu2.options[0].selected=true;';
echo 'MyTopics'.$cc.'(aMenu)';
echo 'break';
}

echo '}';
echo '}';

echo '}';

//* ####################### end of change Subjects ####################### //



//* ####################### changeFiles ####################### //

echo 'function changeFiles(){';
echo 'aMenu=document.formDoubleMenu.menuSubjects';
echo 'aMenu2=document.formDoubleMenu.menuFiles';
echo 'aMenu3=document.formDoubleMenu.menuTopics;';

//* ####################### set of files for option 1 (JavaScript) ####################### //





$cc = 0;
$m = 0;
$c = 0;
$b = -1;
$letter = 'a';

while ($lim4 >= $c){
do {
	if ($cc == 0){
		echo 'if (aMenu3.selectedIndex==1)';
		echo '{';
		echo 'with (aMenu){';

		echo 'switch (selectedIndex) {';
		echo 'case 0:';
		echo 'aMenu2.options.length=0;';
		echo 'aMenu2.options[0]=';

		echo 'new Option("Curriculums appear here","none")';
		echo 'aMenu2.options[0].selected=true;';
		echo 'history.go(0)';

		echo 'break';
		//Description
		}
	echo 'case '.$cc.':';
	echo 'MySubject'.$m.$letter.'(aMenu2)';
	//course1, course2, course3, etc until new subject is reached... then start over.
	echo 'break';
	$cc++;
	}while ($option.$c[0] == $option.$b[0]);
$m = 1;
$cc = 0;
//add one to the letter
$letterORD = ord($letter);
$letterORD++;
$letter = chr($letterORD);
echo '}';
echo '}';
echo '}';
}

echo '}';
//******************************END TRIPLE MENU CODING*****************************/
echo '</script>';
?>

</head>
<body>
<form name="formTripleMenu" id="formTripleMenu" action="">
<table bgcolor="#FFFFFF" border="0">
<tr>
<td valign="top" align="center" width="171">
<div align="center">
<small>
<strong>
<font color="#000000">
Class (Subject)
</font></strong></small></td>
<td valign="top" align="center" width="176">
<small>
<strong>
<font color="#000000">
Course
</font>
</strong>
</small>
</td>
<td valign="top" align="center" width="169">
<small>
<strong>
<font color="#000000">
Curriculum (Textbook)
</font></strong></small></td></tr>
<tr align="center">
<td width="171" valign="top" align="center">
<select name="menuTopics" id="menuTopics" onchange="changeSubjects()" size="1">
<option value="none">
Classes appear here
</option>

<?php
$result1 = mysql_query("SELECT * FROM search1 ORDER BY classes ASC");
$lim1 = mysql_num_rows($result1);

while ($row1 = mysql_fetch_array($result1)){
$class1 = $row1['classes'];
if ($class1 != $class0){
	echo '<option value="none">';
	echo $class1;
	$class0 = $class1;
	echo '</option>';
	}
}
echo '</select></td>';
?>
<td width="176" valign="top" align="center">
<select name="menuSubjects" id="menuSubjects" onchange="changeFiles()" size="1">
<option value="none">
Courses appear here
</option></select></td>
<td width="169" valign="top" align="center">
<select name="menuFiles" id="menuFiles" onchange="go(this)" size="1">
<option value="none">
Curriculums appear here
</option></select></td></tr></table></form>
</body>
</html>

fixed several while loop problems, but not the dominant one

 

<html>
<head>
<title>Test Page</title>
<?php
echo '<script type="text/javascript">';
$link = mysql_connect("mysql79.secureserver.net", "******", "*******") or die(mysql_error());
mysql_select_db("*********") or die(mysql_error());

$result1 = mysql_query("SELECT * FROM search1 ORDER BY classes ASC");
$lim1 = mysql_num_rows($result1);

$c = 0;

while ($row1 = mysql_fetch_array($result1)){
$v = 0;
$w = 0;
$y = 0;
$z = 0;
$class0 = $class1;
$class1 = $row1['classes'];
//get all course options into array for this class
$result2 = mysql_query("SELECT * FROM search1 WHERE classes='$class1' ORDER BY courses ASC");
$lim2 = mysql_num_rows($result2);
while ($row2 = mysql_fetch_array($result2)){
	$courseARR[$y] = $row2['courses'];
	$y++;
	}
$y = 0;
//$courseARR now contains every course present for the class
//we somehow have to update the $courseARR[$y] for every time the last curriculum is discovered.  We do this by making a counter variable start at zero and work its way up towards the limit of the curriculums for that class.  The limit must be updated each time too, however, so this is put in at the end of the while loop as well.
while ($y < $lim2){	
	$result3 = mysql_query("SELECT * FROM search1 WHERE classes='$class1' AND courses='$courseARR[$y]' ORDER BY curriculum ASC");
	$lim3 = mysql_num_rows($result3);
	while ($v < $lim3){
		while ($row3 = mysql_fetch_array($result3)){
			$curriculumARR[$w] = $row3['curriculum'];
			$w++;
			}
		$v++;
		}
	$w = 0;
		//we now have everything we need to create an option.  The option counter is $c
	while ($w < $lim3){
		$option.$c = $class1.'.'.$courseARR[$y].'.'.$curriculumARR[$w];
		//$option.$c[0] = $class1;
		//$option.$c[1] = $courseARR[$y];
		//$option.$c[2] = $curriculumARR[$w];
		$c++;
		$w++;
		}
	$y++;
	}
}

/*with the current setup, the list should go in this order (math only)...
math.algebra.cpm
math.algebra2.cpm
math.calculus.other
math.geometry.cpm
math.pre-algebra.cpm
math.pre-algebra.other
math.precalculus.precalculus: a graphing approach
*/

//at this point we have any number of $option.$c's, somewhere around 100 maximum, in the form of Math.Pre-Algebra.CPM.
//old:  echo 'options[$x]=new Option("'.$curriculum.'","http://homeworkslackers.com/members/'.$class.'/'.$course.'/'.$curriculum.'/index.php");';

//**********************************BEGIN TRIPLE MENU CODING************************************ //
//new Option("text","value")
//this code changes menus
echo 'function nullOptions(aMenu){'."\n";
echo 'var tot=aMenu.options.length'."\n";
echo 'for (i=0;i<tot;i++)'."\n";
echo '{';
echo 'aMenu.options[i]=null'."\n";
echo '}';
echo 'aMenu.options.length=0;'."\n";
echo '}';

$i = 1;
$letter = 'a';
$lim4 = $c;
$c = 0;
$b = -1;
$numberofcurriculums = 0;

while ($lim4 >= $c){
explode($option.$c, '.');
$c++;
}
$c = 0;

//*******************************************************************************************************

//the while loop ends when the last output is reached.  Until then, it will do what is inside.  The first do while loop outputs the MySubject() stuff as well as all of the options for the curriculum (in the second do while loop).  It continues to output the curriculum options until we reach a new course, where it will finish the loop, output the note at the bottom (netscape stuff), and then add one to $i, the counter.  It will go back and do the same as long as the next class is the same as the current one.  If it is different, it will end the loop, move down another letter in the alphabet, and do the next class's curriculums.  All that the chaos below does is dump every possible curriculum into the format needed.
while ($lim4 >= $c){
do {
	echo '//*************************Begin*****************************\\'."\n";
	// start of files for main option 1 "computer stuff"
	echo 'function MySubject'.$i.$letter.'(aMenu){'."\n";
	echo 'nullOptions(aMenu)'."\n";
	echo 'with (aMenu){'."\n";
	$x = 1;
	echo '//Rewrites the text and values'."\n";
	echo 'options[0]=new Option("Select a Curriculum","none");'."\n";
	do {
		echo 'options[$x]=new Option("'.$option.$c[2].'","http://homeworkslackers.com/members/'.$option.$c[0].'/'.$option.$c[1].'/'.$option.$c[2].'/index.php");'."\n";
		$x++;
		$c++;
		$b++;
		$numberofcurriculums++;
		}while ($option.$c[1] == $option.$b[1]);
	echo 'options[0].selected=true';
	echo '}';
	echo '//Netscape likes a refresh, but Microsoft doesnt'."\n";
	echo 'if (navigator.appName.indexOf("Netscape")>-1)'."\n";
	echo 'history.go(0)'."\n";
	echo '}'."\n";
	$i++;
	echo '//**************************End*******************************\\'."\n";
}while ($option.$c[0] == $option.$b[0]);
$letterORD = ord($letter);
$letterORD++;
$letter = chr($letterORD);
}
//END WHILE LOOP STUFF

//\************************************************************************************************************

//at this point we have outputted every curriculum for every course for each class.  Next we need to do the same but only for the courses.



// ####################### mycourses "mytopics" ####################### //

//we have to reset the primary counters because we are moving to the next section.
$c = 0;
$i = 1;
$x = 1;
$b = -1;
$numberofcourses = 0;

//another chaotic while loop... the last of them hopefully.  while the counter is less than the limit (max number of options), it will display the header information for the "topic".  Sets $x to 1, and then types out the default selection.  next, the if loop ensures that repeated courses aren't displayed (which has a high chance of happening).  This is not necessary for the curriculum because it is the most specific choice and has no repeats.  If it is a repeat (is equal to), then it will not echo the data, but it will add one to all the counters (except $x).  If it is still the same subject, the loop will repeat.  If it is not the same subject, the loop will end, the default action will be set, and the footer stuff will be put in.  In this case, we add one to $i, and begin again if $c is still less than the limit.
while ($c <= $lim4){
echo 'function MyTopics'.$i.'(aMenu){';
echo 'nullOptions(aMenu);';
echo 'with (aMenu){';
$x = 1;
echo '//Rewrites the text and values';
echo '$options[0]=new Option("Select a Course","none");';
do {
	if (($option.$c[1] != $option.$b[1]) && ($b >= 0)){
		echo 'options[$x]=new Option("'.$option.$c[1].'","");';
		$x++;
		$numberofcourses++;
		}
	$c++;
	$b++;
	}while ($option.$c[0] == $option.$b[0]);
echo 'options[0].selected=true';

echo '}';
echo '//Netscape likes a refresh, but Microsoft doesnt';
echo 'if (navigator.appName.indexOf("Netscape")>-1)';
echo 'history.go(0)';
echo '}';
$i++;
}



// ####################### go() #######################  - stuff i shouldn't touch :-P //

echo 'function go(aMenu){';

echo 'if (aMenu.options.value!="none")';
echo '{';
echo 'location=aMenu.options[aMenu.selectedIndex].value';
echo '}';

echo 'else';
echo 'aMenu.options[0].selected=true;';

echo '}';

// ####################### setUp ####################### - more stuff i shouldnt touch //

echo 'function setUp(){';

echo 'if (navigator.appName.indexOf("Microsoft")>-1)';
echo '{';
echo 'document.formDoubleMenu.menuTopics.options[0].selected=true;';

echo 'document.formDoubleMenu.menuSubjects.options[0].selected=true;';

echo 'document.formDoubleMenu.menuFiles.options[0].selected=true;';


echo '}';
echo '}';

// ####################### end of setup ####################### //



// ####################### change Subjects ####################### //
echo 'function changeSubjects(){';
echo 'aMenu=document.formDoubleMenu.menuSubjects';
echo 'aMenu2=document.formDoubleMenu.menuFiles';
echo 'aMenu3=document.formDoubleMenu.menuTopics';

echo 'with (aMenu3){';

//initial case - default
echo 'switch (selectedIndex) {';
echo 'case 0:';

echo 'nullOptions(aMenu)';

echo 'nullOptions(aMenu2)';
echo 'aMenu.options[0]=';

echo 'new Option("Curriculums appear here","none")';
echo 'aMenu.options[0].selected=true;';
echo 'history.go(0)';

echo 'break';

$cc = 1;

while ($lim1 >= $cc){
echo 'case '.$cc.':';
//first class
echo 'nullOptions(aMenu)';
echo 'nullOptions(aMenu2)';
echo 'aMenu2.options[0]=';
echo 'new Option("Curriculums appear here","none")';
echo 'aMenu2.options[0].selected=true;';
echo 'MyTopics'.$cc.'(aMenu)';
echo 'break';
$cc++;
}

echo '}';
echo '}';

echo '}';

//* ####################### end of change Subjects ####################### //



//* ####################### changeFiles ####################### //

echo 'function changeFiles(){';
echo 'aMenu=document.formDoubleMenu.menuSubjects';
echo 'aMenu2=document.formDoubleMenu.menuFiles';
echo 'aMenu3=document.formDoubleMenu.menuTopics;';

//* ####################### set of files for option 1 (JavaScript) ####################### //





$cc = 0;
$m = 0;
$c = 0;
$b = -1;
$letter = 'a';

while ($lim4 >= $c){
do {
	if ($cc == 0){
		echo 'if (aMenu3.selectedIndex==1)';
		echo '{';
		echo 'with (aMenu){';

		echo 'switch (selectedIndex) {';
		echo 'case 0:';
		echo 'aMenu2.options.length=0;';
		echo 'aMenu2.options[0]=';

		echo 'new Option("Curriculums appear here","none")';
		echo 'aMenu2.options[0].selected=true;';
		echo 'history.go(0)';

		echo 'break';
		//Description
		}
	echo 'case '.$cc.':';
	echo 'MySubject'.$m.$letter.'(aMenu2)';
	//course1, course2, course3, etc until new subject is reached... then start over.
	echo 'break';
	$cc++;
	$b++;
	$c++;
	}while ($option.$c[0] == $option.$b[0]);
$m = 1;
$cc = 0;
//add one to the letter
$letterORD = ord($letter);
$letterORD++;
$letter = chr($letterORD);
echo '}';
echo '}';
echo '}';
}

echo '}';
//******************************END TRIPLE MENU CODING*****************************/
echo '</script>';
?>

</head>
<body>
<form name="formTripleMenu" id="formTripleMenu" action="">
<table bgcolor="#FFFFFF" border="0">
<tr>
<td valign="top" align="center" width="171">
<div align="center">
<small>
<strong>
<font color="#000000">
Class (Subject)
</font></strong></small></td>
<td valign="top" align="center" width="176">
<small>
<strong>
<font color="#000000">
Course
</font>
</strong>
</small>
</td>
<td valign="top" align="center" width="169">
<small>
<strong>
<font color="#000000">
Curriculum (Textbook)
</font></strong></small></td></tr>
<tr align="center">
<td width="171" valign="top" align="center">
<select name="menuTopics" id="menuTopics" onchange="changeSubjects()" size="1">
<option value="none">
Classes appear here
</option>

<?php
$result1 = mysql_query("SELECT * FROM search1 ORDER BY classes ASC");
$lim1 = mysql_num_rows($result1);

while ($row1 = mysql_fetch_array($result1)){
$class1 = $row1['classes'];
if ($class1 != $class0){
	echo '<option value="none">';
	echo $class1;
	$class0 = $class1;
	echo '</option>';
	}
}
echo '</select></td>';
?>
<td width="176" valign="top" align="center">
<select name="menuSubjects" id="menuSubjects" onchange="changeFiles()" size="1">
<option value="none">
Courses appear here
</option></select></td>
<td width="169" valign="top" align="center">
<select name="menuFiles" id="menuFiles" onchange="go(this)" size="1">
<option value="none">
Curriculums appear here
</option></select></td></tr></table></form>
</body>
</html>

Problem at infinite loop here

do {
		echo 'options[$x]=new Option("'.$option.$c[2].'","http://homeworkslackers.com/members/'.$option.$c[0].'/'.$option.$c[1].'/'.$option.$c[2].'/index.php");';
		$x++;
		$c++;
		$d++;
		$numberofcurriculums++;
		}while ($option.$c[1] == $option.$d[1]);       // while null == null, forever

 

Actually, problems began when you tried using "$option.$c = something". It just manifests itself here.

ok i think i get it, but i dont know how to change the name of a variable now.

 

I want to add one to the variable's name every time i store a value... how do i do this?

 

so for example:

 

value 1 i want to store as $option0[0]

value 2 i want to store as $option1[0]

value 3 i want to store as $option2[0]

value 4 i want to store as $option3[0]

...etc

 

so how do i keep adding one number to a variables name?

 

jeez i feel like this is PHP-101 stuff...

If you use the baaSelect class in my sig

 

<?php
$link = mysql_connect("****", "****", "****") or die(mysql_error());
mysql_select_db("****") or die(mysql_error());

include 'baaselect.php';

/**************************************************************************************************
* create 3 tables, normalizing your current table
*
*        class              course             curriculum
*        ==========         ============       =============
*        id                 id        ---+      id
*        class   ---+       course       |      curriculum
*                   +----   classID      +----  courseID
*
*
**************************************************************/


mysql_query ("CREATE TEMPORARY TABLE tmp_class (
                id int not null auto_increment primary key,
                class varchar(45))");
mysql_query ("CREATE TEMPORARY TABLE tmp_course (
                id int not null auto_increment primary key,
                course varchar(45),
                classID int)");
mysql_query ("CREATE TEMPORARY TABLE tmp_curriculum (
                id int not null auto_increment primary key,
                curriculum varchar(45),
                courseID int)");

$result = mysql_query("INSERT INTO tmp_class(class) SELECT DISTINCT classes FROM search1 ORDER BY classes ASC");
$result = mysql_query("INSERT INTO tmp_course(course,classID) 
                        SELECT s.courses, c.id 
                        FROM search1 s INNER JOIN tmp_class c ON s.classes = c.class");
$result = mysql_query("INSERT INTO tmp_curriculum(curriculum,courseID) 
                        SELECT s.curriculum, c.id 
                        FROM search1 s INNER JOIN tmp_course c ON s.courses = c.course");


/**************************************************************************************************
* use baaSelect class to create the dropdowns
* once the normalized tables are in place   
**************************************************/
$sel = new baaSelect();
$sel->addSelect('xclass', 'tmp_class', 'id', 'class','',1,'-class-');
$sel->addSelect('xcourse', 'tmp_course', 'id', 'course', 'classID',1,'-course-');
$sel->addSelect('xcurriculum', 'tmp_curriculum', 'id', 'curriculum', 'courseID',1,'-curriculum-');
?>
<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>Triple select</title>
<meta name="author" content="Barand">
<?php 
    $sel->makeScript() 
?>
</head>
<body>
<form> 
<?php
     $sel->makeSelect('xclass');
     $sel->makeSelect('xcourse');
     $sel->makeSelect('xcurriculum');
?>
</form>
</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.