JasonBruce88 Posted April 1, 2010 Share Posted April 1, 2010 Hello I am able to populate a select box with items form a mysql table no problem. But what I am creating is a form that repeats the fields depending on the users selection on the previous page. This works no problem except for the select box. It fails to select all the results. I have tried many combinations of nested loops but with no joy. In stead of posting all the code I will post the code which generates the select box. Any help or point in the right direction is very much welcome. echo "<fieldset>\n"; echo "<legend>\n"; echo "Products required for property\n"; echo "</legend>\n"; //echo $productItemLoop; $ProductIDArray = "ProductID$propNumber"; $ProductIDArrayBrackets = "[]"; while($nt1=mysql_fetch_array($result)){ $product_options_open = "<option value=\"$nt1[ProductID]\">"; $row_productType = $nt1['ProductType']; $product_options_close = "</option>\n"; } echo "<p>\n"; echo "Hold 'Ctrl' to select more than one product\n"; echo "<br />\n"; echo "<select multiple"; echo " "; echo "name="; echo "\"$ProductIDArray$ProductIDArrayBrackets\""; echo " "; echo "id=\"ProductID$propNumber\" size=\"5\">\n"; echo "$product_options_open\n"; echo "$row_productType\n"; echo "$product_options_close\n"; echo "</select>\n"; echo "</p>\n"; echo "</fieldset>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/197187-select-boxes-in-a-loop/ Share on other sites More sharing options...
mikesta707 Posted April 1, 2010 Share Posted April 1, 2010 if you want a check box to be selected, you need to set its "selected" attribute to the value "selected". for example <option selected="selected">...</option> Quote Link to comment https://forums.phpfreaks.com/topic/197187-select-boxes-in-a-loop/#findComment-1035043 Share on other sites More sharing options...
JasonBruce88 Posted April 1, 2010 Author Share Posted April 1, 2010 Sorry, it was early in the morning. I meant show results not select results. Sorry for the miss leading question. Quote Link to comment https://forums.phpfreaks.com/topic/197187-select-boxes-in-a-loop/#findComment-1035128 Share on other sites More sharing options...
Axeia Posted April 1, 2010 Share Posted April 1, 2010 'non selected' data from elements that give an array are lost when submitted. That would be your <select multiple> but also for example <input type='checkbox' value='cows' name='opt[]' /><input type='checkbox' value='sheeps' name='opt[]' /> There is no way getting around this besides either sticking all options in <input type='hidden' /> as well, or by simply using static code (or a query) to retrieve the information like you did on the original page. I think HTML5 might offer a solution for this, but too early to use and I'm not even sure if it did. Quote Link to comment https://forums.phpfreaks.com/topic/197187-select-boxes-in-a-loop/#findComment-1035169 Share on other sites More sharing options...
JasonBruce88 Posted April 1, 2010 Author Share Posted April 1, 2010 Again my explanation probably was not the best. What is not happing is the population of the select box options. I do appreciate your guys help even though I am not doing a good job of explaining it so thank you. Quote Link to comment https://forums.phpfreaks.com/topic/197187-select-boxes-in-a-loop/#findComment-1035172 Share on other sites More sharing options...
Axeia Posted April 2, 2010 Share Posted April 2, 2010 If you simply want to read out the values, this is the most basic example I can come up with. <?php if( !isset( $_POST['food'] ) ) { echo ' <form action="" method="post"> <label for="food">Which vegatable would you like for dinner?</label> <select name="food[]" MULTIPLE> <option value="carrots">carrots</option> <option value="peas">peas</option> </select> <input type="submit" />'; } else { foreach( $_POST['food'] as $veggy ) { echo $veggy."<br/>"; } } ?> [edit] Forgot to explain The magic part is in the name attribute, it has to end on []. PHP automatically creates an array out of form elements whos name ends on []. Quote Link to comment https://forums.phpfreaks.com/topic/197187-select-boxes-in-a-loop/#findComment-1035873 Share on other sites More sharing options...
JasonBruce88 Posted April 4, 2010 Author Share Posted April 4, 2010 Thank you very much I will try this when I get back to work Quote Link to comment https://forums.phpfreaks.com/topic/197187-select-boxes-in-a-loop/#findComment-1036887 Share on other sites More sharing options...
DavidAM Posted April 4, 2010 Share Posted April 4, 2010 Your while loop is only producing one OPTION element. It will be the last one from the database. You are overwriting the values on each pass. while($nt1=mysql_fetch_array($result)){ $product_options_open = "<option value=\"$nt1[ProductID]\">"; $row_productType = $nt1['ProductType']; $product_options_close = "</option>\n"; } You might try rearranging the code just a bit to output the OPTIONS directly: echo "<p>\n"; echo "Hold 'Ctrl' to select more than one product\n"; echo "<br />\n"; echo "<select multiple"; echo " "; echo "name="; echo "\"$ProductIDArray$ProductIDArrayBrackets\""; echo " "; echo "id=\"ProductID$propNumber\" size=\"5\">\n"; while($nt1=mysql_fetch_array($result)){ echo "<option value=\"$nt1[ProductID]\">\n"; echo $nt1['ProductType'] . "\n"; echo "</option>\n"; } echo "</select>\n"; echo "</p>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/197187-select-boxes-in-a-loop/#findComment-1036921 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.