Sunday, June 9, 2013

Yii pass variable from view to layout

Today I found out how to pass variable from view to layout ..

using these
Yii::app()->params['your_awesome_name'] =  ' Awesome Name Here ';


and to access it , it will be like ..



$awesome_name = Yii::app()->params['your_awesome_name'];
$awesome_name = $awesome_name ? $awesome_name: "Default Awesome Name Here";



PHP : Add Item on array

So I got this problem that I encounter today.
Heres the code


$list_items = array(
array('label'=>'Dashboard'),
array('label'=>'Home','icon' => 'home', 'url'=>'#'),
array('label'=>'Residents', 'url'=>Yii::app()->createUrl('residents')),
array('label'=>'Users', 'url'=>Yii::app()->createUrl('users')),
array('label'=>'Reports'),
array('label'=>'Population Growth', 'url'=> Yii::app()->createUrl('residents/chart/population_growth')),
array('label' => 'Population by Year','url' => Yii::app()->createUrl('residents/chart/population_by_year')),
array('label' => 'Distribution of Population','url' => Yii::app()->createUrl('residents/chart/distribution_population'))
);
foreach ($list_items as $item) {
if ($item['label']===$selected_menu) {
$item['active']=true;
}
}

Code says that if the $selected_menu is equal to the label add an new item with key 'active' and a value of true.




First , ofcourse it doesnt work . 
After reading few comments  , turns out that I need to pass by reference to add new item on the $list_items , through the use of &     *yes thats an ampersand character




The working code now looks like this 



$list_items = array(
array('label'=>'Dashboard'),
array('label'=>'Home','icon' => 'home', 'url'=>'#'),
array('label'=>'Residents', 'url'=>Yii::app()->createUrl('residents')),
array('label'=>'Users', 'url'=>Yii::app()->createUrl('users')),
array('label'=>'Reports'),
array('label'=>'Population Growth', 'url'=> Yii::app()->createUrl('residents/chart/population_growth')),
array('label' => 'Population by Year','url' => Yii::app()->createUrl('residents/chart/population_by_year')),
array('label' => 'Distribution of Population','url' => Yii::app()->createUrl('residents/chart/distribution_population'))
);

#heres the solution, using the ampersand
foreach ($list_items as &$item) {
if ($item['label']===$selected_menu) {
$item['active']=true;
}

}


Yii HighChart create Bar Chart

Heres a code on how to create Bar Chart using Yii extension HighChart



$this->widget('ext.highcharts.HighchartsWidget',
array(
'id' => 'something',
'options'=> array(
'chart' => array(
'defaultSeriesType' => 'bar',
'style' => array(
'fontFamily' => 'Verdana, Arial, Helvetica, sans-serif',
),
),
'title' => array(
'text' => 'Distribution of Population',
),
'xAxis' => array(
'title' => array(
'text' => 'Place',
),
'categories' => array('Rural','Urban'),
'labels' => array(
'step' => 1,
'rotation' => 0,
'y' => 20,
),
),
'yAxis' => array(
'title' => array(
'text' => 'Number of Population',
),
),
'series' => array(
array(
'name' => 'Boys',
'data' => array(20,60),
),
array(
'name' => 'Girls',
'data' => array(80,120),
),

)
)
)
);

Yii Highchart Create pie chart

Heres a code on how to create Pie Chart in Yii HighChart







Widget('ext.highcharts.HighchartsWidget', array(
'options'=>array(
'title' => array('text' => 'Population By Year'),
'chart' => array('renderTo' =>'charts'),
'credits' => array('enabled' => false),
'series' => array(
array(
'type'=>'pie',
'name'=>'Browser Awesome',
'data'=>array(
array('Firefox',45.0),
array('Internet Explorer',50.0),
array('Internet Explorer',250.0),
array('Internet Explorer',300.0),
),
),
),
),
));

?>