Jump to content

[SOLVED] Multiselect menu population


mikenl

Recommended Posts

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'); 

Link to comment
https://forums.phpfreaks.com/topic/111487-solved-multiselect-menu-population/
Share on other sites

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>";
   }
}

}

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.

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?

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.

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.