将来自两个不同循环的数据放入谷歌饼图中

将来自两个不同循环的数据放入谷歌饼图中

问题描述:

So I have this code for my Google pie chart (pie chart code not included, but it works fine and displays data)

<?php 
    $query = "SELECT * FROM category WHERE type = 'Expense' ";
    $select_category_list = mysqli_query($connection, $query);
    while($row = mysqli_fetch_assoc($select_category_list)) {
    $cat_id = $row['id'];
    $cat_title = $row['title']; 
   echo "['$cat_title'" . ",";
   echo "$cat_id],";
}
?>

I put the echo in different lines, but it really does not matter to if they are in one line or in two lines, what matters is to get it finally working :) Instead of $cat_id I want to put the SUM ($cat_amount) calculated by this code:

$query = "SELECT category_id, SUM(amount) AS TotalAmount FROM spendee
WHERE type = 'Expense' group by category_id ";
    $expense_query = mysqli_query($connection, $query);
    while ($row = mysqli_fetch_assoc($expense_query)) {
         $cat_amount = $row['TotalAmount'];
    }

I have tried putting while loop in a while loop, foreach loop in a while loop, putting $cat_amount instead of $cat_id (that only displays one SUM) and so on. Nothing worked. I have not tried the for loop, but I assume the result would be the same + I am not sure how would I define the times that the loop needs to run, since there could be many type 'Expense' categories added in time.

Any help would be very much appreciated. Thank you and have a lovely day.

所以我的谷歌饼图有这个代码(不包括饼图代码,但它工作正常并显示数据 ) p>

 &lt;?php 
 $ query =“SELECT * FROM category WHERE type ='Expense'”; 
 $ select_category_list = mysqli_query($ connection,$ query)  ; 
 while($ row = mysqli_fetch_assoc($ select_category_list)){
 $ cat_id = $ row ['id']; 
 $ cat_title = $ row ['title'];  
 echo“['$ cat_title'”。  “,”; 
 echo“$ cat_id],”; 
} 
?&gt; 
  code>  pre> 
 
 

我把回声放在不同的行中,但它确实 如果它们在一行或两行中无关紧要,重要的是让它最终工作:) 而不是$ cat_id我想把这个代码计算的SUM($ cat_amount): p> \ n

  $ query =“SELECT category_id,SUM(amount)AS TotalAmount FROM spendee 
WHERE type ='Expense'group by category_id”; 
 $ expense_query = mysqli_query($ connection,$ query);  
 while($ row = mysqli_fetch_assoc($ expense_query)){
 $ cat_amount = $ row ['TotalAmount']; 
} 
  code>  pre> 
 
 

我试过 在while循环中放入while循环,在while循环中放置foreach循环,放入$ cat_amount而不是$ cat_id(仅显示一个SUM),依此类推。 没有任何效果。 我没有尝试过for循环,但是我假设结果是相同的+我不确定如何定义循环需要运行的时间,因为可能会有很多类型的“费用”类别及时添加。 p>

非常感谢任何帮助。 谢谢你,祝你有个美好的一天。 p> div>

maybe i'm missing something, but looks like you could join the two tables in one query

this would get the desired result in one query / loop...

sql:

SELECT a.title as Title, SUM(b.amount) AS TotalAmount FROM category a, spendee b WHERE b.category_id = a.id and b.type = 'Expense' group by a.title

php:

<?php
  $query = "SELECT a.title as Title, SUM(b.amount) AS TotalAmount FROM category a, spendee b WHERE b.category_id = a.id and b.type = 'Expense' group by a.title";
  $select_category_list = mysqli_query($connection, $query);
  while ($row = mysqli_fetch_assoc($select_category_list)) {
    $cat_title = $row['Title'];
    $cat_amount = $row['TotalAmount'];
    echo "['$cat_title'" . ",";
    echo "$cat_amount],";
  }
?>