phppup Posted February 22, 2012 Share Posted February 22, 2012 I'm trying to pull up an identical form after submission by using its "id" and ECHO for the form lines. Question: What is the correct approach for ECHOing an HTML dropdown that is written as: <select name='chixcutlet' value='' > <option value='0.00' selected> --- </option> <option value='1.00'> 1 </option> <option value='2.00'> 2 </option> <option value='3.00'> 3 </option> </select> I want it to get the info from the database so that the option that is saved in the database will be new default when the page is loaded. If I don't change it to the previous info it will update the database with the default option rather then the actual option. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/ Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 Something like... $options = array( '0.00' => '---', '1.00' => '1', '2.00' => '2', '3.00' => '3' ); $selected = '1.00'; foreach($options as $key => $val) { echo '<option value="' . $key . '" ' . ($key == $selected ? 'selected="selected"' : '') . '>' . $val . '</option>'; } Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320129 Share on other sites More sharing options...
MargateSteve Posted February 22, 2012 Share Posted February 22, 2012 The way I normally tackle this (with psuedo code and ignoring the rest of the code is <select name='chixcutlet' > <option value='$valueoptions' <?php if ($valueoptions = $savedvalue) {echo 'selected '}> $name </option> ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320139 Share on other sites More sharing options...
cdoggg94 Posted February 22, 2012 Share Posted February 22, 2012 if you are using something like dreamweaver you can create a recordset but if not your going to have to make a connection something like this: <?php require_once('yourConnection.php'); $query1 = mysql_query("SELECT* FROM your_table ORDER BY id); $info = mysql_fetch_array($query1); ?> <select name='chixcutlet' value='' > <option value='0.00' selected> --- </option> <?php do{ ?> <option value='<?php echo $info['value_from_db'] ;?>'><?php echo $info['name_from_db'] ;?></option> <?php } while ($info = mysql_fetch_assoc($info)); ?> </select> I dont know if thats completely going to work as is...but basically you need to display content as you would any other time but one as the value and one as whatever you want people to choose from and repeat the region for as many records as you have. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320141 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 I've got this in my query: $result = mysql_query("SELECT * FROM pass WHERE id = 4 ") or die(mysql_error()); $row = mysql_fetch_array( $result ); And inside my form: echo "cutlets <select name='chixcutlet' value=''> "; echo " <option value='0.00' selected> --- </option>"; echo " <option value='1.00'> 1 </option>"; echo " <option value='2.00'> 2 </option>"; echo " <option value='3.00'> 3 </option>"; echo " </select>"; My form comes up fine, but no values are extracted from the DB. I want it to get the info from the database so that the option that is saved in the database will be new default when the page is loaded. If I don't change it to the previous info it will update the database with the default option rather then the actual option. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320153 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 Then you should build the select field dynamically, so you can check the value stored in the database against the value of the <option> tag, and echo 'selected="selected"' when they match. Otherwise, you'd need to incorporate the logic into every single <option> tag as you manually write the html. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320158 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 I'll do it manually (as long as it works) but I don't know what to write. How can I do it dynamically if every item has a different field name and I need the SUBMITTED value for EVERY item to become the NEW DEFAULT with the intention of the form being used to update data. example: customer ordered 1 chicken cutlet and now wants 3 instead. I want to view the original order, verify that ONE was ordered, change the selection to 3, and UPDATe the record. If this cannot be handled in one page, I will gladly do it it two steps to avoid complications Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320162 Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 I'll do it manually (as long as it works) but I don't know what to write. How can I do it dynamically if every item has a different field name and I need the SUBMITTED value for EVERY item to become the NEW DEFAULT with the intention of the form being used to update data. example: customer ordered 1 chicken cutlet and now wants 3 instead. I want to view the original order, verify that ONE was ordered, change the selection to 3, and UPDATe the record. If this cannot be handled in one page, I will gladly do it it two steps to avoid complications I showed you how, right here: Something like... $options = array( '0.00' => '---', '1.00' => '1', '2.00' => '2', '3.00' => '3' ); $selected = '1.00'; foreach($options as $key => $val) { echo '<option value="' . $key . '" ' . ($key == $selected ? 'selected="selected"' : '') . '>' . $val . '</option>'; } Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320163 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 This looks like it's establishing a PHP dropdown menu, not takiing the data from my DB, or placing it in my HTML dropdown (the same one that submitted it) from my HTML form. I can change the form to PHP, but I prefer the HTML dropdown for design purposes. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320167 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 Whether you hard code the html or generate it dynamically with php has no bearing on the design of the site. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320168 Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 This looks like it's establishing a PHP dropdown menu, not takiing the data from my DB, or placing it in my HTML dropdown (the same one that submitted it) from my HTML form. I can change the form to PHP, but I prefer the HTML dropdown for design purposes. You cannot "change the form to PHP". PHP outputs HTML, you end up with the same HTML either way. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320170 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 The point of this being that this code: $values = range(0, 10); echo "<select name=\"my_field\">\n"; foreach($values as $v) { echo "<option value=\"$v\">$v</option>\n"; } echo "</select>\n"; Generates this output: <select name="my_field"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> So the end result is the same output, with much less typing, and it's more easily maintainable. If you wanted to make it 20 options instead of 10, you would only need to change one character; in range(0,10), 10 would become 20. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320175 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 OKAY! Now I'm beginning to understand how PHP could have made my life easier. However, I am WAY beyond that. All the code has already been handwritten and the form has already submitted data INTO the DB. Now I have generated the identical form that comes up when I make a request for data FROM the DB. My difficulty at this point is that no VALUES are being placed in the form. ALL the data is generated INTo the database with HTML dropdown menus. I want it to get the info from the database so that the option that is saved in the database will be new default when the page is loaded, and will SHOW in the form. If I don't change it to the previous info it will update the database with the default option rather then the actual option. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320181 Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 And we've shown you how several times. Where are you stuck? Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320188 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 ALSO, with the above code, how would I select MY default value, so that an item that had no selection would be recorded as the default. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320189 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 Not sure WHERE TO PLACE the code now. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320191 Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 What is the column in your table called that you wish to use as the dropdown selection? Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320192 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 There are MANY, but let's work with CUTLETS. (i'd list them all but it's lunchtime and we'd all get hungry.. LOL) By the way, I want to thank you all for your patience. I do appreciate it. [Can you comment on MargateSteve's code. It seems short and concise, but seems to have an unknown variable.] Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320196 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 Beginning to see where this might work. But if my INITIAL default is ZERO, do i change the code to: $selected=0 Or does =1 get the code moving? Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320198 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 Right. Well, it would still be easier to rewrite the form. This is somewhat simplified, but here's why: With your database query already done: $query = "SELECT wings FROM table WHERE order = $order_num"; $result = mysql_query($query); $array = mysql_fetch_assoc($result); By adding one line, and editing another, the field will select the option that was stored in the database. $values = range(0, 10); echo "<select name=\"my_field\">\n"; foreach($values as $v) { $selected = $array['wings'] == $v ? 'selected="selected"' : ''; // Compare value of the option to value from DB. Select the option if they're equal. echo "<option value=\"$v\" $selected>$v</option>\n"; // ADDED $selected } echo "</select>\n"; If it was hard coded in the html, it would need to look like this: <select name="my_field"> <option value="0" <?php echo $array['wings'] == 0 ? 'selected="selected"' : '' ?>>0</option> <option value="1" <?php echo $array['wings'] == 1 ? 'selected="selected"' : '' ?>>1</option> <option value="2" <?php echo $array['wings'] == 2 ? 'selected="selected"' : '' ?>>2</option> <option value="3" <?php echo $array['wings'] == 3 ? 'selected="selected"' : '' ?>>3</option> <option value="4" <?php echo $array['wings'] == 4 ? 'selected="selected"' : '' ?>>4</option> <option value="5" <?php echo $array['wings'] == 5 ? 'selected="selected"' : '' ?>>5</option> <option value="6" <?php echo $array['wings'] == 6 ? 'selected="selected"' : '' ?>>6</option> <option value="7" <?php echo $array['wings'] == 7 ? 'selected="selected"' : '' ?>>7</option> <option value="8" <?php echo $array['wings'] == 8 ? 'selected="selected"' : '' ?>>8</option> <option value="9" <?php echo $array['wings'] == 9 ? 'selected="selected"' : '' ?>>9</option> <option value="10" <?php echo $array['wings'] == 10 ? 'selected="selected"' : '' ?>>10</option> </select> Edited to fix syntax error. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320200 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 Now I'm BEGINNING to see it! And UNDERSTAND what you mean. Is the code that Scootstah offered earlier applicable as a simpler means? Meanwhile, let me hardcode ONE selection just to see that it works. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320205 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 I had a syntax error in the last code block above, so if you try to cut and paste it, you may end up with an error. Just fixed it, though. Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320207 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 Pasted it in with my code from Reply #4. I have a feeling I'm duplicating ECHOs because I received error message for first line of modified code. Also, I need to ensure that unselected items maintain their default. Is there a way to incorporate that? Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320216 Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 There are MANY, but let's work with CUTLETS. Then by modifying my first response, this should work: $result = mysql_query("SELECT * FROM pass WHERE id = 4 "); $row = mysql_fetch_assoc($result); $selected = $row['cutlets']; $options = array( '0.00' => '---', '1.00' => '1', '2.00' => '2', '3.00' => '3' ); foreach($options as $key => $val) { echo '<option value="' . $key . '" ' . ($key == $selected ? 'selected="selected"' : '') . '>' . $val . '</option>'; } Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320217 Share on other sites More sharing options...
phppup Posted February 22, 2012 Author Share Posted February 22, 2012 So am I abandoning Pikachu?? I modified: echo " <option value='1.00'>1 <?php echo $array['roastturkey'] == 0 ? 'selected="selected"' : ''>0</option>"; To this echo "<option value='1.00' $array['roastturkey'] == 0 ? 'selected="selected"' : ''>0</option>"; BUT NOT WORKING! Quote Link to comment https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/#findComment-1320221 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.