PHP code example of asanusi007 / ci4-select2

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

    

asanusi007 / ci4-select2 example snippets



  namespace App\Controllers;

  use App\Libraries\Select2;

  class Example extends BaseController {}

  ...
    public function ajax_select2()
    {
      $options = array(
        'table'=> 'table_name', // name of your table
        'id' => 'id', // id of your table field and will be assigned to value attribute of select option <option value="{id}">{text}/option>
        'text' => 'text', // text of your table field and will be assigned to text of select option
        'additional'=> ['field1','field2'..] // additional data will be assigned to data-* attribute <option data-field="{field}" value="{id}">{text}/option> 
      );

      $select2 = new Select2($options);

      echo json_encode($select2->render());
    }