jim.davidson Posted June 7, 2007 Share Posted June 7, 2007 I'm using php dreamweaver 8 and mySQL 4.1.21 I'm teaching myself php and need some help with arrays and drop down menus I've built and array containing state ids and state names. Example: [1]Alabama [2]Alaska [3]Arizona [4]Arkansas [5]California and so on. I have this section of code to build a dropdown menu from a record set, works fine. <select name="listStates" id="listStates"> <option value="">Select a state</option> <?php do { ?> <option value="<?php echo $row_getStates['state_id']?>"><?php echo $row_getStates['state_name']?></option> <?php } while ($row_getStates = mysql_fetch_assoc($getStates)); $rows = mysql_num_rows($getStates); if($rows > 0) { mysql_data_seek($getStates, 0); $row_getStates = mysql_fetch_assoc($getStates); } ?> </select I now want to build the same menu with the array I get this far and I'm stumped. How do I define my array elements in the foreach loop? <select name="listStatesArray" id="listStatesArray"> <option value="">Select a state</option> <?php foreach ($arrayStates as $statesMenu) { ?> <option value="<?php echo $statesMenu (need ID here)?>"><?php echo $statesMenu (need name here)?></option> <?php } ?> </select> Any help will be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/54592-solved-need-guidence-with-dropdown-menu-and-arrays/ Share on other sites More sharing options...
Psycho Posted June 7, 2007 Share Posted June 7, 2007 <select name="listStatesArray" id="listStatesArray"> <option value="">Select a state</option> <?php foreach ($arrayStates as $stateID => $stateValue) { echo "<option value=\"$stateID\">$stateValue</option>"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/54592-solved-need-guidence-with-dropdown-menu-and-arrays/#findComment-269987 Share on other sites More sharing options...
jim.davidson Posted June 7, 2007 Author Share Posted June 7, 2007 That's what I needed. Thank You Quote Link to comment https://forums.phpfreaks.com/topic/54592-solved-need-guidence-with-dropdown-menu-and-arrays/#findComment-270133 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.