ttmt_101 Posted July 11, 2013 Share Posted July 11, 2013 Hi all I want to create a simple html select menu from an array like: <select> <option value="">Red</option> <option value="">Green</option> <option value="">Blue</option> </select> I know I can output this like: $my_Arr = array('Red','Green','Blue'); echo "<select>"; for($i=0; $i<count($my_Arr); $i++){ echo"<option>".$my_Arr[$i]."</option>"; } echo "</select>"; But I would like to add the select menu to a variable and use later. So I can start the variable off like: $select_menu = "<select>"; But how can I add the rest of the html code to the variable as it's created, so I end up with: $select_menu = "<select> <option value="">Red</option> <option value="">Green</option> <option value="">Blue</option> </select>" Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted July 11, 2013 Solution Share Posted July 11, 2013 Append to the string, like $select_menu = "<select>"; $select_menu = $select_menu . "<option value=''>Red</option>"; $select_menu .= "<option value=''>Green</option>"; // .= is shorthand for the above $select_menu .= "<option value=''>Blue</option>"; $select_menu .= "</select>"; Quote Link to comment Share on other sites More sharing options...
ttmt_101 Posted July 11, 2013 Author Share Posted July 11, 2013 Thanks Still alive I've been doing '+=' for the last 30 mins instead of '.=' Quote Link to comment Share on other sites More sharing options...
web_craftsman Posted July 11, 2013 Share Posted July 11, 2013 There is also a very easy-to-use foreach construction. Quote Link to comment 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.