user3423149
user3423149

Reputation: 159

handle the input from checkboxlist on controller in yii framework

I am creating a checkbox list, and I could like to calculate the price when the user checks on the box.

I have create a checkboxlist in the view part

<?php echo CHtml::checkboxList('cblist_addons','',
CHtml::listData(Addon::model()->findAll("car_type_id=1"),'id','concatenedLabel'),
array('separator'=>'','template'=>'{label}{input}','class'=>'cblist_addons'));?> 

This one works fine. But in the controller part, I would like to generate a string like [a, b, c]

$str_addons = "";

if(isset($_POST['cblist_addons']) && !empty($_POST['cblist_addons']) ) {
foreach($_POST['cblist_addons'] as $addon) {
if ($addon.checked){
    $str_addons .= $addon.val(). ', ';  
}}
$str_addons = substr($str_addons, 0, -2); //to del the last comma
}


$criteria = new CDbCriteria();              
$criteria->select = 'price'; 
if ($str_addons != ''){
$criteria->condition = 'id in (:cblist_addons)';
$criteria->params = array(':cblist_addons'=> $str_addons) ;     
}
else{
$criteria->condition = 'id in (1,2,3,4) ';  //the path go into this else part with no error
}
$model_addons = Addon::model()-> findAll ($criteria);

Seems I am fail in getting the object from view part. I am newbie in yii and php I have just tried to solve this problem in few days so I hope anyone can help me.

Let me explain more. I am creating a checkboxlist and when I press the button or check the checkbox, an ajax function will be called and the price will be calculated and display in the view part.

Upvotes: 0

Views: 642

Answers (1)

Let me see
Let me see

Reputation: 5094

You do not need to check like this

($addon.checked)

as $_POST['cblist_addons'] will only contain the values you have checked.

So you should change this

if(isset($_POST['cblist_addons']) && !empty($_POST['cblist_addons']) ) {
foreach($_POST['cblist_addons'] as $addon) {
if ($addon.checked){
    $str_addons .= $addon.val(). ', ';  
}}
$str_addons = substr($str_addons, 0, -2); //to del the last comma
}

to

if(isset($_POST['cblist_addons']) && !empty($_POST['cblist_addons']) ) {
$str_addons = implode(',',$_POST['cblist_addons']);
}

You can join all the values in an array separated with comma using implode

Now in your criteria you are using this

$criteria->condition = 'id in (:cblist_addons)';

But You can add In condition using criteria like this

$criteria->addInCondition($column, $values);

Where $column is the name of the column and $values is the array of the values 

SO in short your overall code will look like this

    $criteria = new CDbCriteria();              
    $criteria->select = 'price'; 
    if(isset($_POST['cblist_addons']) && !empty($_POST['cblist_addons']) ){
    $criteria->addInCondition('id', $_POST['cblist_addons']);
    } else{
    $criteria->addInCondition('id', array(1,2,3,4));
    }
$model_addons = Addon::model()-> findAll ($criteria);

Second thing:-

In php you do not access properties of an object using . (dot). You will have to use -> for that.

Upvotes: 1

Related Questions