将多维数组插入codeigniter购物车

时间:2014-01-20 02:14:43

标签: php arrays codeigniter-2

我正在使用codeigniter和它的购物车库建立在线商店。我有这个数组

$data = Array
(
    Array
        (
            [id] => BM101
            [qty] => 1
            [price] => 95
            [name] => Legal Structures & Tax Accounting
            [options] => Array
                (
                    [course_hours] => 4
                    [course_description] => This course will review the alternative legal forms that may be used to conduct a building/construction business, including proprietorship, partnerships, joint ventures, limited partnerships, C corporations and S corporations. The pros and cons of each type of entity will be examined in terms of personal liability exposure. The income tax and social security tax consequences of each type of entity, as well as the impact on fringe benefit and retirement programs, will be explored.
                    [class_date] => 2014-03-05
                    [class_time] => 1:00 p.m. - 4:30 p.m.
                    [class_instructor] => Richard Allison
                )

        )

    Array
        (
            [id] => BM102
            [qty] => 1
            [price] => 95
            [name] => Risk Management for Residential General Contractors
            [options] => Array
                (
                    [course_hours] => 4
                    [course_description] => This course will provide a risk management approach to the most common property and liability exposures facing the residential general contractor. A broad review of the major coverage’s builders must have to protect their businesses will be covered. Those coverage’s include: general liability, workers compensation, builders risk, and auto. Other items discussed will be include: contractor tools, office contents, and bonds. This will be a practical approach on how to assess these risks and how to insure them or pass them along to others.
                    [class_date] => 2014-04-03
                    [class_time] => 8:30 a.m. - 12:00 p.m.
                    [class_instructor] => Erik Anderson
                )

        )

)

当我使用$ this-> cart->插入($ data)时,仅插入购物车的最后一项。如何将多维数组插入购物车? 我也做循环

foreach ($data as $item){
$this->cart->insert($item);
}

也只插入最后一项。

2 个答案:

答案 0 :(得分:0)

我认为这与第1项名称字段中的&符号有关。 This Stack Overflow answer should have some more info on getting around that

答案 1 :(得分:0)

而不是在CI中循环,更好的方法是使用:

$this->db->insert_batch('table_name', $data)

insert_batch()用于一次在多维数组中插入多个项目。