依Whi​​le While循环?

时间:2019-07-16 04:17:01

标签: php

我有一些代码:

while($row = mysqli_fetch_array($Sat)){
    echo "['".$row['Month']."',".$row['Store A'].",".$row['Store B'].",".$row['Store C']."],";

哪种方法在用户拥有商店A,B和C的情况下效果很好-但是,我想将其扩展到拥有不同商店数量的人。

我知道$ Sat数组具有标题,因此,可以对标题进行计数,而它们不等于Month(月份需要先输入)回显标题名称,因此我将相关的行输入格式我需要下一步。

最终输出应类似于['Month',Store A,Store B,Store C](如果用户拥有三个商店,或者如果['Month',Store A,Store C]仅具有A和C等。

我一直试图用构建它的变量替换代码,但这似乎不起作用。

我对PHP(或真正的编码)的经验不是很丰富,只是想让某些东西适应内部工作,从而更好地保存自己的理智=)

因此,如果我的数据库是:

+-------+---------+---------+---------+
| Month | Store A | Store B | Store C |
+-------+---------+---------+---------+
| May-19|      0.0|      0.0|      0.0|
| Jun-19|     12.5|      8.3|      0.0|
| Jul-19|     10.5|     14.3|      0.0|
+-------+---------+---------+---------+

所需的输出将是:['May-19',0.0,0.0,0.0],['Jun-19',12.5,8.3,0.0],['Jul-19',10.5,14.3,0.0],以馈入Google图表。

原始SQL查询为:

$sql_prep = "SELECT d.store_list AS NUMB, s.name AS NAME FROM data d INNER JOIN store_list s ON d.store_list=s.store_id WHERE d.status != '' AND d.store_list IN (" . $user_stores . ") GROUP BY store_list";
$sql_code = $conn->query($sql_prep);
    while($row = $sql_code->fetch_assoc()) {
    $select = $select . "CAST(((SUM(CASE WHEN LOWER(`q3`) = 'no' AND store_list = " . $row['NUMB'] . " THEN 1 ELSE 0 END)/SUM(CASE WHEN store_list = " . $row['NUMB'] . " THEN 1 ELSE 0 END))*100) as decimal (10,1)) AS  '" . $row['NAME'] . "',";
}
....
$sql = "SELECT
    " . $select . "
    DATE_FORMAT(date_submitted,'%b-%y') AS 'Month'
FROM data 
WHERE store_list IN (" . $user_stores . ")
GROUP BY YEAR(date_submitted), MONTH(date_submitted)
ORDER BY YEAR(date_submitted) ASC, MONTH(date_submitted) ASC";

它将编译为:

SELECT
    CAST(((SUM(CASE WHEN LOWER(`q3`) = 'no' AND store_list = 1 THEN 1 ELSE 0 END)/SUM(CASE WHEN store_list = 1 THEN 1 ELSE 0 END))*100) as decimal (10,1)) AS  'Store A',
    CAST(((SUM(CASE WHEN LOWER(`q3`) = 'no' AND store_list = 2 THEN 1 ELSE 0 END)/SUM(CASE WHEN store_list = 2 THEN 1 ELSE 0 END))*100) as decimal (10,1)) AS  'Store B',
    CAST(((SUM(CASE WHEN LOWER(`q3`) = 'no' AND store_list = 3 THEN 1 ELSE 0 END)/SUM(CASE WHEN store_list = 3 THEN 1 ELSE 0 END))*100) as decimal (10,1)) AS  'Store C',
    DATE_FORMAT(date_submitted,'%b-%y') AS 'Month'
FROM data 
WHERE store_list IN (1,2,3)
GROUP BY YEAR(date_submitted), MONTH(date_submitted)
ORDER BY YEAR(date_submitted) ASC, MONTH(date_submitted) ASC

并输出:

+---------+---------+---------+-------+
| Store A | Store B | Store C | Month |
+---------+---------+---------+-------+
|      0.0|      0.0|      0.0| May-19|
|     12.5|      8.3|      0.0| Jun-19|
|     10.5|     14.3|      0.0| Jul-19|
+---------+---------+---------+-------+

我发现我可以编辑自己的原始代码:

while($row = mysqli_fetch_array($Sat)){
    echo "['".$row['Month']."',".$row['Store A'].",".$row['Store B'].",".$row['Store C']."],";

while($row = mysqli_fetch_array($Sat)){
    echo "['".$row['Month']."',".$row[0].",".$row[1].",".$row[2]."],";

因此,命名就是这样,我只需要在其中存储行数就可以了,而无需构建巨大的“ IF THEN”查询。我仍然觉得这是一个紧要关头的机会,可以吗?

谢谢

2 个答案:

答案 0 :(得分:2)

在下面尝试此代码,我确实在sql语法中发现了一些类型错误:

    $sql_prep = "SELECT d.store_list AS NUMB, s.name AS NAME FROM data d INNER JOIN store_list s ON d.store_list=s.store_id WHERE d.status != '' AND d.store_list IN (" . $user_stores . ") GROUP BY store_list";
$sql_code = $conn->query($sql_prep);
$select = '';
    while($row = $sql_code->fetch_assoc()) {
        $select .= ", CAST(((SUM(CASE WHEN LOWER(`q3`) = 'no' AND store_list = " . $row['NUMB'] . " THEN 1 ELSE 0 END)/SUM(CASE WHEN store_list = " . $row['NUMB'] . " THEN 1 ELSE 0 END))*100) as decimal (10,1)) AS  '" . $row['NAME'] . "' "; -- right here
    }
.....
$sql = "SELECT DATE_FORMAT(date_submitted,'%b-%y') AS 'Month'
" . $select . "
FROM data 
WHERE store_list IN (" . $user_stores . ")
GROUP BY YEAR(date_submitted), MONTH(date_submitted)
ORDER BY YEAR(date_submitted) ASC, MONTH(date_submitted) ASC";

尝试以下代码,它可能会产生以下代码:

+-------+---------+---------+---------+
| Month | Store A | Store B | Store C |
+-------+---------+---------+---------+
| May-19|      0.0|      0.0|      0.0|
| Jun-19|     12.5|      8.3|      0.0|
| Jul-19|     10.5|     14.3|      0.0|
+-------+---------+---------+---------+

带有数组:

['May-19',0.0,0.0,0.0],['Jun-19',12.5,8.3,0.0],['Jul-19',10.5,14.3,0.0]

要回显完整的数组,您需要这样做:

$result = [];
$index = 1;
while($row = mysqli_fetch_array($Sat)){
    //we know for sure that index 0 has value Month, then..
    $temp = [$row['Month']];
    $number = count($row) - 1;
    //loop thru rest of rows start from index 1
    for($i = 0; $i < $number; $i++){
        //push rows in to temporary array
        array_push($temp, $row[$i]);
    }
    //merge result in main array
    $result[$index] = $temp;
    $index++;
}
var_dump($result);

这就是我得到的:

array (size=5)
  0 => 
    array (size=5)
      0 => string 'moth1' (length=5)
      1 => float 10
      2 => float 20
      3 => float 30.6
      4 => float 45.2
  1 => 
    array (size=6)
      0 => string 'moth2' (length=5)
      1 => float 10
      2 => float 20
      3 => float 30.6
      4 => float 45.2
      5 => float 45.8
  2 => 
    array (size=5)
      0 => string 'moth3' (length=5)
      1 => float 10
      2 => float 20
      3 => float 30.6
      4 => float 45.2
  3 => 
    array (size=7)
      0 => string 'moth4' (length=5)
      1 => float 10
      2 => float 20
      3 => float 30.6
      4 => float 45.2
      5 => float 56.2
      6 => float 56.3
  4 => 
    array (size=5)
      0 => string 'moth5' (length=5)
      1 => float 10
      2 => float 20
      3 => float 30.6
      4 => float 45.2

测试数据:

    $rows = [
       ['Month' => 'moth1', 10.0, 20.0, 30.6, 45.2],
       ['Month' => 'moth2', 10.0, 20.0, 30.6, 45.2, 45.8],
       ['Month' => 'moth3', 10.0, 20.0, 30.6, 45.2],
       ['Month' => 'moth4', 10.0, 20.0, 30.6, 45.2, 56.2, 56.3],
       ['Month' => 'moth5', 10.0, 20.0, 30.6, 45.2],
   ];

答案 1 :(得分:0)

知道了!

感谢@Serghei Leonenco的讲解,我在构建它时使用了您的一些想法,但这完全可以满足N家商店的需要。干杯,没有你就做不到!

broadFileSystemAccess