Undefined variable:

This is what I am doing with Highcharts. Wierd thing is my friend used this exact code and my table and it worked fine but when I added it to a view i get:

PHP Error

Undefined variable: final_results

If I intialize $final_results = array(); I get Undefined offset: 0

Can’t seem to win… lol :) I thought I beat this thing.


<?php 





$sql = "SELECT DATE_FORMAT( completion_date, '%b' ) AS name, COUNT( * ) AS data FROM `pms` AS p WHERE completion_date > schduled_date AND completion_date != '0000-00-00' GROUP BY name LIMIT 0 , 30";


$connection = Yii::app()->db; 

$command = $connection->createCommand($sql); 

$results = $command->queryAll(); 




$months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

$x=0;

for($r=0;$r<3;$r++){

	for($m=0;$m<12;$m++){

		

		if (strcmp($results[$r]['name'], $months[$m]) == 0){

			$final_results[$m] += (integer)$results[$r]['data'];		

		}	

		else{

			$final_results[$m] += 0;	

		}

		 //echo $results[$r]['name'];

	}

	//$final_name[$r]['name'] = $results[$r]['name'];

	

}


$final_data[0] = array('name' => 'Result', 'data' => $final_results);

echo '<br/>';

//print_r($final_data);

//$pms->




	 


$this->Widget('ext.highcharts.HighchartsWidget', array(

   'options'=>array(

   

                'series' => $final_data

                ,

				

				

      'title' => array('text' => 'Fruit Consumption'),

      'xAxis' => array(

         'categories' => $months

      ),

      'yAxis' => array(

         'title' => array('text' => 'Fruit eaten')

      ),

/*      'series' => array(

         array('name' => 'Jane', 'data' => array(1, 0, 4)),

		 array('name' => 'Paul', 'data' => array(11, 10, 14)),

		 array('name' => 'Ken', 'data' => array(13, 23, 12)),

         array('name' => 'John', 'data' => array(5, 7, 3))

      )*/

      //'series' => $results

	  

   )

));


?>

Here is the error I am getting:


PHP Error

Undefined variable: final_results 


C:\xampp\htdocs\cmms\protected\views\pms\chart.php(45)


33 $command = $connection->createCommand($sql); 

34 $results = $command->queryAll(); 

35 

36 $months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

37 $x=0;

38 for($r=0;$r<3;$r++){

39     for($m=0;$m<12;$m++){

40         

41         if (strcmp($results[$r]['name'], $months[$m]) == 0){

42             $final_results[$m] += (integer)$results[$r]['data'];        

43         }    

44         else{

45             $final_results[$m] += (integer)0;    

46         }

47          //echo $results[$r]['name'];

48     }

49     //$final_name[$r]['name'] = $results[$r]['name'];

50     

51 }

52 

53 $final_data[0] = array('name' => 'Result', 'data' => $final_results);

54 echo '<br/>';

55 //print_r($final_data);

56 //$pms->

57 

Stack Trace

#0  +

–

 C:\xampp\htdocs\cmms\framework\web\CBaseController.php(119): require("C:\xampp\htdocs\cmms\protected\views\pms\chart.php") 

114             $data=$_data_;

115         if($_return_)

116         {

117             ob_start();

118             ob_implicit_flush(false);

119             require($_viewFile_);

120             return ob_get_clean();

121         }

122         else

123             require($_viewFile_);

124     }

 

#1  +

–

 C:\xampp\htdocs\cmms\framework\web\CBaseController.php(88): CBaseController->renderInternal("C:\xampp\htdocs\cmms\protected\views\pms\chart.php", array(), true) 

83     {

84         $widgetCount=count($this->_widgetStack);

85         if(($renderer=Yii::app()->getViewRenderer())!==null && $renderer->fileExtension==='.'.CFileHelper::getExtension($viewFile))

86             $content=$renderer->renderFile($this,$viewFile,$data,$return);

87         else

88             $content=$this->renderInternal($viewFile,$data,$return);

89         if(count($this->_widgetStack)===$widgetCount)

90             return $content;

91         else

92         {

93             $widget=end($this->_widgetStack);

 

#2  +

–

 C:\xampp\htdocs\cmms\framework\web\CController.php(866): CBaseController->renderFile("C:\xampp\htdocs\cmms\protected\views\pms\chart.php", array(), true) 

861      */

862     public function renderPartial($view,$data=null,$return=false,$processOutput=false)

863     {

864         if(($viewFile=$this->getViewFile($view))!==false)

865         {

866             $output=$this->renderFile($viewFile,$data,true);

867             if($processOutput)

868                 $output=$this->processOutput($output);

869             if($return)

870                 return $output;

871             else

 

#3  +

–

 C:\xampp\htdocs\cmms\framework\web\CController.php(779): CController->renderPartial("chart", array(), true) 

774      */

775     public function render($view,$data=null,$return=false)

776     {

777         if($this->beforeRender($view))

778         {

779             $output=$this->renderPartial($view,$data,true);

780             if(($layoutFile=$this->getLayoutFile($this->layout))!==false)

781                 $output=$this->renderFile($layoutFile,array('content'=>$output),true);

782 

783             $this->afterRender($view,$output);

784 

 

#4  +

– C:\xampp\htdocs\cmms\protected\controllers\PmsController.php(82): CController->render("chart", array()) 

77             'criteria'=>$criteria,

78             

79         ));*/

80          $this-> render('chart', array(

81              //'dataProvider'=>$dataProvider,

82         ));

83      } 

84     /**

85      * Displays a particular model.

86      * @param integer $id the ID of the model to be displayed

87      */

 



Anyone… :)

where you "intialize $final_results = array(); " ???

  1. you not initialize it at all

  2. array() means empty array, no offsets there

When I do I get Unset 0

use this function

http://php.net/manual/en/function.get-defined-vars.php

Hint: assign a value


$final_results[$m] = 0;

instead of trying to increase it (when the key is undefined)


$final_results[$m] += 0;

/Tommy