Yii2 获取以逗号分隔的产品 ID

2022-01-07 00:00:00 php yii2 phpmyadmin implode

我已经关注了这个人的教程

I have followed this guy tutorial

并获得所有产品的总价,现在我创建了一个名为 order 的表,其中包含(total_price 和 product)列
现在我想要选择(添加到购物车)的所有产品 ID 用逗号分隔并保存到

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

我已经通过控制器将 total_price 保存在数据库中.

I've saved total_price in db through controller though.

提前致谢

推荐答案

使用 PHP implode() 和 explode() 函数来保存逗号分隔的数据在数据库中

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

implode() 函数返回一个字符串,其中包含以相同顺序表示的所有数组元素的字符串

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() 函数返回一个字符串数组.

explode() function returns an array of strings.

$string = "1, 2, 3, 4, 5";
explode(', ', $string);

相关文章