【发布时间】:2016-07-05 09:24:05
【问题描述】:
我已经为这个 foreach loopp 苦苦挣扎了一段时间了。我需要做的是从我的数据库表中获取总和值,进行一些计算,然后使用 css 将它们显示为条形图。但我的 foreach 循环只给了我表中的最后一个值。任何帮助将不胜感激,这是我的代码示例:
<?php
$total_query = "select sum(amount_recieved) from reciepts";
$total_result = safe_query($total_query);
$total = mysql_fetch_row($total_result);
$query = "select category_id from customer_categories";
$result = safe_query($query);
while($cat_id = mysql_fetch_assoc($result)){
foreach ($cat_id as $cat) {
$sum_query = "select sum(amount_recieved) from reciepts where category =".$cat."";
$sum_result = safe_query($sum_query);
$sum = mysql_fetch_array($sum_result);
$percentage = ($sum[0]/$total[0]) * 100;
echo "
<li title='".$cat.", NGN ".$sum[0].", ".$percentage."%' class='bar' style='
position:absolute;
bottom:0;
left:1%;
float:left;
width:7%;
height:".$percentage."%;
margin-right:1%;
margin-left:1%;
background:#999;'>
</li>";
}
}
?>
【问题讨论】:
-
while 循环遍历行,foreach 循环遍历列,这是您想要发生的吗?
-
customer_categories表中有多少个类别? -
真的不应该再发这个了:stackoverflow.com/questions/12859942/…