2017-05-26 61 views
0
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))) 

我有一個像這樣的數組..每個訂單數組中的訂單數組和數組。現在我需要將這些數組添加到數據庫中,每行中的訂單和產品由<br/>分隔。這怎麼可能?提前致謝。在PHP中向數據庫添加多維數組

我用它來打印代碼。

<?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 
} 
?> 

期待這個結果

enter image description here

enter image description here

enter image description here

+0

爲什麼你的意思是「'''現在我需要將這些數組添加到數據庫中,每行中的訂單和產品都由a'''」分隔? – mi6crazyheart

+0

對不起,突破是失蹤。剛剛編輯 –

+0

再次你的意思是'''產品由
'''分隔?你能舉出你預期的產出嗎? – mi6crazyheart

回答

1

我希望這將讓你開始:

$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 
} 

輸出:

enter image description here

您可以插入$row數組的內容到數據庫中。


編輯

請注意,上面的回答是基於你問你的具體問題。

我還是不明白你到底在做什麼。如果您試圖存儲訂單的詳細信息,那麼這是一個很糟糕的方法來完成它!您應該保留單獨的數據庫表並使用關係連接在一起。這是關係數據庫的用途!

例如,讓兩個表爲tblOrderMastertblOrderDetails

這裏,在tblOrderMaster表,主鍵將是order_id,你大概會要去商店總量,折扣金額,總服務稅,訂單日期,客戶ID,訂單狀態等

tblOrderDetails表中,您將有多行,其中我們將存儲特定訂單的每個產品。例如,它會存儲訂單ID(外鍵),產品ID,單價,數量,稅金,總金額(單位價格×數量)等等。

這是我認爲的正確方法。

+0

仍然如何將它添加到數據庫 –

+0

數據將在'$ row'變量中可用。只需將數組中存在的數據插入數據庫! –

+0

我無法添加到數據庫。但我可以打印它。我用數據庫的屏幕截圖更新了我的問題 –

2

嘗試此示例代碼。

$orderList[1001] = 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', 
       'quantity' => 3, 
       'expdate' => 'May 27th 2017', 
       'exptime' => '11:00 PM to 12:00 AM', 
       'expdtype' => 'Mid night delivery' 
      ) 
    ); 

$orderList[1002] = array(
     0 => array(
       'name' => 'Red & Yellow Roses', 
       'code' => 'KB-P-6', 
       'qty' => 1, 
       'type' => 'main', 
       'price' => 800, 
       'stock' => 9, 
       'image' => 'birthday-red-roses.jpg', 
       'quantity' => 10, 
       'expdate' => 'May 27th 2017', 
       'exptime' => '11:00 PM to 12:00 AM', 
       'expdtype' => 'Mid night delivery' 
      ) 
    ); 


echo '<pre>'; 
print_r($orderList); 
echo '<pre>'; 

$productList = array(); 

foreach ($orderList as $key => $value) 
{ 
    $names = array(); 
    $quantity = array(); 

    foreach ($value as $key1 => $value1) 
    { 
     $names[] = $value1['name']; 
     $quantity[] = $value1['quantity']; 
    } 

    $productList[$key]['names'] = implode('<br>', $names); // Used `:` insted of '<br>' for separator. But, You can use whatever you want. But, better don't use any HTML tag. 
    $productList[$key]['quantity'] = implode('<br>', $quantity); 
} 

echo '<pre>'; 
print_r($productList); 
echo '<pre>'; 


// Sample DATA INSERTION Query by PDO 
try { 
    $conn = new PDO('mysql:host=localhost;dbproductName=someDatabase', $userproductName, $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    // Prepare the query ONCE 
    $stmt = $conn->prepare('INSERT INTO someTable VALUES(:name, :quantity)'); 

    foreach ($productList as $key => $value) { 
     $productName = $value['names']; 
     $productQuantity = $value['quantity']; 

     $stmt->bindParam(':name', $productName); 
     $stmt->bindParam(':quantity', $productQuantity); 

     $stmt->execute(); 
    } 

} catch(PDOException $e) { 
    echo $e->getMessage(); 
} 

// Ref : https://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338 
+0

我在問如何將這個數據庫作爲不同的訂單行添加到數據庫中。我能夠打印它 –

+0

由於輸出是以數組的形式,所以你可以在任何循環中使用該數組,並相應地插入查詢以將它們插入到數據庫中。難度如何? – mi6crazyheart

+0

@SarathHari在代碼示例底部添加了一個示例PDO查詢插入操作,請檢查以供參考從數據陣列中插入記錄的問題 – mi6crazyheart