Option List for Sitemagic CMS
The option list, also know as a drop down menu, is a simple control for selecting one or multiple elements from a collection of items.
Using the option list only requires a few lines of code, as shown below. The code uses the
SMOptionList class.
// Create list and add options
$list = new SMOptionList("UniqueId");
$list->AddOption(new SMOptionListItem("UniqueId1", "", ""));
$list->AddOption(new SMOptionListItem("UniqueId2", "I like red", "Red"));
$list->AddOption(new SMOptionListItem("UniqueId3", "I like green", "Green"));
$list->AddOption(new SMOptionListItem("UniqueId4", "I like blue", "Blue"));
// Have option list trigger a post back (page submit) when an item is selected
$list->SetAutoPostBack(true);
// Do something if an option was selected and it was not the empty (first) item
if ($list->GetSelectedValue() !== null && $list->GetSelectedValue() !== "")
// Do something here..
// Render the HTML code for the option list
$output = $list->Render();
Working with multi selection
Allowing for multiple selections is simply a matter of enabling the feature as shown below.
$list->SetAttribute(SMOptionListAttribute::$Multiple, "multiple");
if ($list->GetSelectedValue() !== null)
{
// GetSelectedValue() now returns a semi colon (;) separated list of values
$vals = $list->GetSelectedValue();
$values = explode(";", $vals); // Optionally turn selections into array
}