tomasd Posted May 21, 2007 Share Posted May 21, 2007 Hi, I've got a following code: <?php $stack = Array ( 1 => 'Accounting', 2 => 'Banqueting', 3 => 'Executive Office', 4 => 'Front Office', 5 => 'Housekeeping', 6 => 'Human Resources', 7 => 'Kitchen', 8 => 'Loss Prevention', 9 => 'Mini Bar', 10 => 'Repairs & Maintenance', 11 => 'Revenue', 12 => 'Room Service', 13 => 'Sales & Marketing' ); $select = '<select name="user_department" id="user_department" size="1">'."\are\n"; foreach($stack as $key => $value) { $select .= "\t".'<option value="'.$key.'">' . $value.'</option>'."\are\n"; } $select .= '</select>'; echo $select; ?> Which generates me following html output: <select name="user_department" id="user_department" size="1"> <option value="1">Accounting</option> <option value="2">Banqueting</option> <option value="3">Executive Office</option> <option value="4">Front Office</option> <option value="5">Housekeeping</option> <option value="6">Human Resources</option> <option value="7">Kitchen</option> <option value="8">Loss Prevention</option> <option value="9">Mini Bar</option> <option value="10">Repairs & Maintenance</option> <option value="11">Revenue</option> <option value="12">Room Service</option> <option value="13">Sales & Marketing</option> </select> I need to introduce a variable which would be equal to some int 1 to 13 which would make a selection so for instance if int = 4 I would be getting following html output: <select name="user_department" id="user_department" size="1"> <option value="1">Accounting</option> <option value="2">Banqueting</option> <option value="3">Executive Office</option> <option selected value="4">Front Office</option> <option value="5">Housekeeping</option> <option value="6">Human Resources</option> <option value="7">Kitchen</option> <option value="8">Loss Prevention</option> <option value="9">Mini Bar</option> <option value="10">Repairs & Maintenance</option> <option value="11">Revenue</option> <option value="12">Room Service</option> <option value="13">Sales & Marketing</option> </select> How do I go about making that work? Thanks ever so much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/52345-solved-please-help-to-generate-html-select-from-an-array/ Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 what about <?php $SelectedID = 4; $select = '<select name="user_department" id="user_department" size="1">'."\are\n"; echo '<option value="'.$SelectedID.'">' . $stack[$SelectedID].'</option>'."\are\n"; foreach($stack as $key => $value) { if($key != $SelectedID) { $select .= "\t".'<option value="'.$key.'">' . $value.'</option>'."\are\n"; } } $select .= '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52345-solved-please-help-to-generate-html-select-from-an-array/#findComment-258296 Share on other sites More sharing options...
tomasd Posted May 21, 2007 Author Share Posted May 21, 2007 Thanks for quick response (as usual:)) following code: <?php $stack = Array ( 1 => 'Accounting', 2 => 'Banqueting', 3 => 'Executive Office', 4 => 'Front Office', 5 => 'Housekeeping', 6 => 'Human Resources', 7 => 'Kitchen', 8 => 'Loss Prevention', 9 => 'Mini Bar', 10 => 'Repairs & Maintenance', 11 => 'Revenue', 12 => 'Room Service', 13 => 'Sales & Marketing' ); $SelectedID = 4; $select = '<select name="user_department" id="user_department" size="1">'."\n"; echo '<option selected value="'.$SelectedID.'">' . $stack[$SelectedID].'</option>'."\n"; foreach($stack as $key => $value) { if($key != $SelectedID) { $select .= "\t".'<option value="'.$key.'">' . $value.'</option>'."\n"; } } $select .= '</select>'; echo $select; ?> generates the following html: <option selected value="4">Front Office</option> <select name="user_department" id="user_department" size="1"> <option value="1">Accounting</option> <option value="2">Banqueting</option> <option value="3">Executive Office</option> <option value="5">Housekeeping</option> <option value="6">Human Resources</option> <option value="7">Kitchen</option> <option value="8">Loss Prevention</option> <option value="9">Mini Bar</option> <option value="10">Repairs & Maintenance</option> <option value="11">Revenue</option> <option value="12">Room Service</option> <option value="13">Sales & Marketing</option> </select> It looks like <option selected value="4">Front Office</option> needs to be moved down, also is there any way of placing it in the 4th place in dropdown list oposed to 1st place? Cheers! Tomas Quote Link to comment https://forums.phpfreaks.com/topic/52345-solved-please-help-to-generate-html-select-from-an-array/#findComment-258306 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 Try this (slighlty more advanced) <?php $stack = Array ( 1 => 'Accounting', 2 => 'Banqueting', 3 => 'Executive Office', 4 => 'Front Office', 5 => 'Housekeeping', 6 => 'Human Resources', 7 => 'Kitchen', 8 => 'Loss Prevention', 9 => 'Mini Bar', 10 => 'Repairs & Maintenance', 11 => 'Revenue', 12 => 'Room Service', 13 => 'Sales & Marketing' ); $SelectedID = 4; $select = '<select name="user_department" id="user_department" size="1">'."\n"; foreach($stack as $key => $value) { $Selected = ($key == $SelectedID)?"selected":""; $select .= "\t".'<option $Selected value="'.$key.'">' . $value.'</option>'."\n"; } $select .= '</select>'; echo $select; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52345-solved-please-help-to-generate-html-select-from-an-array/#findComment-258308 Share on other sites More sharing options...
tomasd Posted May 21, 2007 Author Share Posted May 21, 2007 perfect it works just fine! I don't suppose there is a way the ID=4 would make "selected" appear in the 4th place instead of 1st or there is? Quote Link to comment https://forums.phpfreaks.com/topic/52345-solved-please-help-to-generate-html-select-from-an-array/#findComment-258314 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 Oppp edited the one above.. updated Try this (slighlty more advanced) <?php $stack = Array ( 1 => 'Accounting', 2 => 'Banqueting', 3 => 'Executive Office', 4 => 'Front Office', 5 => 'Housekeeping', 6 => 'Human Resources', 7 => 'Kitchen', 8 => 'Loss Prevention', 9 => 'Mini Bar', 10 => 'Repairs & Maintenance', 11 => 'Revenue', 12 => 'Room Service', 13 => 'Sales & Marketing' ); $SelectedID = 4; $select = '<select name="user_department" id="user_department" size="1">'."\n"; foreach($stack as $key => $value) { $Selected = ($key == $SelectedID)?"selected":""; $select .= "\t".'<option $Selected value="'.$key.'">' . $value.'</option>'."\n"; } $select .= '</select>'; echo $select; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52345-solved-please-help-to-generate-html-select-from-an-array/#findComment-258321 Share on other sites More sharing options...
tomasd Posted May 21, 2007 Author Share Posted May 21, 2007 they're all gone selected now <select name="user_department" id="user_department" size="1"> <option $Selected value="1">Accounting</option> <option $Selected value="2">Banqueting</option> <option $Selected value="3">Executive Office</option> <option $Selected value="4">Front Office</option> <option $Selected value="5">Housekeeping</option> <option $Selected value="6">Human Resources</option> <option $Selected value="7">Kitchen</option> <option $Selected value="8">Loss Prevention</option> <option $Selected value="9">Mini Bar</option> <option $Selected value="10">Repairs & Maintenance</option> <option $Selected value="11">Revenue</option> <option $Selected value="12">Room Service</option> <option $Selected value="13">Sales & Marketing</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/52345-solved-please-help-to-generate-html-select-from-an-array/#findComment-258327 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 oops single quote <?php $stack = Array ( 1 => 'Accounting', 2 => 'Banqueting', 3 => 'Executive Office', 4 => 'Front Office', 5 => 'Housekeeping', 6 => 'Human Resources', 7 => 'Kitchen', 8 => 'Loss Prevention', 9 => 'Mini Bar', 10 => 'Repairs & Maintenance', 11 => 'Revenue', 12 => 'Room Service', 13 => 'Sales & Marketing' ); $SelectedID = 4; $select = '<select name="user_department" id="user_department" size="1">'."\n"; foreach($stack as $key => $value) { $Selected = ($key == $SelectedID)?"selected":""; $select .= "\t".'<option '.$Selected.' value="'.$key.'">' . $value.'</option>'."\n"; } $select .= '</select>'; echo $select; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52345-solved-please-help-to-generate-html-select-from-an-array/#findComment-258333 Share on other sites More sharing options...
tomasd Posted May 21, 2007 Author Share Posted May 21, 2007 AWESOME! Thanks again!!! Quote Link to comment https://forums.phpfreaks.com/topic/52345-solved-please-help-to-generate-html-select-from-an-array/#findComment-258339 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.