Yii2获取以逗号分隔的产品ID [英] Yii2 get product id seperated by comma

查看:176
本文介绍了Yii2获取以逗号分隔的产品ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遵循了这个家伙教程

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 内置() 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);

这篇关于Yii2获取以逗号分隔的产品ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