dongxing2263 2017-05-26 09:34
浏览 105
已采纳

在PHP中将多维数组添加到数据库中

Array ( [0] => Array ( [0] => Array ( [name] => Heart Choclates [code] => LFB-P-10 [qty] => 1 [type] => main [price] => 1200 [stock] => 5 [image] => choclates-valentines-day.jpg [quantity] => 12 [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) [1] => Array ( [name] => Birthday Pink [code] => KB-P-5 [qty] => 1 [type] => addon [price] => 600 [stock] => 7 [image] => pink-roses.jpg [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) ) [1] => Array ( [0] => Array ( [name] => Red & Yellow Roses [code] => KB-P-6 [qty] => 1 [type] => main [price] => 800 [stock] => 9 [image] => birthday-red-roses.jpg [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) [1] => Array ( [name] => Signature Cake [code] => KB-P-7 [qty] => 1 [type] => addon [price] => 0 [stock] => 9 [image] => signature-cake.jpg [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) [2] => Array ( [name] => Truffle Cake [code] => KB-P-8 [qty] => 1 [type] => addon [price] => 10 [stock] => 7 [image] => truffle-cake.jpg [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) ) )

I have an array like this.. an array of orders and array of products inside each array of orders. Now i need to add these arrays into database with orders in each row and products seprated by a <br/> . How is it possible? Thanks in advance.

I used this for each to print the code.

<?php
    session_start();
    error_reporting(0);
    print_r($_SESSION["products"]);
    foreach($_SESSION["products"] as $row => $temp){    
?>                                        
<div>
<?php
   foreach($temp as $innerRow => $cart_itm){
    ?>
        <div><?php echo $cart_itm['code']; ?></div>
    <?php
    }
    ?>
</div>
<?php
}
?> 

Expecting this Result

enter image description here

enter image description here

enter image description here

  • 写回答

2条回答 默认 最新

  • dragon4808 2017-05-26 11:20
    关注

    I hope this will get you started:

    $data = array(
        array(
            array(
                'name'  => 'Heart Choclates',
                'code'  => 'LFB-P-10',
                'qty'   => '1' 
            ),
            array(
                'name'  => 'Birthday Pink',
                'code'  => 'KB-P-5',
                'qty'   => '1' 
            )
        ),
        array(
            array(
                'name'  => 'Red & Yellow Roses',
                'code'  => 'KB-P-6',
                'qty'   => '1'
            ),
            array(
                'name'  => 'Signature Cake',
                'code'  => 'KB-P-7',
                'qty'   => '1'
            ),
            array(
                'name'  => 'Truffle Cake',
                'code'  => 'KB-P-8',
                'qty'   => '1'
            )
        )
    );
    
    foreach( $data as $row_item )
    {
        $row = array();
    
        foreach( $row_item as $product )
        {
            foreach( $product as $key => $value )   
            {
                if( isset( $row[ $key ] ) )
                    $row[ $key ] .= $value . ' <br> ';
                else
                    $row[ $key ] = $value . ' <br> ';        
            }
        }
    
        //--- 
        echo 'Row: <br>';
        var_dump( $row );
        echo '<br><br>';
    
        //-- insert the contents of $row array into the database
    }
    

    Output:

    enter image description here

    You can insert the contents of $row array into database.


    EDIT

    Please note that, the above answer is based on your specific question that you asked.

    I still didn't understood what exactly you are doing here. If you are trying to store the details of an order, then this is a poor method of accomplishing it! You should keep separate database table and use a relationship to join together. This is what a relational database is for!

    For example, let the two tables be tblOrderMaster and tblOrderDetails.

    Here, in the tblOrderMaster table, the primary key will be the order_id and you would probably gonna store the total amount, discount amount, total service tax, order date, customer id, order status, etc.

    And in the tblOrderDetails table, you are gonna have several rows, in which we are gonna store each products of a particular order. For example, it will store order id (foreign key), product id, unit price, quantity, tax, total (unit_price x quantity), etc..

    This would be the proper way, I believe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)