Welcome Guest, Not a member yet? Register   Sign In
Transform query ( of query builder in query model
#1

(This post was last modified: 10-24-2021, 06:50 AM by pippuccio76.)

, sorry for english , i have this query :

PHP Code:
            if(!empty($post['data_da'])){

                $data_da date_local_format_to_iso($post['data_da']);
            
            
}else{

                $data_da ='2000/01/01';
            }

            if(!empty($post['data_a'])){

                $data_a date_local_format_to_iso($post['data_a']);
            
            
}else{

                $data_a ='2100/01/01';
            }


            $query_creata "SELECT * from spese WHERE data>='$data_da' and data<='$data_a' ";


            if(!empty($post['id_tipo_spesa'])){

                $id_tipo_spesa $post['id_tipo_spesa'];
                
                $query_creata 
.= " AND id_tipo_spesa= $id_tipo_spesa";
            }
                
            
if(!empty($post['id_tipo_pagamento'])){

                $id_tipo_pagamento $post['id_tipo_pagamento'];
                
                $query_creata 
.= " AND id_tipo_pagamento= $id_tipo_pagamento";
                           

                
            
if(!empty($post['id_tipo_pagamento'])){

                $id_tipo_pagamento $post['id_tipo_pagamento'];
                
                $query_creata 
.= " AND id_tipo_pagamento= $id_tipo_pagamento";
            }
                





            $query 
$db->query($query_creata);
            $spese $query->getResult(); 


this is a little part
PHP Code:
$spese $spese_model->where('data_da>=',$data_da)
                                ->where('data_a<=',$data_a)
                                ->findAll(); 


how can i insert conditional ?
Reply
#2

Solved in this way :

PHP Code:
if(!empty($post['data_da'])){

                $data_da date_local_format_to_iso($post['data_da']);
            
            
}else{

                $data_da ='2000/01/01';
            }

            if(!empty($post['data_a'])){

                $data_a date_local_format_to_iso($post['data_a']);
            
            
}else{

                $data_a ='2100/01/01';
            }

            $array_query=[];

            if(!empty($post['id_tipo_spesa'])){

                $id_tipo_spesa $post['id_tipo_spesa'];
                
                $array_query 
['id_tipo_spesa']    $id_tipo_spesa;
            }
                
            
if(!empty($post['id_tipo_pagamento'])){

                $id_tipo_pagamento $post['id_tipo_pagamento'];
                
                $array_query 
["id_tipo_pagamento"]  =  $id_tipo_pagamento;
            }
                

                
            
if(!empty($post['id_autisti_interni'])){

                $id_autisti_interni $post['id_autisti_interni'];
                
                $array_query 
["id_autisti_interni"] =  $id_autisti_interni;
            }
                                
            
if(!empty($post['id_mezzi'])){

                $id_mezzi $post['id_mezzi'];
                
                $array_query 
["id_mezzi"] =  $id_mezzi;
            }
                

            
if(!count($array_query)){
                $array_query="id!=0";
            }
                


            $spese 
$spese_model->where('data>=',$data_da)
                                ->where('data<=',$data_a)
                                ->where($array_query)
                                ->findAll();


            
            
//log_message('debug',$spese_model->getLastQuery());
            $record_totali =$spese_model->countAllResults(); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB