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:

Array
(
[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";
$result=mysql_query($qry);
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 ;-)

Member Avatar for diafol

Try

$selected = explode("\r",$FieldValue);

instead of

$selected = explode('\r',$FieldValue);

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

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

Member Avatar for diafol

@ pzuurveen:

check his code - he does use the backslash, but encapsulated in single quotes.

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

Member Avatar for Rkeast

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>";
print_r($selected);
echo "</pre>";

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

Array
(
[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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.