PHP Classes

File: docs/base.php

Recommend this page to a friend!
  Classes of Temperini Mirko   APIQL   docs/base.php   Download  
File: docs/base.php
Role: Example script
Content type: text/plain
Description: The base class usage
Class: APIQL
Define and run commands of a data query language
Author: By
Last change:
Date: 12 years ago
Size: 2,374 bytes
 

Contents

Class file image Download
<?php
include 'functions.php';

html_header('Base Example');



echo
'Same example data';
showcode("
\$actors=array(
    array('name'=>'robert','surname'=>'de niro','films'=>100),
    array('name'=>'silvester','surname'=>'stallone','films'=>70),
    array('name'=>'julia','surname'=>'roberts','films'=>40)
    );
"
);



echo
'A function to bind our query';
showcode("
function actors_find(\$sql,\$collection){
    foreach(\$collection as \$item){
        \$checks=array();
        foreach(\$sql['with'] as \$k=>\$v){
            \$checks[\$k]=(\$item[\$k] == \$v)?true:false;
            }
        if(!in_array(false,\$checks)) \$out[]=\$item;
        }
    return \$out;
    }
"
);


echo
"register the query model and bind it to a custom function";
showcode("apiql::register('!select/!actor/!with[json]','actors_find');");


echo
'<hr />';

$actors=array(
    array(
'name'=>'robert','surname'=>'de niro','films'=>100),
    array(
'name'=>'silvester','surname'=>'stallone','films'=>70),
    array(
'name'=>'julia','surname'=>'roberts','films'=>40)
    );

//register query model
apiql::register('!select/!actor/!with[json]','actors_find');

//my binding funciton
function actors_find($sql,$collection){
    foreach(
$collection as $item){
       
$checks=array();
        foreach(
$sql['with'] as $k=>$v){
           
$checks[$k]=($item[$k] == $v)?true:false;
            }
        if(!
in_array(false,$checks)) $out[]=$item;
        }
    return
$out;
    }



echo
'Execute the query passing the actors array as collection (an extra argument)';
showcode("\$finded=apiql::query('select actor with {name:robert}',\$actors);");
$finded=apiql::query('select actor with {name:robert}',$actors);
showresults($finded);
echo
'<hr />';


echo
'Execute again the query passing the actors array as collection (an extra argument)';
showcode("\$finded=apiql::query('select actor with {films:40}',\$actors);");
$finded=apiql::query('select actor with {films:40}',$actors);
showresults($finded);
echo
'<hr />';

echo
'Execute the query passing actors as collection but query error ( \'actors\' instead of \'actor\' )';
showcode("\$finded=apiql::query('select actors with {films:40}',\$actors);");
$finded=apiql::query('select actors with {films:40}',$actors);
showresults($finded);

html_footer();
?>