0

I am fetching data from a table using group by payment method. What happen is in group by the data comes up corresponding to those data only which are in table . But I want all payment types to be shown in result . I am using following query.

 $query = "select  paytype ,sum(totalamount)  from `tbl_payments` group by paytype";

There are 5(cash,credit,paypal..) types of payment . I want all the payment methods with the result , so that if paytype is not find by query result it should be appended like ['credit'] =>[0] , means paytype => amount(which should be 0 at that case).

Please help me on this.

2
  • Please show us full code. Commented May 14, 2015 at 6:51
  • 1
    A schema may help aswell Commented May 14, 2015 at 7:06

3 Answers 3

1

You didn't tell about the table paytype containing the list of valid payment methods.

To get all the payment methods in the result, you must use a left join. Something like :

select pt.paytype, sum(p.totalamount)
from `tbl_paytypes` pt
left join `tbl_payments` p 
on pt.paytype = p.paytype 
group by pt.paytype
Sign up to request clarification or add additional context in comments.

Comments

0

Get all payment types using a Left Join with the paymettypes tables

Comments

0

Best way is to use entity class, ex.

class PaymentTypes{  private $cash;  private $credit;  private $paypal;  ....

//constructor

//setters and getters }

in constructor you can set a default parameters for the fields if you dont get any values

public function __construct($array){   $someDefaultValue = '';   if(!empty($array['name_of_column_in_databaseTable'])){   $this->$cash
= $array['name_of_column_in_databaseTable'];  }else{   $this->$cash = $someDefaultValue; } ... }

Result from database is like array(array(), array(), ...)

so you can make:

foreach($result as $key=>$value){
 $paymentTypes[] = new PaymentTypes($value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.