<?php
include '../config.php';

$data = array();
$QBD_arr = array();
$QBD1_arr = array();
$QBDOVERALL_arr = array();
$QBD_count = array(1,2,3,4);
$QBDY_arr = array();
$QBDY_count = array(1);
$QBDX_arr = array();
$QBDX_count = array(1,2);
$yes_no_array = array(1);
$QBDX4_count = array(1,2,3,4,5,6,7);
$QBDX4_arr = array();
$QBDX3_count = array(1,2,3);
$QBDX3_arr = array();
$QBD_label_arr = array();
$QBDOVERALL_label_arr = array();
$QBDY_label_arr = array();
$QBDX_label_arr = array();
$QBDX4_label_arr = array();
$QBDX3_label_arr = array();

$QBD_b = '';
$QBD_m = '';
$QBD_p = '';
$QBD5_b = '';
$QBD5_m = '';
$QBD5_p = '';
$QBDY_b = '';
$QBDY_t = '';
$QBDX_b = '';
$QBDX_t = '';
$QBDX4_b = '';
$QBDX4_t = '';
$QBDX3_b = '';
$QBDX3_t = '';

$condX = $_REQUEST['trendcondX'];
$conX = $_REQUEST['conX'];
$oemVal = $_REQUEST['oemVal'];
$timeVal = $_REQUEST['timeVal'];

if($oemVal==0)
{
    $brand_C = $brand;
    $column_name = 'Q13b';
}
else
{
    $brand_C = $model[$oemVal-1];
    $column_name = 'Q13';
}

if($conX==1)
{    
    $condX="1=1";   
}
else
{

}

