473,472 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sql query in php put in a function

1 New Member
I have a lot of queries with a bit different from each other for example the first query is same to second query except I add a single line of where statement to it.
Expand|Select|Wrap|Line Numbers
  1. if($ut=='Admin'){ // if the user is Admin the query should not have a filter
  2.             $queryCustomer = Yii::app()->db->createCommand()
  3.                 ->select('cust.mobile_number as customer,sum(trans.price) as sales,trans.datetime')
  4.                  ->from('customers cust')
  5.                 ->join('transactions trans', 'cust.id=trans.customer_id')
  6.                 ->where('transaction_type_id=:id', array(':id'=>1))    
  7.                 ->group('customer_id')
  8.                 ->order('trans.datetime DESC')
  9.                 ->queryAll();
  10.                  $per_customer=$this->renderTableCustomer($queryCustomer); 
  11.         } else { // if the user is not Admin the query should have a filter
  12.             $queryCustomer = Yii::app()->db->createCommand()
  13.                 ->select('cust.mobile_number as customer,sum(trans.price) as sales,trans.datetime')
  14.                  ->from('customers cust')
  15.                 ->join('transactions trans', 'cust.id=trans.customer_id')
  16.                 ->where('transaction_type_id=:id', array(':id'=>1))
  17.                 ->andWhere('user_id=:userID',array(':userID'=>$filter))        
  18.                 ->group('customer_id')
  19.                 ->order('trans.datetime DESC')
  20.                 ->queryAll();
  21.                  $per_customer=$this->renderTableCustomer($queryCustomer);
  22.         }  
  23.  
Since the query is same, is there a possibility to put it on a function and when I call it I will add the where statement as a parameter?

For example. I put the query to function.
Expand|Select|Wrap|Line Numbers
  1. public function actionSample($queryCustomer){
  2.         $queryCustomer = Yii::app()->db->createCommand()
  3.                 ->select('cust.mobile_number as customer,sum(trans.price) as sales,trans.datetime')
  4.                  ->from('customers cust')
  5.                 ->join('transactions trans', 'cust.id=trans.customer_id')
  6.                 ->where('transaction_type_id=:id', array(':id'=>1))    
  7.                 ->group('customer_id')
  8.                 ->order('trans.datetime DESC')
  9.                 ->queryAll();
  10.  
  11.            return $queryCustomer;
  12.  
  13.  
  14.     }
then if the user is not admin I will add this line of code
Expand|Select|Wrap|Line Numbers
  1. ->andWhere('user_id=:userID',array(':userID'=>$filter))
This is the code what I've done so far.
Expand|Select|Wrap|Line Numbers
  1.   if($ut=='Admin'){ // if the user is Admin the query should not have a filter
  2. $results=$this->actionSample($queryCustomer);
  3. $per_day="$this->renderTable($results);
  4. else{
  5. $addWhere="->andWhere('user_id=:userID',array(':userID'=>$filter))";
  6. $results=$this->actionSample($queryCustomer,$addWhere);
  7. $per_day="$this->renderTable($results);
  8. }
The else code doesn't work. What is my mistake? By the way I am using Yii framework. please help! THanks in advance
Nov 3 '14 #1
0 1232

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: george young | last post by:
I recently found myself writing something like: def get_connection(): if tcp_conn(): if server_allows_conn(): return 'good_conn' else: return 'bad_auth' else: return 'no_server'
2
by: | last post by:
I'd want to make a custom class that will generate breadcrumb navigation for my site via a recursive query, e.g. Home > Page1 > Subpage 1. I've structured my database with hierarchical parent/child...
1
by: kevin | last post by:
I thought that the parse query feature in Query Analyzer simply wrapped your statement in a TRAN and then issued a ROLLBACK statement to prevent making real changes... but i just had a query parse...
7
by: Cyrax1033 | last post by:
I have a database that is fully operational (thanks to everyone's help) now I'm doing some housekeeping. What I'm wanting to change is one query function; I have a form that performs a search based...
4
by: Jeigh | last post by:
Been using PHP for a little while now but have never really looked into using functions and such thinking I didn't really need them (coupled with me be lazy). I've started a new project and have...
5
by: dantebothermy | last post by:
I'm looking for a function that is similar to one in excel: =max(,) I know I can write it using an iif statement, but I'm looking for an easier approach. Any ideas? Thanks, Dante
8
by: kujito | last post by:
I'm running weighted averages on ore grades for mine resource estimates. I got the wtd. avg. to come back correctly, but I only need the results out to 3 decimal places instead of the 15 it spit...
5
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php...
3
by: nicolenwn | last post by:
Hello all, Many thanks in advance again for your expertise. I need help in Microsoft Access VBA once again ): I'd like to inquire if it is indeed possible to create a button that when it is...
3
yosiro
by: yosiro | last post by:
Here's my code in query design (column FIELD) Date of Birt: IIf(IsNull(); IIf(IsNull(); ); IIf(IsNull(); ; & ", " & )) The result is "Alabama, 2/12/2000"
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.