mikenl Posted June 23, 2008 Share Posted June 23, 2008 Hi, I made a function that takes names for a multiselect menu from an array and checks if there is a db entry for it, and lists the entry as selected. What I can't get to work is the selected part... the names are being listed properly. $rows is query result which checks the db... function loadform2($values,$session_profset,$rows,$fieldname){ foreach ($values as $key => $value){ if(!empty($key)){ if ($rows[0][$fieldname] == 3) echo " selected=\"selected\""; echo " <option>$values[$key]</option>"; } } } This is how I call the function: $names= array(1 => "test1","test2","test3"); loadform2($names,$session_profset,$rows,'names'); Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/ Share on other sites More sharing options...
dmccabe Posted June 23, 2008 Share Posted June 23, 2008 dont think you need the "selected=" part just echo "selected" Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-572169 Share on other sites More sharing options...
mikenl Posted June 23, 2008 Author Share Posted June 23, 2008 apparently selected="selected" is the proper HTML. I can change it to SELECTED but it doesn't make a difference... the problem must be in how the $rows values are handled Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-572173 Share on other sites More sharing options...
.josh Posted June 23, 2008 Share Posted June 23, 2008 hmmm... - echo $rows[0][$fieldname] inside your function see if it has your expected value. - I suspect you probably wanted to put $key as the 1d element: $rows[$key][$fieldname] because otherwise I don't really see the point of that condition being inside the loop like that. - After all that, your code should be causing " selected=\"selected\" to be physically written on your screen at some point in time, because it's not being echoed inside option tags. Based on your provided script, here is my guess: function loadform2($values,$session_profset,$rows,$fieldname){ foreach ($values as $key => $value){ if(!empty($key)){ echo "<option "; if ($rows[$key][$fieldname] == 3) echo " selected=\"selected\""; echo ">$values[$key]</option>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-572246 Share on other sites More sharing options...
mikenl Posted June 23, 2008 Author Share Posted June 23, 2008 I tried to play with the $key and when I use this...: if ($rows[0][$key][$fieldname].$key == 1) echo " <option>++++++testing++++++</option>"; ...it shows me ++++++testing++++++ on the first place in the menu, with 3 leading to 3rd place etc... ??? Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-572316 Share on other sites More sharing options...
lemmin Posted June 23, 2008 Share Posted June 23, 2008 apparently selected="selected" is the proper HTML. I can change it to SELECTED but it doesn't make a difference... the problem must be in how the $rows values are handled Actually, selected is a boolean property so the proper HTML would be selected="true"; however, when properties are boolean, just having them in the tag sets them to true, so dmccabe is right. Of course, the string "selected" evaluates to true, so you are right in saying that it doesn't make a difference. Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-572339 Share on other sites More sharing options...
.josh Posted June 23, 2008 Share Posted June 23, 2008 I tried to play with the $key and when I use this...: if ($rows[0][$key][$fieldname].$key == 1) echo " <option>++++++testing++++++</option>"; ...it shows me ++++++testing++++++ on the first place in the menu, with 3 leading to 3rd place etc... ??? umm. so is $rows now a 3d array? Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-572350 Share on other sites More sharing options...
mikenl Posted June 23, 2008 Author Share Posted June 23, 2008 there are only 2 arrays: $values and $rows. $rows I get by left joining a table. $values is set in the HTML as an array. I have a similar function for select menus that aren't using this left joined table: function loadform($values,$session_profset,$rows,$fieldname){ foreach ($values as $key => $value){ echo "<option value='". sprintf("%1d",$key) ."'"; if ($key==$rows[0][$fieldname]) echo " selected=\"selected\""; echo ">$values[$key]</option>\n"; } } This works perfectly. It shows a list of country names (from $values), with the ones coming from the db query ($rows) selected. Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-572372 Share on other sites More sharing options...
.josh Posted June 23, 2008 Share Posted June 23, 2008 in that one line piece of code you just posted you have $rows[0][$key][$fieldname] that's a 3d array as in 3 dimensional. Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-572382 Share on other sites More sharing options...
mikenl Posted June 23, 2008 Author Share Posted June 23, 2008 hmmmm... obviously I'm not good with arrays, but it works I added [$fieldname] to get the values from that row in the db for that select menu. Does this help? Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-572395 Share on other sites More sharing options...
mikenl Posted June 24, 2008 Author Share Posted June 24, 2008 Can anyone help me with the problem of how to retrieve rows from a db and set them as selected? I have been trying for the last 2 days and cannot get it to work I guess for an experienced coder this must be a piece of cake? Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-573227 Share on other sites More sharing options...
mikenl Posted June 24, 2008 Author Share Posted June 24, 2008 OK, brainwave: foreach ($values as $key => $value){ if(!empty($key)){ echo "<input name=\"$fieldname"."[]"."\" type=\"checkbox\" value=\"$key\""; if ($rows[0][$fieldname.$key] == 1) echo "checked"; echo "/>$values[$key]<br />"; } } This populates the scrollable checkboxlist with the values from $values (countrynames) and checks the ones that are stored in the db ($rows). I hope someone can use this Quote Link to comment https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/#findComment-573239 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.