php - Nestled While loops?
I have a bit of code:
while($row = mysqli_fetch_array($Sat)){
echo "['".$row['Month']."',".$row['Store A'].",".$row['Store B'].",".$row['Store C']."],";
Which works fine for cases when a user has Store A, B & C - however, I want to expand this to people who will have a varying amount of stores.
I know the array $Sat has headers, so, is it possible to count the headers and whilst they don't equal Month (Month needs to go first) echo out the header name so I feed the associated rows into the format I need for my next step.
The final output should look like['Month',Store A,Store B,Store C]
(if the user had the three stores, or maybe['Month',Store A,Store C]
if they had just A & C etc..
I've been trying to substitute the code with a variable that builds it but that doesn't seem to work.
I'm not very well experienced in PHP (or coding really) and just trying to adapt something to work internally at work a bit better and save my sanity = )
So, if my database was:
+-------+---------+---------+---------+ | 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| +-------+---------+---------+---------+
The required output would be:['May-19',0.0,0.0,0.0],['Jun-19',12.5,8.3,0.0],['Jul-19',10.5,14.3,0.0],
to feed into Google Charts.
Original SQL query is:
$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";
which would compile to:
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
and output:
+---------+---------+---------+-------+ | 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| +---------+---------+---------+-------+
I've found I can edit my original code:
while($row = mysqli_fetch_array($Sat)){
echo "['".$row['Month']."',".$row['Store A'].",".$row['Store B'].",".$row['Store C']."],";
to
while($row = mysqli_fetch_array($Sat)){
echo "['".$row['Month']."',".$row[0].",".$row[1].",".$row[2]."],";
So this get's around the naming, I just need to have in there as many rows as I have stores now, without building a giant "IF THEN" query. I still feel this is a nestled while or foreach that would do this?
Thank you
Answer
Solution:
Try this code below, i did found some type errors in sql syntax:
Try this code, it suppose to produce this:
with arrays:
to echo full array you need this:
That's what i get:
tested data:
Answer
Solution:
GOT IT!
Thanks to @Serghei Leonenco for talking this through, I used some of your ideas in building this, but this works exactly as needed for N stores. Cheers, couldn't've done it without you!