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("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;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.