ActaNonVerba1 Posted February 9, 2011 Share Posted February 9, 2011 Hey guys. Me again... Essentially what i am doing is pulling data from a MySQL database about the number of thumbnails on a page. The user can then change this using a <select> dropdown menu. How ever, i want the <select> to default to the amount already specified by the Database. I know i can do this by inserting a Selected attribute to one of the <options> but what is the best way of doing this? Heres my code.. $NumberOfThumbnails = mysql_result($data, 0,"NumberOfThumbnails"); <select name="numberofthumbnails"> <option value="0">None</option> <option value="2">2</option> <option value="4">4</option> <option value="6">6</option> <option value="8">8</option> <option value="10">10</option> <option value="12">12</option> <option value="14">14</option> <option value="16">16</option> <option value="18">18</option> <option value="20">20</option> <option value="22">22</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> </select> Thanks - Danny Quote Link to comment https://forums.phpfreaks.com/topic/227212-changing-default-select-position-due-to-mysql-result/ Share on other sites More sharing options...
Psycho Posted February 9, 2011 Share Posted February 9, 2011 <?php $selectedNoOfThumbs = mysql_result($data, 0,"NumberOfThumbnails"); $thumbOptionsList = array(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 25, 26, 27, 28); $thumbOptionsHTML = ''; foreach($thumbOptionsList as $thumbNo) { $selected = ($thumbNo==$selectedNoOfThumbs) ? ' selected="selected"' : ''; $thumbOptionsHTML .= " <option value=\"{$thumbNo}\"{$selected}>{$thumbNo}</option>\n"; } ?> <select name="numberofthumbnails"> <?php echo $thumbOptionsHTML; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/227212-changing-default-select-position-due-to-mysql-result/#findComment-1172048 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.