0

我正在尝试将我计算出的一些百分比存储到一个数组中,以便我以后可以调用它来创建一个饼图,但不知道如何去做。到目前为止,这是我计算百分比的 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);
}
4

1 回答 1

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);
}
于 2013-01-10T17:20:34.447 回答