Yii2 get product id seperated by comma

2019-04-17 05:53发布

I have followed this guy tutorial

and got total price of all products and now I have created a table called order which contains (total_price and product) columns
now I want what all products ID which are selected (added to cart) separated by comma and be saved into

$model= new orders ()
$model->product (product id added in cart separated by comma)in db

I've saved total_price in db through controller though.

Thanks in advance

1条回答
ら.Afraid
2楼-- · 2019-04-17 06:09

Use PHP implode() and explode() function to save comma seprated data in DB

implode() function returns a string containing a string representation of all the array elements in the same order

$model= new orders ();
$array = [1, 2, 3, 4, 5];
$model->product = implode(', ', $array);
$model->save();

explode() function returns an array of strings.

$string = "1, 2, 3, 4, 5";
explode(', ', $string);
查看更多
登录 后发表回答