由于某种原因,我的代码是一分钟而不是下一代。我只是在学习php而我无法解决这个问题。我已经使用此页面来解决大部分问题:How to add two Google charts on the one page?
我的代码也用在将数据显示为表格的页面上。在那个页面上它工作得很好....但由于某种原因,我在饼图页面上获得为foreach()提供的无效参数......并且它正在说它& #39;不是数组。我不确定如何解决这个问题。
我使用的网址是?action = scores& data = pie所以它应该列出所有大学" get_universities()" (那里有3个)。
有人可以帮忙吗?
开关:
// Showing scores
case 'scores':
// Grab the Uni ID and use the appropriate query
if (isset($_GET['uni_id']))
{
$uni_id = $_GET['uni_id'];
$uni = get_university($uni_id);
}
else
{
$uni = get_universities();
}
// We have to display this data according to the request
if (isset($_GET['data']))
{
$data = $_GET['data'];
}
else
{
$data = "table";
}
// Display the data accordingly
include (root . '/view/' . $data . '_view.php');
break;
饼图:
// Create the data table.
<?php foreach ($uni as $uni) : ?>
var data<?php echo $uni['university_id']; ?> = new google.visualization.DataTable();
data<?php echo $uni['university_id']; ?>.addColumn('string', 'Area');
data<?php echo $uni['university_id']; ?>.addColumn('number', 'Score');
data<?php echo $uni['university_id']; ?>.addRows([
['Teaching', <?php echo $uni['teaching_score']; ?>],
['Intl Outlook', <?php echo $uni['int_outlook_score']; ?>],
['Industry Income', <?php echo $uni['ind_income_score']; ?>],
['Research', <?php echo $uni['research_score']; ?>],
['Citations', <?php echo $uni['citations_score']; ?>]
]);
<?php endforeach ?>
// Set chart options
<?php foreach ($uni as $uni) : ?>
var options<?php echo $uni['university_id']; ?> = {'title':'<?php echo $uni['university_name']; ?> Scores',
'width':400,
'height':300};
<?php endforeach ?>
// Instantiate and draw our chart, passing in some options.
<?php foreach ($uni as $uni) : ?>
var chart<?php echo $uni['university_id']; ?> = new google.visualization.PieChart(document.getElementById('chart_div<?php echo $uni['university_id']; ?>'));
chart.draw(data<?php echo $uni['university_id']; ?>, options<?php echo $uni['university_id']; ?>);
<?php endforeach ?>
表格视图(有效):
<?php foreach ($uni as $uni) : ?>
<td>
<a href="?uni=<?php echo $uni['university_id']; ?>">
<?php echo $uni['university_name']; ?>
</a>
</td>
<etc>
答案 0 :(得分:0)
问题在于您的foreach
声明:
foreach ($uni as $uni) :
在此重写$uni
变量。为集合和项目使用不同的名称,例如:
foreach ($uni as $theUni) :
// also change instances of $uni below
修改强>
以上是错误。 foreach
的第一个参数的解析只发生一次,因此在中 foreach
它不会成为问题。但是foreach
不是新范围,因此如果您计划重用数组变量,则需要选择不同的名称作为迭代器,以免被覆盖。
$a = array(); // $a is array
foreach ($a as $a) {
// $a is element of original $a
}
// $a is the last element of the original $a array
foreach ($a as $a) // Fail, since $a is not an array anymore