if ($cart = $this->cart->contents())
{
foreach ($cart as $item){
$order_detail = array(
'res_id' =>$this->session->userdata('menu_id[]'),
'customer_id' =>$coustomers,
'payment_id' =>$payment,
'name' => $item['name'],
'productid' => $item['id'],
'quantity' => $item['qty'],
'price' => $item['price'],
'subtotal' => $item['subtotal']
);
}
print_r($order_detail); exit;
when the foreach loop ends, only the last iteration value is left. I need all the values to be within the array.
Because order_detail will overwrite each time. Use array instead of simple variable.
$order_detail = array();
if ($cart = $this->cart->contents())
{
foreach ($cart as $item){
$order_detail[] = array(
'res_id' =>$this->session->userdata('menu_id[]'),
'customer_id' =>$coustomers,
'payment_id' =>$payment,
'name' => $item['name'],
'productid' => $item['id'],
'quantity' => $item['qty'],
'price' => $item['price'],
'subtotal' => $item['subtotal']
);
}
print_r($order_detail); exit;
Change this line
$order_detail = array(..);
to
$order_detail[] = array(..);
try this
first define the array
$order_detail=array();
array_push($order_detail, array(...));
array declaration must be outside the loop.
Related
I need to fill an array with a dynamic list of products.
To do so, I'm using the following code:
$list_array = array(
$products[] = array(
'SKU' => '0001',
'Title' => 'Bread',
'Quantity' => '',
),
$products[] = array(
'SKU' => '0002',
'Title' => 'Butter',
'Quantity' => '',
)
);
return $list_array;
It works fine if I know every product in the array.
But in my use case I have no idea which products are in the array.
So I want to fill the array with dynamic data.
I came up with something this:
$products = get_posts( 'numberposts=-1&post_status=publish&post_type=product' );
foreach ( $products as $product ) {
$products[] = array(
'SKU' => $product->id,
'Title' => $product->post_title,
'Quantity' => '',
),
}
return $products;
I know there is something really wrong with the array. But I couldn't figure out what it is.
The code you submitted cannot work. The short syntax $a[] = ... is to append data to the $a array, for example:
$a = [];
$a[] = 1;
$a[] = 2;
// $a = [1, 2]
You can also do it in a more efficient way with a map function:
function reduce($product)
{
return array(
'SKU' => $product->id,
'Title' => $product->post_title,
'Quantity' => '',
);
}
return array_map('reduce', $products);
It will execute the function reduce and replace value for each element of you array. Complete doc here: https://www.php.net/manual/en/function.array-map.php
Your problem is that you are overwriting the $products array that you are looping over inside the loop. Change the name of the variable in the loop to fix that:
$list_array = array();
foreach ( $products as $product ) {
$list_array[] = array(
'SKU' => $product->id,
'Title' => $product->post_title,
'Quantity' => ''
);
}
return $list_array;
How do I add a variable to one of the variables in the item codeigniter price (php)
i have a variable :
$shipping = 10000;
and cart session :
foreach ($this->cart->contents() as $item){
if($item['discount'] == "0"){
$harga_before = $item['before'];
$harga_after = $item['price'];
}else{
$harga_before = $item['before'];
$harga_after = $item['price'];
}
// result is
$data_order = array(
'id_product' => '1',
'product_name' => 'ferari',
'harga_fix' => '100' + $shipping,
'harga_before' => '150',
),
array(
'id_product' => '2',
'product_name' => 'lamb',
'harga_fix' => '70',
'harga_before' => '85',
);
}
how to add $shipping to one of the item prices? thank you
I have a question about the passing of multidimensionsal arrays in functions.
I have the following code (arrays), there are a few more arrays.
$product = array();
$product[] = array(
'Category' => "Smartphone",
'Seller' => "Apple",
'Product' => "<img src='Bild_iPhone_8.JPG' alt='iPhone 8' height='130px' />",
'Price' => 836,
'Selection' => "Mark1"
);
$product[] = array(
'Category' => "Smartphone",
'Seller' => "Samsung",
'Product' => "<img src='Bild_Galaxy_S8.JPG' alt='Galaxy S8' height='130px' />",
'Price' => 649,
'Selection' => "Mark2"
);
$product[] = array(
'Category' => "Notebook",
'Seller' => "Apple",
'Product' => "<img src='Bild_MacBookAir.JPG' alt='MacBook Air' width='130px' />",
'Price' => 999,
'Selection' => "Mark3"
);
I dont know now exactly how to put the parameters in a function.
I tried already a lot of possibilities, but nothin is working.
It should be possible to make requests with forms.
Can you please help me? Thanks a lot!
Greets, Mikra
I don't get your problem. You could pass the whole multi-dimensional array to a function the same way, you pass a one-dimensional array.
$products = [];
$products[] = [
'Category' => "Smartphone",
'Seller' => "Apple",
'Product' => "<img src='Bild_iPhone_8.JPG' alt='iPhone 8' height='130px' />",
'Price' => 836,
'Selection' => "Mark1"
];
// ... add more products
processProducts($products);
and later in your code
function processProducts($products) {
foreach ($products as $product) {
processProduct($product['Category'], $product['Seller'], and so on...);
// or..
processProduct($product);
}
}
function processProduct($category, $seller, ...) {
}
or
function processProduct($product) {
}
Use Array.push to insert multiple items to single array.
$product = array();
$item = array(
'Category' => "Smartphone",
'Seller' => "Apple",
'Product' => "<img src='Bild_iPhone_8.JPG' alt='iPhone 8' height='130px' />",
'Price' => 836,
'Selection' => "Mark1"
);
array_push($product,$item);
print_r($product);
and then you can pass the main $product array to any function.
calculatePrice($product);
To access first element use $product[0] or use foreach to loop over all.
I'm working on this e-commerce site and I'm trying to create a jSON array containing the cart items in PHP.
So far I have:
for ($i=0; $i < count($_SESSION['cart']); $i++) {
$prodid = $_SESSION['cart'][$i][0];
$sizeId = $_SESSION['cart'][$i][1];
$colorId = $_SESSION['cart'][$i][2];
$qty = $_SESSION['cart'][$i][3];
$inslagning = $_SESSION['cart'][$i][4];
$wrapCost += ($inslagning == 'YES' ? 20 : 0);
$row = get_product_buy($prodid, $sizeId, $colorId);
$prodname = $row['prodname'];
$color = $row['color'];
$size = $row['size'];
$prodCatid = $row['catid'];
$image = $row['biggerimage'];
$box = $row['box_number'];
for ($j=0;$j<$qty;$j++) {
$cart = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
}
}
I know I have the $cart var inside the loop which is probably wrong. The end result should be like this:
$cart = array(
array(
'reference' => '123456789',
'name' => 'Klarna t-shirt',
'quantity' => 1,
'unit_price' => $att_betala * 100,
'discount_rate' => 0,
'tax_rate' => 2500
),
array(
'reference' => '123456789',
'name' => 'Klarna t-shirt',
'quantity' => 1,
'unit_price' => $att_betala * 100,
'discount_rate' => 0,
'tax_rate' => 2500
)
);
All help is appreciated!
You have to append a new child to $cart instead of overwriting it. To append values to an array (the easy way), use $array[] = …. PHP increments the child's ID automatically.
Not required, but please initialize $cart first and use descriptive variables.
To inspect an array (or other data), use var_dump.
// Initialize an empty array. Not needed, but correct to do.
$cart = array();
for ($i=0; $i < count($_SESSION['cart']); $i++) {
$prodid = $_SESSION['cart'][$i][0];
$sizeId = $_SESSION['cart'][$i][1];
$colorId = $_SESSION['cart'][$i][2];
$qty = $_SESSION['cart'][$i][3];
$inslagning = $_SESSION['cart'][$i][4];
$wrapCost += ($inslagning == 'YES' ? 20 : 0);
$row = get_product_buy($prodid, $sizeId, $colorId);
$prodname = $row['prodname'];
$color = $row['color'];
$size = $row['size'];
$prodCatid = $row['catid'];
$image = $row['biggerimage'];
$box = $row['box_number'];
// Append products $qty times.
for ($productCount=0; $productCount<$qty; $productCount++) {
// Append a new product to $cart.
$cart[] = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
}
}
Use like this
$cart[] = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
Try Using
for ($j=0;$j<$qty;$j++) {
$cart[] = array(
'reference' => '123456789',
'name' => $prodname,
'quantity' => $qty,
'unit_price' => $price,
'discount_rate' => 0,
'tax_rate' => 2500
);
}
$json_enc = json_encode($cart);
You are not appending to the $cart variable, you are overwriting it on every pass of the loop.
Use the [] syntax to append to an array:
$cart[]=...
Also, its good to declare an empty array at the top of the code:
$cart=array();
I have a Mysql table which contains a column of JSON data and a column with an amount.
The goal is to extract the JSON data and the amount and build an array within the foreach loop.
Here is my code:
$sql = "SELECT `Amount`, `NewObject` FROM `mb_cart` WHERE `MyID` = '$id'";
$data_main = $db->query($sql);
Here is my statement that I am using to build the array:
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
However when I run this, I am only returning only the first record set and the amount is blank, but the JSON data is listed. Appreciate any insight anyone cares to share.
You need to add [] to $cart array. WIth each run of foreach you're overwriting the variable $cart.
something like so would work:
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
Or if you wanted the array key to match that of the ID of each row:
Note: You will need to set $id variable somehow above IE: SELECT id, amount also note that you COULD potentially have issues if integer from id is non-unique.. eg(1,1,2,3,4,5,6) it will only show the last id of 1 instead of both (since key's are duplicates).
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[$id] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
Your variable $cart is being overwritten in each loop because you are calling array() each time.
Instead, declare your array outside the loop, and just add values to it as needed:
$cart = array();
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
Try this :
$cart=array();
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
You were treating the cart as a variable instead of array
$cart = array();
foreach ($data_main as $transaction_main) {
$json_decoded = json_decode($transaction_main);
$cart[] = array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}
print_r($cart);
the way to approach this is first create a variable $cart = array();
and then do foreach();
i have written code below please go through it
foreach($data_main as $transaction_main){
$json_decoded = json_decoded($transaction_main);
$cart[]=array('Amount' => $amount, 'CodeType' => $json_decoded->data->Type->data->codeType, 'Name' => $json_decoded->data->Name, 'SiteName' => $json_decoded->data->SiteName);
}