0

I have tried doing research for about an hour on extract in PHP and assoc arrays but have not found the answer I need.

The problem is I am trying to create a pie chart using a google visualization API but am unable to pass data to the function because the extract function I am using in my PHP file is returning 0 meaning that no variables were successfully extracted.

Here is all of the relevant code for making the pie chart.

and here is a picture of the resulting output (ps the comment where it says load api is where the above php code ends):

$GradeDistQuery = "select avg(PCT_A),avg(PCT_B),avg(PCT_C),avg(PCT_DF) from GradeDistribution where Course = $CourseNumRow->Course and Subject='$SubjectRow->Subject' ";
$GradeDistResult = mysql_query($GradeDistQuery);
$GradeRow=mysql_fetch_assoc($GradeDistResult); // for the amount of rows the query returns echo the CourseTitle and Course(number)
//-display the result of the array  
//TODO: Limit 1 result per class title ie CS as field of wrk stdy should only appear one time not 3
echo "<ul>\n";
echo "<p> Average percent of Students Who Got an A: </p>";
echo "<p class='rows'>".$GradeRow['avg(PCT_A)']."</p>";
echo "<p> Average percent of Students Who Got a B: </p>";
echo "<p class='rows'>".$GradeRow['avg(PCT_B)']."</p>";
echo "<p> Average percent of Students Who Got a C: </p>";
echo "<p class='rows'>".$GradeRow['avg(PCT_C)']."</p>";
echo "<p> Average percent of Students Who Got a D or a F: </p>";
echo "<p class='rows'>".$GradeRow['avg(PCT_DF)']."</p>";
echo "</ul>";   
$GradeTable = extract($GradeRow);
echo "<p> extract return value = $GradeTable</p>";

      <!-- en load api -->
<script type="text/javascript" src="http://www.google.com/jsapi"></script>

<script type="text/javascript">
    //load package
    google.load('visualization', '1', {packages: ['corechart']});
</script>

<script type="text/javascript">
    function drawVisualization() {
        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
            ['Grade', ' average percent of students who got that grade'],
            <?php
            while( $Graderow = mysql_fetch_assoc($GradeDistResult) ){
                echo "<p> hello friend</p>";
                extract($GradeRow);
                //echo "['{$name}', {$ratings}],";
            }
            ?>
        ]);
        // Create and draw the visualization.
        new google.visualization.PieChart(document.getElementById('visualization')).
        draw(data, {title:"Average Grade Distribution for: "});
    }
    google.setOnLoadCallback(drawVisualization);
</script>

Here is a picture of the resulting output:

enter image description here

Any suggestions to as why extract is returning 0?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TeeseCaprice
  • 55
  • 1
  • 13
  • 4
    Because `avg(PCT_DF)` and others are not valid variable names – u_mulder Nov 15 '16 at 18:14
  • 1
    Please stop using the `mysql_*` functions. They were deprecated in PHP 5.5, which is so old it no longer even receives security updates, and completely removed in PHP 7. Use `PDO` or `mysqli_*` instead. See https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php for details. – ChrisGPT was on strike Nov 15 '16 at 18:14
  • 3
    1. Just use the array instead of `extract()` . 2. to get better names try `SELECT avg(PCT_A) as A, etc...` – AbraCadaver Nov 15 '16 at 18:18
  • Thank you for all the comments I appreciate it after opening the console log I believe u_mulder is correct as it say the variable names $GradeRow is not defined. Again thank you and I will try and update to mysqli functions – TeeseCaprice Nov 15 '16 at 18:48
  • Hey so I figured out a work around for the undefined variable. you can just have a small in loo of doing a ajax. I know this is hackish but im on crunch time lol – TeeseCaprice Nov 15 '16 at 19:48

0 Answers0