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

}

Link to comment
Share on other sites

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...

 

???

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.