I'm a new PHP user and could use some help creating a nested "While" loop. The code below creates a drop-down menu. For each value in the drop-down menu, I would like to create another drop-down menu. For instance, in the primary drop-down, you'd see Category1, Category2....CategoryN (with values pulled from the first field in database "test"). Then, if the user selects Category1, another drop-down appears (to the right) and provides SubCategory1, SubCategory2....SubCategoryN (pulled from the second field in database "test").

Questions:
1) Can I basically reuse the same "While" loop or do I have to make some significant modifications?

2) Can I use $result and $row in the second "While" loop?

3) If the answer to #1 above is yes, where do I start the second "While" loop? Immediately after the first "{"?

<select name="result">
<option value="0"
<?php
if (empty ($result)) echo "selected";
?>>- select -</option>



<?php 

// set database server access variables: 

// open connection 

// select database 

// create query 
$query = "SELECT * FROM test"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 



// While Loop 

    while($row = mysql_fetch_row($result)) { 
          echo " <option";
          if ($result==$row)  { echo " selected";  }
          echo ">", $row[0],"</option>\n";        
    }  


// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 

?> 

</select>

Recommended Answers

All 6 Replies

does it matter if the page reloads or not.

if not, then i would use the $_GET method to do this. which means store the select value in the url.

Thanks for the reply, kkeith29. Don't want the page to reload...

Not sure the $_GET method will solve my problem though because:

1) I want to use available data from a field (or fields) in an existing MySQL database (rather than from user input or from a form) to generate drop-down lists (providing users with the ability to select specific values).

2) I want the nested "While" loop to produce output similar to the following:

Value1
Value2
Value3 Subvalue3a
Value4 Subvalue3b
. Subvalue3c
. Subvalue3d
. .
ValueN .
.
Subvalue3N


Where the first column represents a drop-down menu and the second column represents a drop-down menu generated from each of the values in the first drop-down menu (where all values are received from an existing MySQL database).

Your going to have to use JavaScript. PHP runs on the server. Once the server passes the page to the client, PHP, by itself, can no longer interact with the page. Only JavaScript can. You could either:

a.) have php print or echo a javascript function and an onchange event.
or
b.) Look into incorporating AJAX.

You can pull all the data down at once and toggle the display option with <div>s.

hidden = <div style="display:none;"></div>
shown = <div style="display:block;"></div> or <div style="display:inline;"></div>

here is a quick example of a js function that you can use.

<script language="javascript">
function toggledivs(id, state)
{
	if(state == "hide")
	{
		displaystate = "none";
	}
	else if(state == "show")
	{
		displaystate = "block";
	}
	else
	{
		return false;
	}
	
	document.getElementById(id).style.display = displaystate;
}
</script>

It takes a while to get the hang of it but once you do you will find yourself wanting to use it more and more. This process also works extremely well with ajax, they kind of go hand in hand. Personally, I would pull all the data down first, store it in hidden divs and then manipulate it with javascript, however if there is a lot of data to transfer to the browser, I would go with ajax.

As far as the nested loops, you do have to use different variable names.

Your going to have to use JavaScript. PHP runs on the server. Once the server passes the page to the client, PHP, by itself, can no longer interact with the page. Only JavaScript can. You could either:

a.) have php print or echo a javascript function and an onchange event.
or
b.) Look into incorporating AJAX.

Thank buddylee!

I'll look into "a" above.

You can pull all the data down at once and toggle the display option with <div>s.

hidden = <div style="display:none;"></div>
shown = <div style="display:block;"></div> or <div style="display:inline;"></div>

here is a quick example of a js function that you can use.

<script language="javascript">
function toggledivs(id, state)
{
	if(state == "hide")
	{
		displaystate = "none";
	}
	else if(state == "show")
	{
		displaystate = "block";
	}
	else
	{
		return false;
	}
	
	document.getElementById(id).style.display = displaystate;
}
</script>

It takes a while to get the hang of it but once you do you will find yourself wanting to use it more and more. This process also works extremely well with ajax, they kind of go hand in hand. Personally, I would pull all the data down first, store it in hidden divs and then manipulate it with javascript, however if there is a lot of data to transfer to the browser, I would go with ajax.

As far as the nested loops, you do have to use different variable names.

Thanks R0bb0b!

I like your idea and will look into it further.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.