Can anybody take a look at my code and tell me as to why i’m getting an error?
The following is my code
public function actionCreate()
{
$connection=Yii::app()->db;
$model=new BusType;
$model2=new BusProducts;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
//Get the number of models you will be working with
$maxRows =count($_POST['quantity']);
if(isset($_POST['BusType']))
{
$model->attributes=$_POST['BusType'];
$model2->attributes=$_POST['BusProducts'];
if($model->save())
{
$bus_type_id=$model->bus_type_id;
for ($i = 0; $i < $maxRows ; $i++)
{
$quantity = $_POST['quantity'][$i];
$product_id = $_POST['product_id'][$i];
$cat_id = $_POST['cat_id'][$i];
$subcat_id = $_POST['subcat_id'][$i];
$sql = "insert into bus_products (bus_type_id,quantity,product_id,cat_id,subcat_id) values (:bus_type_id,:quantity,:product_id,:cat_id,:subcat_id)";
$command=$connection->createCommand($sql);
$command->bindParam(":bus_type_id",$bus_type_id,PDO::PARAM_INT);
$command->bindParam(":quantity",$quantity,PDO::PARAM_INT);
$command->bindParam(":product_id",$product_id,PDO::PARAM_INT);
$command->bindParam(":cat_id",$cat_id,PDO::PARAM_INT);
$command->bindParam(":subcat_id",$subcat_id,PDO::PARAM_INT);
$command->execute();
}
$this->redirect(array('view','id'=>$model->bus_type_id));
}
}
$this->render('create',array(
'model'=>$model,
'model2'=>$model2,
));
}
The error which i am getting is: mb_strlen() expects parameter 1 to be string, array given .
I have a field called quantity. This field is in a loop. i want to save all the values of quantity in the loop for which i need to convert the filed quantity to an array as follows
<? echo $form->textField(BusType::model(),'quantity[]',array('size'=>10,'maxlength'=>255));
?> // as an array
If i put the code as follows the error goes.
<? echo $form->textField(BusType::model(),'quantity',array('size'=>10,'maxlength'=>255));
?> //not an array
But i need it be an array to update the db correctly.
How do i go about this. Suggest if there is any other way of doing it.