aramiz
(Yaramiz)
November 20, 2010, 10:11am
1
Hi to all. I have a table Cars and Options. Just created a table Cars_Options. In the relations of Cars model i set that the:
<?php
'services'=>array(self::MANY_MANY, 'Options','Cars_options(cars_id, options_id)')
?>
With which widjet i can render $model->options array in Cars view?
I just want to use widjet to display Cars related Options.
aramiz
(Yaramiz)
November 20, 2010, 10:34am
2
I have no idea how to display an array . It sims simple but i dont know how to do this simple thing.
aramiz
(Yaramiz)
November 20, 2010, 10:54am
3
I try to render it by
<?php for($i=0;$i<2;$i++){
echo $model->options[$i]->option."<br>";
}
?>
But i think there must be an easier and more corect way.
jacmoe
(Jacob Moen)
November 20, 2010, 11:32am
4
You could do:
foreach( $model->options as $option) {
echo $option . '<br/>';
}
Use the PHP docs.
No, seriously: they are great.
And you can find lots of nice tips in the comments.
-> http://www.php.net/m...res.foreach.php
aramiz
(Yaramiz)
November 20, 2010, 11:39am
5
Thanks for reply. I know that i can do it by using for or foreach cycle. But is there any way to do it with widget?
jacmoe
(Jacob Moen)
November 20, 2010, 11:52am
6
Oh, right.
Create a widget:
class CarOptions extends CWidget {
public $options = null;
public function getOptions() {
return (isset($this->options) ? $this->options : array());
}
public function run() {
// this method is called by CController::endWidget()
$this->render('carOptions');
}
}
Then, in the directory of the widget, create a directory called ‘views’ and put this in it:
<?php foreach($this->getOptions() as $option) : ?>
<div>
<?php echo $option; ?>
</div>
<?php endforeach; ?>
It should be named ‘carOptions.php’.
Then, in your Car view:
<?php $this->widget('CarOptions', array('options' => $options)); ?>
Not tested by me, but it shows one possible way of doing it.
aramiz
(Yaramiz)
November 20, 2010, 11:57am
7
Ok. Its cool. But is there any way to use standar widgets? Like: CListView or CGridView.
jacmoe
(Jacob Moen)
November 20, 2010, 12:21pm
8
You can use anything in a widget.
Think of it as a mini-controller with it’s own view.
aramiz
(Yaramiz)
November 20, 2010, 1:30pm
9
Ok. Another question: i’m using many to many relation. Options of car i’m setting via using checkboxlist. How can i update cars_options table with selected id?
jacmoe
(Jacob Moen)
November 20, 2010, 2:28pm
10
Look into CHtml::listData which can be used to generate the $options array for dropdownbuttons, checklistsboxes, etc.
CHtml::CheckBoxList('Options', null, CHtml::listData(CarOptions::model()->findAll(), 'id', 'name')