I've built a function which builds options for a SELECT FIELD. It draws options from a list table and then appends the SELECTED when the value that I am looping through in present in the $selected ARRAY.

In my $FieldValue, multiple values are delineated by a "\r". I use and Implode("/r", The Field) when saving it in to the DB. But when I EXPLODE() the value in this function, it does not create the ARRAY properly. With the debugging lines I've added, it looks like this:

[0] => After Pastor
Appreciative Inquiry
Boundary Training

Here is the function:

function Get_Menu_Selected ($vType, $FieldValue) {

$options .= "<option value=\"\"></option>\n";
$qry = "select `vValue` FROM `vLists` where `vType`='$vType' ORDER BY `Sort` ASC";
echo "fieldvalue ".nl2br($FieldValue);
$selected = explode('\r',$FieldValue);
	echo "<pre>";
	print_r ($selected);
	echo "</pre>";
while($row=mysql_fetch_array($result)) {
$inst = $row['vValue']; 
  $sel = '';
  if ( in_array($inst,$selected) ) {
	$sel = 'selected="selected " ';
  $options .= "<option {$sel}value=\"{$inst}\">{$inst}</option>\n";
} return $options;

Why is the the array not being built properly? I am hoping I am missing something simple.

Thanks ;-)

7 Years
Discussion Span
Last Post by Rkeast

you said that you used Implode("/r", The Field) to create the Db

you should have used Implode("\r", The Field)


i'm just say he should check the routine that put the data into the db (he didn't list it)


I am stumped by this one, and I think we'll need some more information...

I tried testing your method with this code:

$field_array = array(test, test2, test3);
$field = implode('\r', $field_array);

echo "fieldvalue ".nl2br($field);
$selected = explode('\r', $field);

echo "<pre>";
echo "</pre>";

And it gives the desired output:
fieldvalue test\rtest2\rtest3

[0] => test
[1] => test2
[2] => test3

Perhaps if you show us the code where you build the $FieldValue, or show an example of the $FieldValue you're passing in.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.