foreach($timeVal as $key7 => $value7)
{
    foreach($brand_C as $key=> $value)
    {
        if($oemVal==0)
        {
            if($key==9)
            {
                foreach($QBD_count as $key2 => $value2)
                {
                    $QBD_b = $QBD_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QBD1_'.$value2.'` != "" AND `Quarter` = '.$value7.' then record end) as T_QBD1_'.$value2.'_b'.$key.'_'.$value7.',';
                    $QBD_m = $QBD_m.' avg(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QBD1_'.$value2.'` != "" AND `T_QBD1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) AND `Quarter` = '.$value7.' then T_QBD1_'.$value2.' end) as T_QBD1_'.$value2.'_m'.$key.'_'.$value7.',';
                    $QBD_p = $QBD_p.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QBD1_'.$value2.'` IN (9,10) AND `Quarter` = '.$value7.' then record end) as T_QBD1_'.$value2.'_p'.$key.'_'.$value7.',';
                }
    
                $QBD5_b = $QBD5_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBD5` != "" AND `Quarter` = '.$value7.' then record end) as QBD5_b'.$key.'_'.$value7.',';
                $QBD5_m = $QBD5_m.' avg(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBD5` != "" AND `QBD5` IN (1,2,3,4,5,6,7,8,9,10) AND `Quarter` = '.$value7.' then QBD5 end) as QBD5_m'.$key.'_'.$value7.',';
                $QBD5_p = $QBD5_p.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBD5` IN (9,10) AND `Quarter` = '.$value7.' then record end) as QBD5_p'.$key.'_'.$value7.','; 
    
                foreach($QBDY_count as $key3 => $value3)
                {
                    $QBDY_b = $QBDY_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDY'.$value3.'` != "" AND `Quarter` = '.$value7.' then record end) as QBDY'.$value3.'_b'.$key.'_'.$value7.',';
                    foreach($yes_no_array as $key4 => $value4)
                    {
                        $QBDY_t = $QBDY_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDY'.$value3.'` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDY'.$value3.'_t'.$key.'_'.$value7.',';
                    }
                }
    
                foreach($QBDX_count as $key3 => $value3)
                {
                    $QBDX_b = $QBDX_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX'.$value3.'` != "" AND `Quarter` = '.$value7.' then record end) as QBDX'.$value3.'_b'.$key.'_'.$value7.',';
                    foreach($yes_no_array as $key4 => $value4)
                    {
                        $QBDX_t = $QBDX_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX'.$value3.'` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX'.$value3.'_t'.$key.'_'.$value7.',';
                    }
                }
    
                $QBDX4_b = $QBDX4_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX4` != "" AND `Quarter` = '.$value7.' then record end) as QBDX4_b'.$key.'_'.$value7.',';
                foreach($QBDX4_count as $key4 => $value4)
                {
                    $QBDX4_t = $QBDX4_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX4` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX4_'.$value4.'_t'.$key.'_'.$value7.',';
                }
    
                $QBDX3_b = $QBDX3_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX3` != "" AND `Quarter` = '.$value7.' then record end) as QBDX3_b'.$key.'_'.$value7.',';
                foreach($QBDX3_count as $key4 => $value4)
                {
                    $QBDX3_t = $QBDX3_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX3` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX3_'.$value4.'_t'.$key.'_'.$value7.',';
                }
            }
            else if($key==10)
            {
                foreach($QBD_count as $key2 => $value2)
                {
                    $QBD_b = $QBD_b.' count(case when '.$column_name.' in (4,5) AND `T_QBD1_'.$value2.'` != "" AND `Quarter` = '.$value7.' then record end) as T_QBD1_'.$value2.'_b'.$key.'_'.$value7.',';
                    $QBD_m = $QBD_m.' avg(case when '.$column_name.' in (4,5) AND `T_QBD1_'.$value2.'` != "" AND `T_QBD1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) AND `Quarter` = '.$value7.' then T_QBD1_'.$value2.' end) as T_QBD1_'.$value2.'_m'.$key.'_'.$value7.',';
                    $QBD_p = $QBD_p.' count(case when '.$column_name.' in (4,5) AND `T_QBD1_'.$value2.'` IN (9,10) AND `Quarter` = '.$value7.' then record end) as T_QBD1_'.$value2.'_p'.$key.'_'.$value7.',';
                }
    
                $QBD5_b = $QBD5_b.' count(case when '.$column_name.' in (4,5) AND `QBD5` != "" AND `Quarter` = '.$value7.' then record end) as QBD5_b'.$key.'_'.$value7.',';
                $QBD5_m = $QBD5_m.' avg(case when '.$column_name.' in (4,5) AND `QBD5` != "" AND `QBD5` IN (1,2,3,4,5,6,7,8,9,10) AND `Quarter` = '.$value7.' then QBD5 end) as QBD5_m'.$key.'_'.$value7.',';
                $QBD5_p = $QBD5_p.' count(case when '.$column_name.' in (4,5) AND `QBD5` IN (9,10) AND `Quarter` = '.$value7.' then record end) as QBD5_p'.$key.'_'.$value7.','; 
    
                foreach($QBDY_count as $key3 => $value3)
                {
                    $QBDY_b = $QBDY_b.' count(case when '.$column_name.' in (4,5) AND `QBDY'.$value3.'` != "" AND `Quarter` = '.$value7.' then record end) as QBDY'.$value3.'_b'.$key.'_'.$value7.',';
                    foreach($yes_no_array as $key4 => $value4)
                    {
                        $QBDY_t = $QBDY_t.' count(case when '.$column_name.' in (4,5) AND `QBDY'.$value3.'` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDY'.$value3.'_t'.$key.'_'.$value7.',';
                    }
                }
    
                foreach($QBDX_count as $key3 => $value3)
                {
                    $QBDX_b = $QBDX_b.' count(case when '.$column_name.' in (4,5) AND `QBDX'.$value3.'` != "" AND `Quarter` = '.$value7.' then record end) as QBDX'.$value3.'_b'.$key.'_'.$value7.',';
                    foreach($yes_no_array as $key4 => $value4)
                    {
                        $QBDX_t = $QBDX_t.' count(case when '.$column_name.' in (4,5) AND `QBDX'.$value3.'` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX'.$value3.'_t'.$key.'_'.$value7.',';
                    }
                }
    
                $QBDX4_b = $QBDX4_b.' count(case when '.$column_name.' in (4,5) AND `QBDX4` != "" AND `Quarter` = '.$value7.' then record end) as QBDX4_b'.$key.'_'.$value7.',';
                foreach($QBDX4_count as $key4 => $value4)
                {
                    $QBDX4_t = $QBDX4_t.' count(case when '.$column_name.' in (4,5) AND `QBDX4` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX4_'.$value4.'_t'.$key.'_'.$value7.',';
                }
    
                $QBDX3_b = $QBDX3_b.' count(case when '.$column_name.' in (4,5) AND `QBDX3` != "" AND `Quarter` = '.$value7.' then record end) as QBDX3_b'.$key.'_'.$value7.',';
                foreach($QBDX3_count as $key4 => $value4)
                {
                    $QBDX3_t = $QBDX3_t.' count(case when '.$column_name.' in (4,5) AND `QBDX3` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX3_'.$value4.'_t'.$key.'_'.$value7.',';
                }
            }
            else
            {
                foreach($QBD_count as $key2 => $value2)
                {
                    $QBD_b = $QBD_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` != "" AND `Quarter` = '.$value7.' then record end) as T_QBD1_'.$value2.'_b'.$key.'_'.$value7.',';
                    $QBD_m = $QBD_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` != "" AND `T_QBD1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) AND `Quarter` = '.$value7.' then T_QBD1_'.$value2.' end) as T_QBD1_'.$value2.'_m'.$key.'_'.$value7.',';
                    $QBD_p = $QBD_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` IN (9,10) AND `Quarter` = '.$value7.' then record end) as T_QBD1_'.$value2.'_p'.$key.'_'.$value7.',';
                }
    
                $QBD5_b = $QBD5_b.' count(case when '.$column_name.' in ('.$key.') AND `QBD5` != "" AND `Quarter` = '.$value7.' then record end) as QBD5_b'.$key.'_'.$value7.',';
                $QBD5_m = $QBD5_m.' avg(case when '.$column_name.' in ('.$key.') AND `QBD5` != "" AND `QBD5` IN (1,2,3,4,5,6,7,8,9,10) AND `Quarter` = '.$value7.' then QBD5 end) as QBD5_m'.$key.'_'.$value7.',';
                $QBD5_p = $QBD5_p.' count(case when '.$column_name.' in ('.$key.') AND `QBD5` IN (9,10) AND `Quarter` = '.$value7.' then record end) as QBD5_p'.$key.'_'.$value7.','; 
    
                foreach($QBDY_count as $key3 => $value3)
                {
                    $QBDY_b = $QBDY_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDY'.$value3.'` != "" AND `Quarter` = '.$value7.' then record end) as QBDY'.$value3.'_b'.$key.'_'.$value7.',';
                    foreach($yes_no_array as $key4 => $value4)
                    {
                        $QBDY_t = $QBDY_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDY'.$value3.'` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDY'.$value3.'_t'.$key.'_'.$value7.',';
                    }
                }
    
                foreach($QBDX_count as $key3 => $value3)
                {
                    $QBDX_b = $QBDX_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX'.$value3.'` != "" AND `Quarter` = '.$value7.' then record end) as QBDX'.$value3.'_b'.$key.'_'.$value7.',';
                    foreach($yes_no_array as $key4 => $value4)
                    {
                        $QBDX_t = $QBDX_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX'.$value3.'` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX'.$value3.'_t'.$key.'_'.$value7.',';
                    }
                }
    
                $QBDX4_b = $QBDX4_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX4` != "" AND `Quarter` = '.$value7.' then record end) as QBDX4_b'.$key.'_'.$value7.',';
                foreach($QBDX4_count as $key4 => $value4)
                {
                    $QBDX4_t = $QBDX4_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX4` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX4_'.$value4.'_t'.$key.'_'.$value7.',';
                }
    
                $QBDX3_b = $QBDX3_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX3` != "" AND `Quarter` = '.$value7.' then record end) as QBDX3_b'.$key.'_'.$value7.',';
                foreach($QBDX3_count as $key4 => $value4)
                {
                    $QBDX3_t = $QBDX3_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX3` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX3_'.$value4.'_t'.$key.'_'.$value7.',';
                }
            }
        }
        else
        {
            foreach($QBD_count as $key2 => $value2)
            {
                $QBD_b = $QBD_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` != "" AND `Quarter` = '.$value7.' then record end) as T_QBD1_'.$value2.'_b'.$key.'_'.$value7.',';
                $QBD_m = $QBD_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` != "" AND `T_QBD1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) AND `Quarter` = '.$value7.' then T_QBD1_'.$value2.' end) as T_QBD1_'.$value2.'_m'.$key.'_'.$value7.',';
                $QBD_p = $QBD_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` IN (9,10) AND `Quarter` = '.$value7.' then record end) as T_QBD1_'.$value2.'_p'.$key.'_'.$value7.',';
            }
    
            $QBD5_b = $QBD5_b.' count(case when '.$column_name.' in ('.$key.') AND `QBD5` != "" AND `Quarter` = '.$value7.' then record end) as QBD5_b'.$key.'_'.$value7.',';
            $QBD5_m = $QBD5_m.' avg(case when '.$column_name.' in ('.$key.') AND `QBD5` != "" AND `QBD5` IN (1,2,3,4,5,6,7,8,9,10) AND `Quarter` = '.$value7.' then QBD5 end) as QBD5_m'.$key.'_'.$value7.',';
            $QBD5_p = $QBD5_p.' count(case when '.$column_name.' in ('.$key.') AND `QBD5` IN (9,10) AND `Quarter` = '.$value7.' then record end) as QBD5_p'.$key.'_'.$value7.','; 
    
            foreach($QBDY_count as $key3 => $value3)
            {
                $QBDY_b = $QBDY_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDY'.$value3.'` != "" AND `Quarter` = '.$value7.' then record end) as QBDY'.$value3.'_b'.$key.'_'.$value7.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QBDY_t = $QBDY_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDY'.$value3.'` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDY'.$value3.'_t'.$key.'_'.$value7.',';
                }
            }
    
            foreach($QBDX_count as $key3 => $value3)
            {
                $QBDX_b = $QBDX_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX'.$value3.'` != "" AND `Quarter` = '.$value7.' then record end) as QBDX'.$value3.'_b'.$key.'_'.$value7.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QBDX_t = $QBDX_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX'.$value3.'` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX'.$value3.'_t'.$key.'_'.$value7.',';
                }
            }
    
            $QBDX4_b = $QBDX4_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX4` != "" AND `Quarter` = '.$value7.' then record end) as QBDX4_b'.$key.'_'.$value7.',';
            foreach($QBDX4_count as $key4 => $value4)
            {
                $QBDX4_t = $QBDX4_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX4` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX4_'.$value4.'_t'.$key.'_'.$value7.',';
            }
    
            $QBDX3_b = $QBDX3_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX3` != "" AND `Quarter` = '.$value7.' then record end) as QBDX3_b'.$key.'_'.$value7.',';
            foreach($QBDX3_count as $key4 => $value4)
            {
                $QBDX3_t = $QBDX3_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX3` = '.$value4.' AND `Quarter` = '.$value7.' then record end) as QBDX3_'.$value4.'_t'.$key.'_'.$value7.',';
            }
        }
    }
}

