Returning value as selected for <option value>

Hi All,

I need help bit confused on the best way.

i have some options on a form that i want to be selected when the page loads depending on database value but i also want it to return all the other values for the col.

i had this idea.

function status ($status)
{
	if ($status == $status)
	{
		$line = '<option value="'.$status.'" selected>'.$status.'</option>';
	} else {
		$line = '<option value="'.$status.'">'.$status.'</option>';
	}
	return $line;
}

problem being it does not return all the other values.

will i have to query the database each time to return each distinct value of the col to generate the rest of the options?

Yes, you’ll need to do a query to return each of the options that you want to populate the list with.

Note that in the function above you’re comparing $status and $status, which will not end well. You could make it less complex using the ternary operator:

echo '<option value="' . $status . '"' . ($status == $defaultstatus ? ' selected' : ' ') . '> ' . $status . '</option>';

where $status is the value that comes from your query, and $defaultstatus is the one you need pre-selected.

thank you :slight_smile: i will give this ago :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.