Php Mysqli, как сложить два разных условия результата - PullRequest
0 голосов
/ 13 сентября 2018

Запрос ниже работает, но он отлично показывает две разные суммы разных ордеров, как я сделал GROUP BY ORDER_ID как (1000, 2000).

Но я хочу это (1000, 2000) как (3000). Если в неделю 2 заказа, то 1 имеет процентную скидку, а один - без скидки, и каждый заказ имеет разную стоимость доставки.

Каждый заказ имеет различный процент скидки и разную стоимость доставки. Вот почему я сделал group by order_id в своем запросе, и если я удаляю group by order_id, это даст мне другую общую сумму. Но с group by order_id он показывает правильное количество заказов на текущую неделю, месяц или год.

Пожалуйста, помогите в этом отношении. Ниже мой вопрос, что я пытаюсь сделать.

$totalX = "select sum(price) as price, coupon, city, delivery_type, order_id
from orders where date between '2018-09-12' and '2018-09-13' group by order_id";
$totalXx = $dba2->query($totalX);
while ($total = $totalXx->fetch_assoc()) {

    $couponX = "select coupon, price, percent from couponsAll where id = '".$total['coupon']."'";
    $couponXx = $dba->query($couponX);
    $coupon = $couponXx->fetch_assoc();

    $areaBX = "select * from countries_citiesALL where id = '".$total['city']."' ";
    $areaBXx = $dba->query($areaBX);
    $areaBXxx = $areaBXx->fetch_assoc();

    $area6X = "select * from delivery_typeALL where area = '".$total['city']."' and id = '".$total['delivery_type']."'";
    $area6Xx = $dba->query($area6X);
    $area6xXx = $area6Xx->fetch_assoc();
    $dchargezQ = $area6xXx['price'];

    if ($coupon['price'] >= 1 && $coupon['percent'] < 1) {
        /// this condition is if price based discount
        $priceAcoup = $total['price'] - $coupon['price'];
        $gtotalx = $priceAcoup + $areaBXxx['price'] + $dchargezQ;
        $gtotal = number_format($gtotalx, 3);
        echo '<font color="black" style=""><b>'.$gtotal.'</b></font>';
    } else {
        if ($coupon['price'] < 1 && $coupon['percent'] >= 1) {
            /// this condition is if percentage based discount
            $priceAcoup = $total['price'] - (($total['price'] * $coupon['percent']) / 100);
            $gtotalx = $priceAcoup + $areaBXxx['price'] + $dchargezQ;
            $gtotal = number_format($gtotalx, 3);
            echo '<font color="black" style=""><b>'.$gtotal.'</b></font>';
        } else {
            /// this condition is if there is no percentage or price based discount
            $gtotalx = $total['price'] + $areaBXxx['price'] + $dchargezQ;
            $gtotal = number_format($gtotalx, 3);
            echo '<font color="black" style=""><b>'.$gtotal.'</b></font>';
        }
    }
}

1 Ответ

0 голосов
/ 15 сентября 2018

Проблема скважины решена, как указано ниже.

Я изменил $weekTotal[] при каждом условии с помощью $weekTotal1[], $weekTotal2[], $weekTotal3[] и поместил $weekTotal1=array();, $weekTotal2=array();, $weekTotal3=array(); над 2-м циклом запроса, и он заработал.

Спасибо за вашу помощь.

$__xz_rTorders1     = $dba2->query("SELECT count(distinct(order_id)) as count, order_id, date FROM orders_confirmed
group by WEEK(date), MONTH(date)
order by WEEK(date), MONTH(date)
");
while($__xz_rTordersa4 = $__xz_rTorders1->fetch_assoc()) {

$__xz_signupweek = $__xz_rTordersa4['date'];
/*start day*/
for($__xz_i = 0; $__xz_i <7 ; $__xz_i++) {
 $__xz_date = date('Y-m-d', strtotime("-".$__xz_i."days", strtotime($__xz_signupweek)));
 $__xz_dayName = date('D', strtotime($__xz_date));
 if($__xz_dayName == "Sun") {
//echo "<b>From:</b> ". date( "d/m/Y", strtotime($__xz_date))." / ";
$mstart_date=date( "Y-m-d", strtotime($__xz_date));
 }
}
/*end day*/
 for($__xz_i = 0; $__xz_i <7 ; $__xz_i++) {
 $__xz_date = date('Y-m-d', strtotime("+".$__xz_i."days", strtotime($__xz_signupweek)));
 $__xz_dayName = date('D', strtotime($__xz_date));
 if($__xz_dayName == "Sat") {
//echo "<b>To:</b> ". date( "d/m/Y", strtotime($__xz_date));
$mend_date=date( "Y-m-d", strtotime($__xz_date));
 }
 }

///////////////////***********///////////////////
$weekTotal1=array();
$weekTotal2=array();
$weekTotal3=array();
///////////////////***********///////////////////

$totalX= "select sum(price) as price, coupon, city, delivery_type, order_id
from orders_confirmed where date between '".$mstart_date."' and '".$mend_date."'
group by order_id
";
$totalXx= $dba2->query($totalX);
while ($total = $totalXx->fetch_assoc()){

$couponX= "select coupon, price, percent
from coupons
where id = '".$total['coupon']."'
";
$couponXx= $dba->query($couponX);
$coupon = $couponXx->fetch_assoc();

$areaBX= "select * from countries_cities
where id = '".$total['city']."' ";
$areaBXx= $dba->query($areaBX);
$areaBXxx= $areaBXx->fetch_assoc();

$area6X= "select * from delivery_type
where area = '".$total['city']."'
and id = '".$total['delivery_type']."'
";
$area6Xx= $dba->query($area6X);
$area6xXx= $area6Xx->fetch_assoc();
$dchargezQ= $area6xXx['price'];


if ($coupon['price'] >= 1 && $coupon['percent'] < 1) {
$priceAcoup1=$total['price']-$coupon['price'];
///////////////////***********///////////////////
$weekTotal1[] = $priceAcoup1+$areaBXxx['price']+$dchargezQ;
///////////////////***********///////////////////

}else if ($coupon['price'] < 1 && $coupon['percent'] >= 1) {
$priceAcoup2=$total['price']-(($total['price']*$coupon['percent'])/100);

///////////////////***********///////////////////
$weekTotal2[] = $priceAcoup2+$areaBXxx['price']+$dchargezQ;
///////////////////***********///////////////////

}else{
///////////////////***********///////////////////
$weekTotal3[] = $total['price']+$areaBXxx['price']+$dchargezQ;
///////////////////***********///////////////////

} 

} // End Of While Loop (2nd)
$weekTotal_val3 =  array_sum($weekTotal1);
$weekTotal_val4 =  array_sum($weekTotal2);
$weekTotal_val5 =  array_sum($weekTotal3);

$finalTOTAL=number_format($weekTotal_val3+$weekTotal_val4+$weekTotal_val5,3); 
echo '<font color="red" style=""><b>'.$finalTOTAL.'</b></font>';
} // End Of While Loop (1st)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...