Yii2 get product id seperated by comma

北战南征 提交于 2019-12-06 15:54:07

问题


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:


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);


来源:https://stackoverflow.com/questions/53075003/yii2-get-product-id-seperated-by-comma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!