HtRadios
This library can be used to generate radio buttons in an easy way.
I recommend you take a look at the documentation of its parent classes in order to grasp all the inherited functionality:, (*1)
Installation
Via composer:, (*2)
composer require flsouto/htradios
Usage
In the following example we generate a choice list with three options., (*3)
<?php
require_once('vendor/autoload.php');
use FlSouto\HtRadios;
$select = new HtRadios("id_category");
$select->options([1=>'Category1',2=>'Category2',3=>'Category3']);
echo $select;
Outputs:, (*4)
Notice: the options method also accepts other formats besides an associative array. Take a look at the documentation of the HtChoice class in order to learn more., (*5)
Changing the separator
By default, the separator used to separate the options is a <br/> element, that is, a line break.
But you can change that by using the separator method. In the example below we change the separator to be two spaces so that the options are displayed horizontally:, (*6)
<?php
require_once('vendor/autoload.php');
use FlSouto\HtRadios;
$select = new HtRadios("id_category");
$select->options([1=>'Category1',2=>'Category2',3=>'Category3'])
->separator(" ");
echo $select;
Outputs:, (*7)
Selecting an option
If you have read the documentation of the HtField and the HtWidget parent classes you already know that you are supposed
to use the context method in order to set the value of a field/widget:, (*8)
<?php
require_once('vendor/autoload.php');
use FlSouto\HtRadios;
$select = new HtRadios('id_category');
$select->options([1=>'Category1',2=>'Category2',3=>'Category3']);
$select->context(['id_category'=>2]);
echo $select;
Outputs:, (*9)