我已经在这个foreach循环中挣扎了一段时间了。我需要做的是从我的数据库表中获取总和值,进行一些计算,然后使用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>";
}
}
?>
答案 0 :(得分:0)
您的问题是您将数据可能检索到的每个条形放置在完全相同的位置:
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>";
向左浮动并绝对定位,它们都会被覆盖。无需对这些li元素进行绝对定位,请尝试删除position:absolute;
。
答案 1 :(得分:0)
试试这个; $ sum_query =“选择sum(amount_recieved)来自reciepts,其中category =”。$ cat ['category_id'];