PHP code example of flsouto / htchoice

1. Go to this page and download the library: Download flsouto/htchoice library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

flsouto / htchoice example snippets




use FlSouto\HtChoice;

class SimpleChoice extends HtChoice{

	// Question to be asked on 'writable' mode
	protected $simpleChoiceQuestion = '';

	function __construct($name, $question='Type in the desired option:'){
		parent::__construct($name);
		$this->simpleChoiceQuestion = $question;
	}

	// Generates the options for our custom widget
	private function renderSimpleChoiceOptions(){
		// always call resolveOptions before accessing $this->options
		$this->resolveOptions();
		$input = $this->value();
		foreach($this->options as $value => $label){
			if($value === $label){
				// if value and label are equal, simplify option output
				$line = "- $value";
			} else {
				$line = "$value - $label";
			}
			if($this->compareOptionsValues($value,$input)){
				// make selected option bold
				$line = "<b>$line (selected)</b>";
			}
			echo $line;
			echo "<br/>\n";
		}
	}

	// Show options + input dialog
	function renderWritable(){
		$attrs = $this->attrs;
		$attrs['value'] = $this->value();
		$attrs['size'] = 10;
		echo "$this->simpleChoiceQuestion \n";
		echo "<input $attrs /> <br/> \n";
		$this->renderSimpleChoiceOptions();
	}

	// show only list of options, without input dialog
	function renderReadonly(){
		$this->renderSimpleChoiceOptions();
	}

}



ice = new SimpleChoice('color');
$choice->options([
	1 => 'Black',
	2 => 'White',
	3 => 'Gray'
]);

echo $choice;


ice = new SimpleChoice('season');
$choice->options([
	1 => 'Spring',
	2 => 'Summer',
	3 => 'Fall',
	4 => 'Winter'
]);
$choice->context(['season'=>3]); // selects season 3 (Fall)

echo $choice;


ice = new SimpleChoice('season');
$choice->options([
	1 => 'Spring',
	2 => 'Summer',
	3 => 'Fall',
	4 => 'Winter'
]);
$choice->context(['season'=>3])
	->readonly(true);

echo $choice;


ice = new SimpleChoice("language","Type in the desired language code:");
$choice->options(['en','es','pt','fr']);

echo $choice;


ice = new SimpleChoice("category");
$choice->options([
	['id'=>1,'name'=>'Action'],
	['id'=>2,'name'=>'Drama'],
	['id'=>3,'name'=>'Sci-fi']
])->context(['category'=>2]); // selects category 2

echo $choice;


ice = new SimpleChoice("category");

$option1 = new StdClass();
$option1->id = 1;
$option1->name = 'Action';

$option2 = new StdClass();
$option2->id = 2;
$option2->name = 'Drama';

$choice->options([$option1, $option2])
	->context(['category'=>2]); // selects category 2

echo $choice;


ice = new SimpleChoice("category");
$choice->options([[1,'Action'],[2,'Drama'],[3,'Sci-fi']]);

echo $choice;


ice = new SimpleChoice("category");
$choice->options(function(){
	// pretend this was fetched from the db
	$rows = [
		['id'=>1,'name'=>'Action'],
		['id'=>2,'name'=>'Drama'],
		['id'=>3,'name'=>'Sci-fi']
	];

	return $rows;

})->context(['category'=>3]); // selects category 3

echo $choice;


ice = new SimpleChoice("category");
$choice->options(function(){
	// return a function which returns an associative array:
	return function(){
		return [1=>'Category A',2=>'Category B',3=>'Category C'];
	};
});

$choice->context(['category'=>2]); // selects category 2

echo $choice;