PHP code example of ngekoding / codeigniter-api-query-parser

1. Go to this page and download the library: Download ngekoding/codeigniter-api-query-parser 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/ */

    

ngekoding / codeigniter-api-query-parser example snippets


// CodeIgniter 3 Example

// Get a query builder
// Please note: we don't need to call ->get() here
$queryBuilder = $this->db->select('p.*, c.name category')
                    ->from('posts p')
                    ->join('categories c', 'c.id=p.category_id');

/**
 * The first parameter is the query builder instance
 * and the second is the codeigniter version (3 or 4) 
 */
$queryParser = new \Ngekoding\CodeIgniterApiQueryParser\QueryParser($queryBuilder);
$result = $queryParser->applyParams(); // done

print_r($result);

// CodeIgniter 4 Example

$db = db_connect();
$queryBuilder = $db->from('posts p')
                   ->select('p.*, c.name category')
                   ->join('categories c', 'c.id=p.category_id');

$queryParser = new \Ngekoding\CodeIgniterApiQueryParser\QueryParser($queryBuilder);
$result = $queryParser->applyParams(); // done

print_r($result);

$queryParser = new \Ngekoding\CodeIgniterApiQueryParser\QueryParser($queryBuilder);

// Tell that the `id` is `p.id` (posts table id)
$queryParser->addColumnAlias('id', 'p.id');

// Or add multiple aliases at once
$queryParser->addColumnAlias([
    'id' => 'p.id',
    'title' => 'p.title'
]);

$result = $queryParser->applyParams(); // done