$all = mysqli_query($conn,"SELECT $QBD_b $QBD_m $QBD_p $QBD5_b $QBD5_m $QBD5_p $QBDY_b $QBDY_t $QBDX_b $QBDX_t $QBDX4_b $QBDX4_t $QBDX3_b $QBDX3_t record FROM `cherry` WHERE $condX");
$ALL= mysqli_fetch_array($all);

foreach($timeVal as $key7 => $value7)
{
    $QBD1_arrXT = array();
    $QBD_label_arrXT = array();
    foreach($QBD_count as $key2 => $value2)
    {
        $QBD1_arrX = array();
        $QBD_label_arrX = array();
        foreach($brand_C as $key => $value)
        {
            $QBD1_arr2 = array();
            $QBD_b = $ALL['T_QBD1_'.$value2.'_b'.''.$key.'_'.$value7.''];
            if($QBD_b==0)
            {
                $QBD_b=1;
            }

            if(is_numeric($ALL['T_QBD1_'.$value2.'_m'.''.$key.'_'.$value7.'']))
            {
                $QBD_m = round($ALL['T_QBD1_'.$value2.'_m'.''.$key.'_'.$value7.'']);
            }
            else
            {
                $QBD_m = 0;
            }
            $QBD_p2 = $ALL['T_QBD1_'.$value2.'_p'.''.$key.'_'.$value7.''];
            $QBD_p = round(($QBD_p2 / $QBD_b ) * 100);
            array_push($QBD1_arr2,$QBD_m,$QBD_p,$QBD_b);
            array_push($QBD1_arrX,$QBD1_arr2);
            array_push($QBD_label_arrX,$value);
        }
        if(sizeof($QBD1_arrX)!=0)
        {
            array_push($QBD1_arrXT,$QBD1_arrX);
            array_push($QBD_label_arrXT,$QBD_label_arrX);
        }
    }
    array_push($QBD1_arr,$QBD1_arrXT);
    array_push($QBD_label_arr,$QBD_label_arrXT);
}

foreach($timeVal as $key7 => $value7)
{
    $QBDOVERALL_arrXT = array();
    foreach($brand_C as $key => $value)
    {
        $QBDOVERALL_arrX = array();
        $QBD5_b = $ALL['QBD5_b'.''.$key.'_'.$value7.''];
        if($QBD5_b==0)
        {
            $QBD5_b=1;
        }

        if(is_numeric($ALL['QBD5_m'.''.$key.'_'.$value7.'']))
        {
            $QBD5_m = round($ALL['QBD5_m'.''.$key.'_'.$value7.'']);
        }
        else
        {
            $QBD5_m = 0;
        }
        $QBD5_p2 = $ALL['QBD5_p'.''.$key.'_'.$value7.''];
        $QBD5_p = round(($QBD5_p2 / $QBD5_b ) * 100);
        array_push($QBDOVERALL_arrX,$QBD5_m,$QBD5_p,$QBD5_b);
        array_push($QBDOVERALL_arrXT,$QBDOVERALL_arrX);
        array_push($QBDOVERALL_label_arr,$value);
    }
    array_push($QBDOVERALL_arr,$QBDOVERALL_arrXT);
}

foreach($timeVal as $key7 => $value7)
{
    $QBDY_arr2T = array();
    $QBDY_label_arrXT = array();
    foreach($QBDY_count as $key3 => $value3)
    {
        $QBDY_arr2 = array();
        $QBDY_label_arrX = array();
        foreach($brand_C as $key => $value)
        {
            $QBDY_arr3 = array();
            $QBDY_b = $ALL['QBDY'.$value3.'_b'.''.$key.'_'.$value7.''];
            if($QBDY_b==0)
            {
                $QBDY_b=1;
            }
            foreach($yes_no_array as $key4 => $value4)
            {
                $QBDY_t2 = $ALL['QBDY'.$value3.'_t'.''.$key.'_'.$value7.''];
                $QBDY_t = round(($QBDY_t2/$QBDY_b)*100);
                // if($QBDY_t!=0)
                // {
                    array_push($QBDY_arr3,$QBDY_t);
                    array_push($QBDY_label_arrX,$value);
                //}
            }

            if(sizeof($QBDY_arr3)!=0)
            {
                array_push($QBDY_arr2,$QBDY_arr3);
            }
        }
        if(sizeof($QBDY_arr2)!=0)
        {
            array_push($QBDY_arr2T,$QBDY_arr2);
            array_push($QBDY_label_arrXT,$QBDY_label_arrX);
        }
    }
    array_push($QBDY_arr,$QBDY_arr2);
    array_push($QBDY_label_arr,$QBDY_label_arrXT);
}

foreach($timeVal as $key7 => $value7)
{
    $QBDX_arr2T = array();
    $QBDX_label_arrXT = array();
    foreach($QBDX_count as $key3 => $value3)
    {
        $QBDX_arr2 = array();
        $QBDX_label_arrX = array();
        foreach($brand_C as $key => $value)
        {
            $QBDX_arr3 = array();
            $QBDX_b = $ALL['QBDX'.$value3.'_b'.''.$key.'_'.$value7.''];
            if($QBDX_b==0)
            {
                $QBDX_b=1;
            }
            foreach($yes_no_array as $key4 => $value4)
            {
                $QBDX_t2 = $ALL['QBDX'.$value3.'_t'.''.$key.'_'.$value7.''];
                $QBDX_t = round(($QBDX_t2/$QBDX_b)*100);
                // if($QBDX_t!=0)
                // {
                    array_push($QBDX_arr3,$QBDX_t);
                    array_push($QBDX_label_arrX,$value);
                //}
            }
            if(sizeof($QBDX_arr3)!=0)
            {
                array_push($QBDX_arr2,$QBDX_arr3);
            }
        }
        if(sizeof($QBDX_arr2)!=0)
        {
            array_push($QBDX_arr2T,$QBDX_arr2);
            array_push($QBDX_label_arrXT,$QBDX_label_arrX);
        }
    }
    array_push($QBDX_arr,$QBDX_arr2T);
    array_push($QBDX_label_arr,$QBDX_label_arrXT);
}


foreach($timeVal as $key7 => $value7)
{
    $QBDX4_arr2T = array();
    $QBDX4_label_arrXT = array();
    foreach($QBDX4_count as $key4 => $value4)
    {
        $QBDX4_arr2 = array();
        $QBDX4_label_arrX = array();
        foreach($brand_C as $key => $value)
        {
            $QBDX4_b = $ALL['QBDX4_b'.$key.'_'.$value7.''];
            if($QBDX4_b==0)
            {
                $QBDX4_b=1;
            }

            $QBDX4_t2 = $ALL['QBDX4_'.$value4.'_t'.$key.'_'.$value7.''];
            $QBDX4_t = round(($QBDX4_t2 / $QBDX4_b) *100);
            array_push($QBDX4_arr2,$QBDX4_t);
            array_push($QBDX4_label_arrX,$value);
        }
        if(sizeof($QBDX4_arr2)!=0)
        {
            array_push($QBDX4_arr2T,$QBDX4_arr2);
            array_push($QBDX4_label_arrXT,$QBDX4_label_arrX);
        }
    }
    array_push($QBDX4_arr,$QBDX4_arr2T);
    array_push($QBDX4_label_arr,$QBDX4_label_arrXT);
}


foreach($timeVal as $key7 => $value7)
{
    $QBDX3_arr2T = array();
    $QBDX3_label_arrXT = array();
    foreach($QBDX3_count as $key4 => $value4)
    {
        $QBDX3_arr2 = array();
        $QBDX3_label_arrX = array();
        foreach($brand_C as $key => $value)
        {
            $QBDX3_b = $ALL['QBDX3_b'.$key.'_'.$value7.''];
            if($QBDX3_b==0)
            {
                $QBDX3_b=1;
            }

            $QBDX3_t2 = $ALL['QBDX3_'.$value4.'_t'.$key.'_'.$value7.''];
            $QBDX3_t = round(($QBDX3_t2 / $QBDX3_b) *100);
            array_push($QBDX3_arr2,$QBDX3_t);
            array_push($QBDX3_label_arrX,$value);
        }
        if(sizeof($QBDX3_arr2)!=0)
        {
            array_push($QBDX3_arr2T,$QBDX3_arr2);
            array_push($QBDX3_label_arrXT,$QBDX3_label_arrX);
        }
    }
    array_push($QBDX3_arr,$QBDX3_arr2T);
    array_push($QBDX3_label_arr,$QBDX3_label_arrXT);
}
array_push($QBD_arr,$QBD1_arr);
array_push($QBD_arr,$QBDOVERALL_arr);
$data['QBD'] = $QBD_arr;
$data['QBDL'] = $QBD_label_arr;
$data['QBDOVERALLL'] = $QBDOVERALL_label_arr;
$data['QBDY'] = $QBDY_arr;
$data['QBDYL'] = $QBDY_label_arr;
$data['QBDX'] = $QBDX_arr;
$data['QBDXL'] = $QBDX_label_arr;
$data['QBDX4'] = $QBDX4_arr;
$data['QBDX4L'] = $QBDX4_label_arr;
$data['QBDX3'] = $QBDX3_arr;
$data['QBDX3L'] = $QBDX3_label_arr;


echo json_encode($data);

?>