Jump to content

[SOLVED] Enum & dropdown values


Shamrox

Recommended Posts

I have a select form field (dropdown) that lists values that are enumerated in my database table. I now don't want one of the values to be selectable, but if an old record is selected for viewing and it has this value already saved, i'd like it to show in the dropdown. I just don't want any new records to be able to be given this value.

Possible?

If so, how would I adjust my current code shown below? Thanks.

 

<select name=registrationstatus  size=1   onChange="setChangedIndicator('hide');" >
                    <option value='Unknown'<?PHP echo (@$regstatus == "Unknown")?" selected=\"selected\"":""; ?>>Unknown</option>
                    <option value='Quote'<?PHP echo (@$regstatus == "Quote")?" selected=\"selected\"":""; ?>>Quote</option>
                    <option value='Enrolled'<?PHP echo (@$regstatus == "Enrolled")?" selected=\"selected\"":""; ?>>Enrolled</option>
                    <option value='Pending'<?PHP echo (@$regstatus == "Pending")?" selected=\"selected\"":""; ?>>Pending</option>
                    <option value='Cancelled Student'<?PHP echo (@$regstatus == "Cancelled Student")?" selected=\"selected\"":""; ?>>Cancelled Student</option>
                    <option value='Cancelled Vendor'<?PHP echo (@$regstatus == "Cancelled Vendor")?" selected=\"selected\"":""; ?>>Cancelled Vendor</option>
                    <option value='Rescheduled'<?PHP echo (@$regstatus == "Rescheduled")?" selected=\"selected\"":""; ?>>Rescheduled
                </select>

Link to comment
Share on other sites

It's much easier to put the options in an array and loop through them....

 

$options = array(
'Unknown',
'Quote',
'Enrolled',
'Pending',
'Cancelled Student',
'Cancelled Vendor',
'Rescheduled'
);

$html = '
<select name=registrationstatus  size=1   onChange="setChangedIndicator(\'hide\');" >';

foreach ($options as $value) {
	$html .= '
		<option value="' . $value . '" ' . ($value == $regstatus ? 'selected="selected"' : '') . '>' . $value . '</option>';

	if (!in_array($regstatus, $options)) {
		$html .= '
			<option value="' . $regstatus . '" selected="selected">' . $regstatus . '</option>';
	}
}

$html .= '
</select>';

Link to comment
Share on other sites

You just need a condition that checks whether the record is 'old' or not and output accordingly...

 

<?php if ($record == 'old') : ?>
<option value='Rescheduled'<?PHP echo (@$regstatus == "Rescheduled")?" selected=\"selected\"":""; ?>>Rescheduled
<?php  endif; ?>

 

There's a disabled attribute for the option tag, but IE doesn't seem to support it. If you wanted to show it without it being selectable, I think you'd need to search for a javascript solution.

Link to comment
Share on other sites

		if (!in_array($regstatus, $options)) {
		$html .= '
			<option value="' . $regstatus . '" selected="selected">' . $regstatus . '</option>';
	}

 

If the value isn't in the array that contains the current values, then it specifically adds that one to the drop down using the above if statement...hence the use of an array...it makes checking to see if it exists easier.

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.