donglangtun1850 2019-03-27 13:41
浏览 85

如何将php数组读入变量

I'm using ajax to post my json data ( to ci controller ) and then json_decode to read my data.

this data from table

Array
(
    [Detail] => Array
        (
            [0] => Array
                (
                    [row0] => Array
                        (
                        )

                )

            [1] => Array
                (
                    [row1] => Array
                        (
                            [0] => Array
                                (
                                    [cell0] => 04019
                                )

                            [1] => Array
                                (
                                    [cell1] => 
                                )

                            [2] => Array
                                (
                                    [cell2] => 2.00
                                )

                            [3] => Array
                                (
                                    [cell3] => 
                                )

                            [4] => Array
                                (
                                    [cell4] => 4530000
                                )

                            [5] => Array
                                (
                                    [cell5] => 
                                )

                            [6] => Array
                                (
                                    [cell6] => 
                                )

                        )

                )


        )

)

I want to read data and update the database using the ci model

    $data = array(
     'Customer'  => $this->input->post('Customer'),
     'Total' => $this->input->post('Total'),
     'Detail' => $this->input->post('Detail')
    );

    $json = json_decode($data['Detail'],true);

    $Sql ="";
    $row=0;
    foreach ($json as $doc)
    {
        if ($row !=0)
        {
         $Sql .= ' Insert Detail' + $doc[0]->cell0 . ' ' . $doc[2]->cell2  . ' ' . $doc[4]->cell4;
        }
         $row++;
    }

print_r($Sql);

but it's not working,

How to read the data array and save it to the Sql var. Thanks

  • 写回答

1条回答 默认 最新

  • dongqiang8683 2019-03-27 13:59
    关注

    That's not how Codeigniter works.

    You should respect MVC, having something like this:

    Controller:

    $details = json_decode($data['Detail'],true);
    
    $this->load->model('my_model');
    
    foreach($details as $detail)
    {
        $this->my_model->add_detail($detail);
    }
    

    Model:

    function add_detail($detail)
    {
        $this->db->insert('table_name', $detail);
    }
    

    Read about Codeigniter Active Record

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度