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
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
}
?>
期待此结果
答案 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
答案 1 :(得分: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
}
输出:
您可以将$row
数组的内容插入数据库。
修改强>
请注意,上述答案基于您提出的具体问题。
我仍然不明白你到底在做什么。如果您试图存储订单的详细信息,那么这是一种很难实现的方法!您应该保留单独的数据库表并使用关系连接在一起。这就是关系数据库的用途!
例如,让两个表格为tblOrderMaster
和tblOrderDetails
。
此处,在tblOrderMaster
表中,主键将是order_id
,您可能会存储总金额,折扣金额,总服务税,订单日期,客户ID,订单状态等等。
在tblOrderDetails
表中,您将有几行,我们将在其中存储特定订单的每个产品。例如,它将存储订单ID(外键),产品ID,单价,数量,税,总计(unit_price x数量)等。
我相信这是正确的方式。