User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 425,896 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,937 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1334 | Replies: 6
Reply
Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

PHP nested While loop

  #1  
Jun 11th, 2008
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 "{"?




  1. <select name="result">
  2. <option value="0"
  3. <?php
  4. if (empty ($result)) echo "selected";
  5. ?>>- select -</option>
  6.  
  7.  
  8.  
  9. <?php
  10.  
  11. // set database server access variables:
  12.  
  13. // open connection
  14.  
  15. // select database
  16.  
  17. // create query
  18. $query = "SELECT * FROM test";
  19.  
  20. // execute query
  21. $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
  22.  
  23.  
  24.  
  25. // While Loop
  26.  
  27. while($row = mysql_fetch_row($result)) {
  28. echo " <option";
  29. if ($result==$row) { echo " selected"; }
  30. echo ">", $row[0],"</option>\n";
  31. }
  32.  
  33.  
  34. // free result set memory
  35. mysql_free_result($result);
  36.  
  37. // close connection
  38. mysql_close($connection);
  39.  
  40. ?>
  41.  
  42. </select>
Last edited by peter_budo : Jun 11th, 2008 at 2:23 pm. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 554
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 57
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Posting Pro

Re: PHP nested While loop

  #2  
Jun 11th, 2008
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.
Last edited by kkeith29 : Jun 11th, 2008 at 10:57 am.
Reply With Quote  
Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Re: PHP nested While loop

  #3  
Jun 11th, 2008
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).
Reply With Quote  
Join Date: Nov 2007
Location: Arkansas
Posts: 396
Reputation: buddylee17 will become famous soon enough buddylee17 will become famous soon enough 
Rep Power: 2
Solved Threads: 78
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Whiz

Re: PHP nested While loop

  #4  
Jun 11th, 2008
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.
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 770
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 63
R0bb0b's Avatar
R0bb0b R0bb0b is online now Online
Master Poster

Re: PHP nested While loop

  #5  
Jun 12th, 2008
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.
Last edited by R0bb0b : Jun 12th, 2008 at 12:29 am.
Reply With Quote  
Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Re: PHP nested While loop

  #6  
Jun 12th, 2008
Originally Posted by buddylee17 View Post
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.
Reply With Quote  
Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Re: PHP nested While loop

  #7  
Jun 12th, 2008
Originally Posted by R0bb0b View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 7:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC