Populate Drop Down, Radio Buttons or Checkboxes with a CSV File

You can easily popoulate your Drop Down, Radio Buttons as well as Checkboxes fields using a CSV file without having to enter each choice one by one by yourself. This will help you save a lot of time if you have a large list of options. Below you can find a step by step tutorial on how to populate your fields.

  1. What is a CSV file?
  2. Create the CSV file
  3. Upload CSV file to your server
  4. Create your field
  5. Add the PHP Snippet

1. What is a CSV file?

CSV stands for comma separated value. A CSV or .csv file is a delimited text file that uses a comma to separate values. CSV files can be imported to and exported from programs that store data in tables, such as Microsoft Excel or Google Sheets. For example, let's say you had a spreadsheet containing the following data.

Label Value Calc Value
Mars 5 500
Jupiter 50 250
Earth 15 60
Moon 35 380

The above data could be represented in a CSV-formatted file as follows:

Mars,5,500
Jupiter,50,250
Earth,15,60
Moon,35,380

Here, the fields of data in each row are delimited with a comma and individual rows are separated by a newline.

2. Create the CSV file

First of all, you need to create a CSV file if you have not yet created one. This CSV file needs to be comma-separated and have the following format:

Label,Value,Calculation Value

Label: Label of the item

Value: Value of the item

Calculation Value: Value used in the calculations of the item

Value and Calculation Value are optional and may not be included in the CSV.

Example containing only labels:

Option 1
Option 2
Option 3

Example containing labels and values:

Option 1,5
Option 2,10
Option 3,15

Example containing labels, values and calculation values:

Option 1,5,50
Option 2,10,100
Option 3,15,150

3. Upload CSV file to your server

After you have created your CSV file, its now ready to be uploaded to your server. Log into your administrator panel > Content > Media > upload your CSV file or use a FTP software of your choice to upload the CSV file to your desired location.

4. Create your field

This field can either be a dropdown, radio buttons or checkboxes.

One you have created your field, set the Field Name.

4. Add the PHP Snippet

Copy the following PHP snippet and paste it in your form > Behavior > PHP Scripts > Form Prepare

// Enter the Field Name of the field you created above
$choices_field_name = 'dropdown';

// Enter path to CSV file starting from your Joomla root directory
$file_path = 'media/csv/cf_data.csv';

// Do not edit below
$file_path = JPATH_SITE . '/' . ltrim($file_path, '/');
if (!\JFile::exists($file_path))
{
	return;
}

if (!$contents = file_get_contents($file_path))
{
	return;
}

// remove previous choices
unset($form['fields'][$choices_field_name]['choices']['choices']);

// add CSV choices
foreach(preg_split("/((\r?\n)|(\r\n?))/", $contents) as $line)
{
	$data = explode(',', $line);

	$label = isset($data[0]) ? $data[0] : false;
	if (!$label)
	{
		continue;
	}
	
	$form['fields'][$choices_field_name]['choices']['choices'][] = [
		'label' => $label,
		'value' => isset($data[1]) ? $data[1] : '',
		'calc-value' => isset($data[2]) ? $data[2] : ''
	];
}

Finally, you need to set which Field will be populated as well as define the CSV file path. To do so, in the above code you copy/pasted, set the Field Name as well as the path to the CSV file in the following variables:

// Enter the Field Name of the field you created above
$choices_field_name = 'dropdown';

// Enter path to CSV file starting from your Joomla root directory
$file_path = 'media/csv/cf_data.csv';

Note: $file_path needs to start from the root directory of your Joomla installation.

Last updated on Nov 26th 2024 12:11