PHP code example of solarismedia / select2input

1. Go to this page and download the library: Download solarismedia/select2input 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/ */

    

solarismedia / select2input example snippets


$form = new Form;
$form->addSelect2('client_id', $clientRepository, 'Klient:')
	->setRequired('Vyberte klienta!')
	->setResultsPerPage(15);
$form->addSelect2Multiple('clients', $clientRepository, 'Klienti:')

namespace App\Model\Repository;

use TomasKarlik\Select2Input\ISelect2DataSource;
use TomasKarlik\Select2Input\Select2ResultEntity;


class ClientRepository implements ISelect2DataSource
{

	/**
	 * @param string $query
	 * @param int $limit
	 * @param int $offset
	 * @return Select2ResultEntity[]
	 */
	public function searchTerm(string $query, int $limit, int $offset): array
	{
		$return = [];
		$selection = $this->getClientTable()
			->where(
				'company ILIKE ?', '%' . $query . '%'
			)
			->order('company')
			->select('client.id, client.company')
			->limit($limit, $offset);

		while ($row = $selection->fetch()) {
			$result = new Select2ResultEntity;
			$result->setId($row->id);
			$result->setText($row->company);
			$return[] = $result;
		}
		return $return;
	}


	/**
	 * @param string $query
	 * @return int
	 */
	public function searchTermCount(string $query): int
	{
		return $this->getClientTable()
			->where(
				'company ILIKE ?', '%' . $query . '%'
			)
			->count('*');
	}


	/**
	 * @param mixed $key
	 * @return Select2ResultEntity|NULL
	 */
	public function findByKey($key): ?Select2ResultEntity
	{
		if ( ! is_numeric($key)) {
			return NULL;
		}

		$client = $this->getClientTable()->wherePrimary($key)->fetch();
		if ( ! $client) {
			return NULL;
		}

		$result = new Select2ResultEntity;
		$result->setId($client->id);
		$result->setText($client->company);
		return $result;
	}

}