Reputation: 219
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
Upvotes: 0
Views: 287
Reputation: 6608
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:
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.
Upvotes: 1
Reputation: 5997
Try this sample code.
$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
Upvotes: 2