PHP nested While loop

Reply

Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

PHP nested While loop

 
0
  #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 3:23 pm. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: PHP nested While loop

 
0
  #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 11:57 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Re: PHP nested While loop

 
0
  #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 Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: PHP nested While loop

 
0
  #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 Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: PHP nested While loop

 
0
  #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.

  1. <script language="javascript">
  2. function toggledivs(id, state)
  3. {
  4. if(state == "hide")
  5. {
  6. displaystate = "none";
  7. }
  8. else if(state == "show")
  9. {
  10. displaystate = "block";
  11. }
  12. else
  13. {
  14. return false;
  15. }
  16.  
  17. document.getElementById(id).style.display = displaystate;
  18. }
  19. </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 1:29 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Re: PHP nested While loop

 
0
  #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 Quick reply to this message  
Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Re: PHP nested While loop

 
0
  #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.

  1. <script language="javascript">
  2. function toggledivs(id, state)
  3. {
  4. if(state == "hide")
  5. {
  6. displaystate = "none";
  7. }
  8. else if(state == "show")
  9. {
  10. displaystate = "block";
  11. }
  12. else
  13. {
  14. return false;
  15. }
  16.  
  17. document.getElementById(id).style.display = displaystate;
  18. }
  19. </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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC