简体   繁体   中英

Adding zero values to an array

I am doing the following queries on a database that holds various customers orders.

SELECT WEEKDAY(orderDateTime) Date, COUNT(clientID) totalCount FROM orders WHERE YEARWEEK(orderDateTime,1) = YEARWEEK(NOW(),1) GROUP BY DATE(orderDateTime)

SELECT WEEKDAY(orderDateTime) Date, COUNT(clientID) totalCount FROM orders WHERE YEARWEEK(orderDateTime,1) = YEARWEEK(NOW() - INTERVAL 1 WEEK,1) GROUP BY DATE(orderDateTime)

SELECT DATE_FORMAT(orderDateTime, '%Y') as 'year', DATE_FORMAT(orderDateTime, '%m') as 'month', COUNT(clientID) as 'total' FROM orders GROUP BY DATE_FORMAT(orderDateTime, '%Y%m')

I am them building these into an array with the month/ day numbers against the counted value:

[[0, 1], [1,4]...]

I am using Flot to graph these but what is happening is that any days or months that there are no orders it has no values (Obviously) so what you get is something like this:

[[0, 1], [1,4], [6,12]] which makes the graph plot wrong.

What trying to work out is how to pad it so it looks like:

[[0, 1], [1,4], [2,0], [3,0], [4,0], [5,0], [6,12]] <-- For the week days and,

[[1, 1], [2,4], [3,0], [4,0], [5,0], [6,0], [7,12], [8,0], [9,12], [10,0], [11,0], [12,0]] <-- for each month.

Im using PHP as the main grunt. Any pointers would be appreciated and sorry if I'm not that clear. Ask if you need any clarification.

PHP:

$thisWeekA = array();

$thisWeekQ = $database->query("SELECT WEEKDAY(orderDateTime) Date, COUNT(clientID) totalCount FROM orders WHERE YEARWEEK(orderDateTime,1) = YEARWEEK(NOW(),1) GROUP BY DATE(orderDateTime)")->fetchAll();

foreach($thisWeekQ as $thisweek){
    $thisWeekA[] = array( $thisweek['Date'], $thisweek['totalCount'] );
}

array(2) {
  [0]=>
  array(4) {
    ["Date"]=>
    string(1) "2"
    [0]=>
    string(1) "2"
    ["totalCount"]=>
    string(1) "3"
    [1]=>
    string(1) "3"
  }
  [1]=>
  array(4) {
    ["Date"]=>
    string(1) "3"
    [0]=>
    string(1) "3"
    ["totalCount"]=>
    string(1) "2"
    [1]=>
    string(1) "2"
  }
}

for weekly data use for loop in your php update it as, Logic only 7 days in a week of instead of using foreach from the result data we can use the for loop for seven days

ie, similarly use for years also

$thisWeekA = array();    
$thisWeekQ = $database->query("SELECT WEEKDAY(orderDateTime) Date, COUNT(clientID) totalCount FROM orders WHERE YEARWEEK(orderDateTime,1) = YEARWEEK(NOW(),1) GROUP BY DATE(orderDateTime)")->fetchAll();

    for ($i = 0; $i <= 6; $i++) {
        if ($thisWeekQ[$i]['Date'] == $i) {
            $thisWeekA[$i][] = array($thisWeekQ[$i]['Date'], $thisWeekQ[$i]['totalCount']);
        } else {
            $thisWeekA[$i][] = array($i, 0);
        }
    }

You might want to think about building your arrays more like this:

var dow = [];
dow[0] = 1;
dow[1] = 4;
dow[6] = 12;

At this point you can populate your array for your chart

var chartData = [];
for (var i=0; i<7; i++){
    chartData[i] = [i, (dow[i] === undefined ? 0 : dow[i])];
}

I managed to figure out a way of doing this. Thanks for pointing me in the direction of the For loop!

This is what I have came up with and works!

function arrayhunt($products, $field, $value){

   foreach($products as $key => $product){

        if ($product[$field] == $value){
            return $key;
        }
   }
   return false;
}


    $i = 0;
    for ($i = 0; $i <= 6; $i++) {

        $keys = arrayhunt($thisWeekQ,"Date",$i);

        if($keys !== FALSE){
            $thisWeekA[] = array($thisWeekQ[$keys]['Date'], $thisWeekQ[$keys]['totalCount']);
        } else {
            $thisWeekA[] = array($i, 0);
        }

        $keys = "";
    }

Hopefully someone finds this useful later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM