0

For some reason my code is an array one minute and not the next. I'm just learning php and I can't figure this out. I've used this page to figure most of this out: How to add two Google charts on the one page?

My code is also used on a page that shows the data as a table. On that page it works fine....but for some reason I'm getting Invalid argument supplied for foreach() on the pie charts page...and it's saying it's not an array. I'm not sure how to go about fixing this.

The url I'm using is ?action=scores&data=pie so it should list all of the universities in "get_universities()" (there's 3 there).

Can anyone help please?

The switch:

// 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;

The pie graphs:

    // 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 ?>

The table view (which works):

<?php foreach ($uni as $uni) : ?>
<td>
    <a href="?uni=<?php echo $uni['university_id']; ?>">
        <?php echo $uni['university_name']; ?>
    </a>
</td>
<etc>
Community
  • 1
  • 1

1 Answers1

0

The problem is with your foreach statement:

foreach ($uni as $uni) :

Here you override the $uni variable. Use different names for the collection and item, eg:

foreach ($uni as $theUni) :
// also change instances of $uni below

EDIT

The above is wrong. The parsing of the first parameter of foreach happens only once so in that foreach it won't be a problem. However a foreach is not a new scope, so if you plan to reuse the array variable, you need to choose a different name as the iterator so it won't get overridden.

$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
fejese
  • 4,601
  • 4
  • 29
  • 36