Alex Sykes dot com

Occasionally, there is the need to create a set of radio button options which are individual for each record. For example, A shoe-shop lists many different shoes which all have the common features of manufacturer, stylename, size, colour and so on. However, some shoes may be available in brown, black and grey - while other shoes may be black, white, grey or red. Obviously, we only wish to offer customers the colour choices which we can supply, so the use of a Custom Field would be appropriate here. The challenge is to create a script which will get the colour choices from the chosen shoe database entry.

The flow

Initially, we need to add an extra field to our Shoe Admin View which will store the choices for each shoe. To make this as simple as possible, the field should contain the available colours in a set of comma-separated strings. That is, something like 'Black,White,Green,Red'. A different shoe style may have 'Black, Tan' as its choices. Once we have these stored, they can be pulled from the database for a either a single shoe or a set of shoes - with each shoe having its own colours. These can then be listed to show available colours or used to create a Custom Field radio button set.

 The Least we should do…

    …is to create a simple text field with an appropriate name - say, colours - and our Admin user adds the available colours separated by commas. Advantage - quick, simple, versatlie as there is no restriction on colours. Disadvantage - none, really. If you chose this method, it's safe to ignore the next section and go here

The Restricted choice…

    …is to create a checkbox set of ALL possible choices. Advantage - more control over what the Admin user can offer. Disadvantage - more complex, cannot accommodate 'unusual' choices. A checkbox set could be defined using a Checkboxes field type

<field 
	type="checkboxes" 
	name="colourchoices" 
	label="Select Colours" 
	option="Black,Brown,White,Green,Blue,Red,Grey,Tan,Silver,Dark Red,Light Tan,Dark Tan" 
	default="" 
	description="Select the available colours" 
	required="" 
	class="" 
	showon="" 
/>

The first step in solving this problem is to create a new Field - which I named colourchoices and labelled Select Colours - to include every possible choice, although only a sample is included here. The XML field definition would be:

Following compilation, the user could enter the choices above and after saving to the database, the data in the colourchoices field would be:

{"0":"white","1":"green","2":"blue","3":"red"}

The next stage is to extract the colour data from this and convert it into the comma-separated string, then store it in the colours field. The code which does this is added to

// Remove leading and trailing braces
$tmp = rtrim($colourchoices,'}');  
$tmp = ltrim($tmp,'{');

// Replace quotes with commas
$tmp = str_replace('"','',$tmp);

// Remove any slashes
$tmp = stripslashes($tmp);

// Remove indices - numbers followed by a colon
$tmp = preg_replace('(\d+:)', '', $tmp);

// Create and sort new array from string 
$colourArray = explode(',', $tmp);	
sort($colourArray);

// Convert Array to comma-separated string
$colours = implode($colourArray,",");
$data['colours'] = $colours;

Incomplete - more to come