将百分比存储到数组中

时间:2013-01-10 17:09:16

标签: php mysql arrays

我正在尝试将我已经计算出的一些百分比存储到一个数组中,这样我以后可以调用它来创建一个饼图,但是不知道怎么做。以下是我的PHP代码,用于计算百分比:感谢您提前获得任何帮助!

//execute the SQL query and return records
$result = mysql_query("SELECT book_id, COUNT(*) FROM loan GROUP BY book_id ASC ORDER BY     count(*) DESC LIMIT 10");
$book_count = mysql_num_rows($result);

$step = 1;

while($row = mysql_fetch_array($result))
{
    $total = $total + $row['COUNT(*)'];
    $i[$step] = $row['COUNT(*)'];
    $step++;
}

for($index = 1; $index <= 10; $index++)
{
    $percentage[$index] = (($i[$index]/$total)*100);
    #$degrees = (($percentage/100)*360);
}

1 个答案:

答案 0 :(得分:0)

这应该做你要求的,只需用变量替换真实的变量:

array_push($array, $value);
// This will add "$value" on to the end of $array, i.e the last element

它也可以做得更简单:

$array[] = $value;

更新的代码:

//execute the SQL query and return records
$result = mysql_query("SELECT book_id, COUNT(*) FROM loan GROUP BY book_id ASC ORDER BY       count(*) DESC LIMIT 10");
$book_count = mysql_num_rows($result);

$step = 1;

while($row = mysql_fetch_array($result))
{
    $total = $total + $row['COUNT(*)'];
    $i[$step] = $row['COUNT(*)'];
    $step++;
}

$percentage_array = array();

for($index = 1; $index <= 10; $index++)
{
    array_push($percentage_array, (($i[$index]/$total)*100));
    #$degrees = (($percentage/100)*360);
}