Local Binding

This article shows how to bind Kendo UI DropDownList for PHP to a PHP array. Local binding means that filtering operations happen on the client side.

Approaches

This PHP array can be populated from a database or declared inline (in the page).

Bind to PDO-Returned Arrays

PHP Data Objects (PDO) is an interface for accessing various databases in PHP.

Below are listed the steps for you to follow when binding the Kendo UI DropDownList for PHP to an array returned by PDO.

Important

The following demo is using the sample SQLite database shipped with the Telerik UI for PHP demos (/wrappers/php/sample.db).

Step 1 Make sure you followed all the steps from the introductory article on Telerik UI for PHP—include the autoloader, JavaScript, and CSS files.

Step 2 Create a PDO connection.

    <?php
    $db = new PDO('sqlite:../sample.db');
    ?>

Step 3 Retrieve all records from the Products table.

    <?php
    $statement = $db->prepare('SELECT * FROM Products');
    $products = $statement->fetchAll(PDO::FETCH_ASSOC);
    ?>

Step 4 Create a DataSource and set its data and schema. Setting the schema is required to specify the model fields. These fields are required for filtering.

    <?php
    // Create the schema model
    $model = new \Kendo\Data\DataSourceSchemaModel();

    // Create a field for the 'ProductName' column of the 'Products' table
    $productNameField = new \Kendo\Data\DataSourceSchemaModelField('ProductName');
    $productNameField->type('string');

    // Create a field for the 'ProductID' column of the 'Products' table
    $productIDField = new \Kendo\Data\DataSourceSchemaModelField('ProductID');
    $productIDField->type('number');

    $model->addField($productNameField, $productIDField);

    // Create the schema
    $schema = new \Kendo\Data\DataSourceSchema();

    // Set its model
    $schema->model($model)

    // Create the data source
    $dataSource = new \Kendo\Data\DataSource();

    // Specify the schema and data
    $dataSource->data($products)
               ->schema($schema);
    ?>

Step 5 Create a DropDownList, configure its dataTextField and dataValueField options and set its dataSource.

    <?php
    $dropdownlist = new \Kendo\UI\DropDownList('DropDownList');
    $dropdownlist->dataSource($dataSource);
    $dropdownlist->dataTextField('ProductName');
    $dropdownlist->dataValueField('ProductID');
    ?>

Step 6 Output the DropDownList by echoing the result of the render method.

    <?php
    echo $dropdownlist->render();
    ?>

Use DataSourceResult Helpers

The DataSourceResult class is a helper utility on top of PDO which simplifies common CRUD data operations. It is distributed with the Telerik UI for PHP demos and can be found in the /wrappers/php/lib/ directory of the Telerik UI for PHP distribution.

Step 1 Make sure you followed all the steps from the introductory article on Telerik UI for PHP—include the autoloader, JavaScript, and CSS files.

Step 2 Copy /wrappers/php/lib/DataSourceResult.php to your website root and include it.

    <?php require_once 'lib/DataSourceResult.php'; ?>

Step 3 Create a new instance of the DataSourceResult and use its read method to retrieve data from the database.

    <?php
    // The constructor accepts the PDO DSN for the target database
    $result = new DataSourceResult('sqlite:../sample.db');

    // The 'read' method accepts table name and array of columns to select.
    $data = $result->read('Products', array('ProductName'));
    // The result of the 'read' method is an array with two elements 'data' and 'total'.
    ?>

Step 4 Configure a DataSource and schema.

    <?php
    // Create the schema model
    $model = new \Kendo\Data\DataSourceSchemaModel();

    // Create a field for the 'ProductName' column of the 'Products' table
    $productNameField = new \Kendo\Data\DataSourceSchemaModelField('ProductName');
    $productNameField->type('string');

    // Create a field for the 'ProductID' column of the 'Products' table
    $productIDField = new \Kendo\Data\DataSourceSchemaModelField('ProductID');
    $productIDField->type('number');

    $model->addField($productNameField, $productIDField);

    // Create the schema
    $schema = new \Kendo\Data\DataSourceSchema();

    // Set its model and describe the data format.
    $schema->model($model)
           ->data('data')
           ->total('total');

    // Create the data source
    $dataSource = new \Kendo\Data\DataSource();

    // Specify the schema and data
    $dataSource->data($data)
               ->schema($schema);
    ?>

Step 5 Create an DropDownList, configure its dataTextField, dataValueField and set its dataSource.

    <?php
    $dropdownlist = new \Kendo\UI\DropDownList('DropDownList');
    $dropdownlist->dataSource($dataSource);
    $dropdownlist->dataTextField('ProductName');
    $dropdownlist->dataValueField('ProductID');
    ?>

Step 6 Output the DropDownList by echoing the result of the render method.

    <?php
    echo $dropdownlist->render();
    ?>

See Also

In this article