Login   Register  
PHP Classes
elePHPant
Icontem

File: docs/subqueries.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Temperini Mirko  >  APIQL  >  docs/subqueries.php  >  Download  
File: docs/subqueries.php
Role: Example script
Content type: text/plain
Description: How to use subqueries
Class: APIQL
Define and run commands of a data query language
Author: By
Last change:
Date: 2011-09-03 10:44
Size: 2,573 bytes
 

Contents

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

html_header('Subqueries 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){
    global \$actors;
    \$collection=\$actors;
    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 (isset(\$sql['first']))
        ?array_shift(\$out)
        :\$out;
    }
"
);


echo 
"register the query model and bind it to a custom function";
showcode("apiql::register('!select/?first/!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/?first/!actor/!with[json]','actors_find');

//my binding funciton
function actors_find($sql){
    global 
$actors;
    foreach(
$actors 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 (isset(
$sql['first']))
        ?
array_shift($out)
        :
$out;
    }



echo 
'execute query with 1 subquery';
showcode("\$finded=apiql::query('select actor with apiql::query(\"select actor with {name:robert}\")')");
$finded=apiql::query('select actor with apiql::query("select first actor with {name:robert}")');
showresults($finded);

echo 
'<hr />';

echo 
'execute query with 1 subquery and a subquery in the subquery .... omg :-)';
showcode("\$finded=apiql::query('
    select actor with 
        apiql::query(\"
            select first actor with
                apiql::query(\'select first actor with {name:robert}\')
            \")
        ');
    "
);
$finded=apiql::query('
    select actor with 
        apiql::query("
            select first actor with
                apiql::query(\'select first actor with {name:robert}\')
            ")
        '
);
showresults($finded);


html_footer();
?>