| | |
PHP nested While loop
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 11
Reputation:
Solved Threads: 0
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 "{"?
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 "{"?
php Syntax (Toggle Plain Text)
<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>
Last edited by peter_budo; Jun 11th, 2008 at 3:23 pm. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Jun 2008
Posts: 11
Reputation:
Solved Threads: 0
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).
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.
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.
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.
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.
PHP Syntax (Toggle Plain Text)
<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 1:29 am.
•
•
Join Date: Jun 2008
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Jun 2008
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
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.
PHP Syntax (Toggle Plain Text)
<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.
![]() |
Similar Threads
- Clean Previous Next Script for MySQL results (PHP)
- PHP Help needed badly (PHP)
- need advice on nested loops. (PHP)
- three dimensional arrays (PHP)
- Image Load: MIA (Python)
- errors showing up when page isn't running (PHP)
- C++ Reorder random numbers (C++)
- Need help with php (PHP)
Other Threads in the PHP Forum
- Previous Thread: Using preg_match to block spam URLs in PHP forms
- Next Thread: Problem with uploading images to website
| Thread Tools | Search this Thread |
advanced alerts apache api archive array autosuggest beginner binary broken cakephp checkbox class clients cms code cron curl database date display dynamic echo email emptydisplayvalue eregi error execute explodefunction file files folder form forms function functions google hack href htaccess html if...loop image include insert ip javasciptvalidation javascript joomla keywords library limit link login mail matching menu mlm multiple mysql object oop password paypal pdf php phpincludeissue problem query radio random recursion recursive remote script search searchbox server sessions shot smarty sms source space speed sql syntax system table tutorial update upload url validator variable vbulletin video web website youtube






