how to insert data while looping and inside array as well

。_饼干妹妹 提交于 2020-01-06 06:44:23

问题


This is my table order:

+-------+-------+
| month | count |
+-------+-------+
|     6 |    11 |
|    11 |    27 |
|    12 |     9 |
+-------+-------+

I want to create a graph using fusioncharts. Let's say that the year is 2017. How do I put the missing month into the array of which fusioncharts I'm using? I'm stuck on the condition.

This is my code:

$strQuery2 = "SELECT DATE_FORMAT(order_date, '%c') as month, COUNT(*) AS cnt FROM orders where YEAR(order_date)='2017' GROUP BY month  ORDER BY `month` DESC";
$result2 = $dbhandle->query($strQuery2);
// Push the data into the array
while($row2 = $result2->fetch_assoc()) {
    $q = $row2["month"];
    $w = $row2["cnt"];
    $e = 0;
    $x = 1;
    if ($x != $q){
        echo "true";
        array_push($arrData2["data"],
            array(
                "label" => $row2[$x],
                "value" => $row2[$e]
            )
        );
    }
    else{
        echo "false";
        array_push($arrData2["data"],
            array(
                "label" => $row2["month"],
                "value" => $row2["cnt"]
            )
        );
    }
    $x++;
    echo $row2["month"] . "<br />";
    echo $row2["cnt"] . "<br />";
}

回答1:


You never get a month from the database that isn't there. If you push data into an empty array:

array_push($arrData2["data"]

you still end up with an array that only contains the months returned from the database.

What you can do is first create an array with all 12 months:

$arrData2["data"]=range(1,12,false);

You have now an array that contains 12 elements (months) from 1 - 12, all with values of false. (You could use any value, an array or false if you need). Now inside your resultloop, just replace the array elements you have values for:

$arrData2["data"][$row2["month"]] = array(
      'label' => $row2["month"],
      'value' => $row2["cnt"]
      );

$arrData2 now will look like:

array( 
   1 => false,
   2 => false,
   3 => false,
   4 => false,
   5 => false,
   6 => array('label'=> 6,'value'=> 11),
   7 => false,
   8 => false,
   9 => false,
  10 => false,
  11 => array('label'=> 11,'value'=> 27),
  12 => array('label'=> 12,'value'=> 9)
)

Now you can replace all the false values for arrays with value 0

So your whole code could be schortened to:

//set empty array
$arrData2['data'] = range(1, 12, false);

//fill elements we know
while($row2 = $result2->fetch_assoc()) {
    $arrData2["data"][$row2["month"]] = array(
      'label' => $row2["month"],
      'value' => $row2["cnt"]
      );
    }

//replace the rest with 0
foreach($arrData2["data"] as $key => $value){
    // skip what we already know
    if($values !== false) continue;
    // replace the rest
    $arrData2["data"][$key]=array(
                              'label'=>$key,
                              'value'=> 0
                              );
}
echo print_r($arrData2,true);



回答2:


There are basically two ways to approach it. One to fill missing months on PHP side and one on SQL. I will give you SQL version:

SELECT
    m.`month`,
    IFNULL(x.cnt, 0) AS cnt
FROM
    (SELECT 1 AS `month` UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS m
    LEFT JOIN (SELECT DATE_FORMAT(order_date, '%c') as `month`, COUNT(*) AS cnt FROM orders where YEAR(order_date)='2017' GROUP BY `month`) AS x ON m.`month` = x.`month`
ORDER BY m.`month` DESC

It basically generates list of all months from 1 to 12 and if such month exists in your query than it is filled with real value. Otherwise it's zero.



来源:https://stackoverflow.com/questions/48339949/how-to-insert-data-while-looping-and-inside-array-as-well

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!