I want to create a simple shopping cart, but i can't save the data from cart session.
Example:
product_code | qty
Orange | 10
Apple | 5
I want to get orange quantity but it always get the latest one qty(apple)
Here is the value of $_SESSION['products']:
Array
(
[10] => Array
(
[product_qty] => 2
[kode_produk] => 10
[nama_produk] => Rainbow Cake
[harga] => 75000
[gambar] => rainbow.jpg
)
[15] => Array
(
[product_qty] => 3
[kode_produk] => 15
[nama_produk] => Black Forest
[harga] => 100000
[gambar] => black.jpg
)
)
Here is my code:
if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){
foreach($_SESSION["products"] as $product){ //Print each item, quantity and price.
$product_name = $product["nama_produk"];
$product_qty = $product["product_qty"];
$product_price = $product["harga"];
$product_code = $product["kode_produk"];
$_SESSION['kode_produk'] = $product_code;
$jumlah = $_SESSION['product_qty'];
$foto=$product["gambar"];
$asu= $_SESSION["sssdasd"];
$sql1 = ' insert into detail_pemesanan
(kode_produk,jumlah)
values
("'.$product_code.'","'.$product_qty.'");
';
$query = mysqli_multi_query($mysqli_conn,$sql1);
}
}
but it always ended like this
Orang 10
Apple 10
Related
I'm trying to build arrays based off of two queries, one in mysql one on db2. I've built the main arrays, the first dealing with product info and the 2nd dealing with customer info but they have some values in common.
This is working as far as structure but I've been trying to build a final array to contain data from the initial arrays that will build a report.
For now, I'm just going to try and output a report or csv file for each sku_id from the first query, and the total number/ quantity from the 2nd query.
The 2nd query/array contain multiple attributes on a per product/customer basis.
(
[1] => 2
)
Array
(
[1] => 5
)
Array
(
[1] => 14
)
Basically, I pulled three records from the first table for sku_id '1' and matched those three records in db2 for the same sku. Each element corresponds to a customer, so this structure says "customer A had 2 of sku_id 1, customer B had 5 of sku_id 1 and customer C had 14 of sku_id 1'
For the purpose of the report, each line will be for each sku so I want to instead output this as:
array
(
[1] => 21
)
Showing that overall, sku_id 1 had a total of 21. So I still want to keep my initial arrays structured like they are, but I want to modify my final array to just give me line items for the report.
How can I do that?
$skuQuery = "
SELECT
sku_id,
dealer_id,
locations,
s.sku_group_id as groupID,
s.frame as frame,
s.cover1 as cover,
s.color1 as color,
start_date - interval 7 day as start_date
from products p
inner join skus s on p.sku_id = s.id
where curdate() between p.start_date and p.expire_date
group by sku_id, dealer_id
limit 3";
$skuRslt = mysqli_query($conn,$skuQuery);
while($skuRow = mysqli_fetch_assoc($skuRslt)){
$skuResult['sku_id'] = $skuRow;
$dealerQuery = "
SELECT
cstnoc,
sum(orqtyc) as TotalQTY
from table
where cstnoc = {$skuRow['dealer_id']}
AND framec = {$skuRow['frame']}
AND colr1c = {$skuRow['color']}
AND covr1c = {$skuRow['cover']}
AND extd2d >= " . str_replace('-', '', $skuRow['start_date']) . "
group by cstnoc, framec
";
$dealerRslt = odbc_exec($DB2Conn, $dealerQuery);
$dealerResult = [];
foreach($skuResult as $skuRow){
while($dealerRow = odbc_fetch_array($dealerRslt)){
if ( !isset($dealerResult[$dealerRow['CSTNOC']]) ) {
$dealerResult[$dealerRow['CSTNOC']] = 0;
}
$dealerResult[$dealerRow['CSTNOC']] += $dealerRow['TOTALQTY'];
}
}
$skuTots = array_fill_keys(array_unique(array_column($skuResult, 'sku_id')), 0);
foreach ($skuResult as $rec) {
$skuTots[$rec['sku_id']] += $dealerResult[$rec['dealer_id']];
}
print_r($skuTots);
}
UPDATE:
This is the print out for my first two arrays:
Array
(
[0] => Array
(
[sku_id] => 1
[dealer_id] => 1976
[locations] => 1
[groupID] => 1
[frame] => 1051
[cover] => 1150
[color] => 99
[start_date] => 2018-03-
)
[1] => Array
(
[sku_id] => 1
[dealer_id] => 5400
[locations] => 1
[groupID] => 1
[frame] => 1051
[cover] => 1150
[color] => 99
[start_date] => 2017-04-
)
[2] => Array
(
[sku_id] => 1
[dealer_id] => 11316
[locations] => 1
[groupID] => 1
[frame] => 1051
[cover] => 1150
[color] => 99
[start_date] => 2017-02-
)
)
Array
(
[0] => Array
(
[CSTNOC] => 1976
[TOTALQTY] => 2
)
[1] => Array
(
[CSTNOC] => 5400
[TOTALQTY] => 5
)
[2] => Array
(
[CSTNOC] => 11316
[TOTALQTY] => 14
)
)
And here is an example of my end result I'm trying to achieve:
In the below example, sku_id, groupID and location would come directly from the first query. Days is the number of days between start_date (from query 1) and curDate(). Qty is orqtyc from query 2 and Average is a calculation based on locations, days and qty. Each row is based on a different customer, so even though the below example is for one sku, the rows signify a different customer with different locations, qty, etc.
SKU_id | groupID | locations (n) | Days (x) | Qty(q) | Average (x/(q/n))
--------------------------------------------------------------------------------
123 1 3 120 15 24
123 1 2 12 6 4
The Report or CSV would then only show one line item for the above:
SKU | Group | Average Days | Total units sold
123 | 1 | 28 | 21
Basically, for every sku I'm getting those records and then I need to do those calculations by row, and get a total of those values for each sku. The report will only have one line item per sku.
Im trying to figure something out. I want to apply coupon/discount amount to multiple products evenly.
For example if I have an array with 3 items
Array
(
[0] => stdClass Object
(
[id] => 1
[price] => 10.00
)
[1] => stdClass Object
(
[id] => 2
[price] => 20.00
)
[2] => stdClass Object
(
[id] => 3
[price] => 30.00
)
)
my coupon vale is 10 $ off
and I have tried this:
foreach ($array as $row) {
$data['price'] = ($row->price - 10 / 3) * 100
}
So basically I want to deduct from each item price proportionally total of 10 and end up with something like:
Array
(
[0] => stdClass Object
(
[id] => 1
[price] => 7.50
)
[1] => stdClass Object
(
[id] => 2
[price] => 17.50
)
[2] => stdClass Object
(
[id] => 3
[price] => 25.00
)
)
where total price sum is 50 instead original of 60
So, from the comments under the question, you want to distribute the coupon value proportionally (not evenly) to prices of all items. That's pretty easy, it's more a mathematical problem than a programming one.
Calculate x as coupon / sum(prices)
Iterate through all items and subtract x * item_price from item_price
That's it.
For your example (coupon = 10, prices = [10, 20, 30]), the result is:
x = 10 / 60 = 0.1666667
Subtract x * item_price from each item's item_price:
10 - 10 * 0.1666667 = 10 - 1.6666667 = 8.33333
20 - 20 * 0.1666667 = 20 - 3.3333333 = 16.6666
30 - 30 * 0.1666667 = 30 - 5 = 25
To verify: 1.666667 + 3.3333333 + 5 = 10
I have a query that returns a list of rows from mysql database, the list of rows generated have of rows values being the same.
Table
id mAmount paidAmount
1 100 50
1 100 30
2 200 20
2 200 150
I actually want to sum the paidAmount and subtract from just one of mAmount value per the same id.
For instance
1 | (100) - (50 + 0) = 50 |
1 | (100) - (30 + 50) = 20 |
2 | (200) - (20 + 0) = 180 |
2 | (200) - (150 + 20) = 30 |
I do not want anyone any to give me the entire codes to this, but an idea as to how to go about it using either php with while loop or possibly foreach statements. Thanks really appreciate.
Just have to loop and accumulate the paid amount, then do the subtraction.
For this example I suppose you pre-order your records by id and pass the records to an multidimensional array called rows:
$currentId = $rows[0]['id'];
$accumulated = 0;
foreach ( $rows as $row ) {
if ( $row['id'] != $currentId ) {
$accumulated = 0;
$currentId = $row['id'];
} // end if not current id
$accumulated += $row['paidAmount'];
$balance = $row['mAmount'] - $accumulated;
$row['balance'] = $balance;
$result[] = $row;
} // end foreach
print_r ( $result );
Results are printed like this:
Array
(
[0] => Array
(
[id] => 1
[mAmount] => 100
[paidAmount] => 50
[balance] => 50
)
[1] => Array
(
[id] => 1
[mAmount] => 100
[paidAmount] => 30
[balance] => 20
)
[2] => Array
(
[id] => 2
[mAmount] => 200
[paidAmount] => 20
[balance] => 180
)
[3] => Array
(
[id] => 2
[mAmount] => 200
[paidAmount] => 150
[balance] => 30
)
)
I have a two table 1. grocery_product and 2. grocery_attribute where i can get all data from grocery_product my array look like as below.
Array
(
[0] => Array
(
[id] => 1
[pname] => Royal Moong Dal/Moong Dal
[categoryid] => 4
[subcatid] => 3
[added_date] => 2016-08-16
)
[1] => Array
(
[id] => 2
[pname] => Royal Toor Dal/Arhar Dal/Tuver Dal
[categoryid] => 4
[subcatid] => 3
[added_date] => 2016-08-16
)
)
What i want to do that in my grocerry_attribute table i am passing grocery_product_id and fetch all data from grocery_attribute where product is only one and attributes are multiple.
My grocery atrtribute table look like .
id 1
product_id 2
price 1500
discount 15
product_qty 20
id 2
product_id 2
price 1000
discount 10
product_qty 50
i want to fetch one grocery_product(id=1) and its attribute data.
Query ??
You can use LEFT JOIN like,
SELECT p.id,p.pname,g.price,g.discount,g.product_qty FROM
grocery_product as p
LEFT JOIN grocery_attribute as g
ON p.id = g.product_id
WHERE p.id=2
Now you can loop for attributes and show different price, discount, and product_qty for a single product.
// Let $products is multidimensional array
if(!empty($products) && isset($products[0]['pname'])){
echo '<h1>'.$products[0]['pname'].'</h1>';
foreach($products as $product){
echo 'Price:'.$product['price']."\n";
echo 'Discount:'.$product['discount']."\n";
echo 'Product Quantity:'.$product['product_qty']."\n";
}
}
First I added a session then I print my session it looks completely ok here it is
Array
(
[14] => Array//(main key is my restaurant Id)
(
[retaurantDetail] => Array
(
[restId] => 14
[restaurantName] => Barca
[published] => 1
[timings] => 10 to 10
[normalCost] => 150
[logo] => 44f7afcffb0aeea5c69ccee9041cab84.jpg
[email] => barca#barca.com
[phone] => 741258
)
[menuArray] => Array
(
[70] => Array // (menu ID is the Key)
(
[menuId] => 70
[productId] => 35
[productName] => Coca Cola
[categoryTitle] => Beverages
[categoryId] => 52
[price] => 100
[attributeName] => 1.5L
[isDefault] => 1
[qty] => 1
)
)
)
)
Now I add some logic if some one add again that menu in add to cart in short he/she adds plus 1 qty to that menu here it is my code,(code is not completed yet but now I am focused on just to update qty)
foreach($session->cartSession as $sessionKey=>$sessionVal)
{
foreach($sessionVal['menuArray'] as $sessionMenuKey=>$sessionMenuVal)
{
if($sessionMenuKey == $post_data['menuId'])
{
echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>";
//$qty = $session->cartSession[$restaurantDetail['restId']]['menuArray'][$sessionMenuKey];
$sessionMenuVal['qty'] = $sessionMenuVal['qty']+1;
echo"<pre>"; print_r($sessionMenuVal['qty']); echo "</pre>";
}
}
}
What I am missing I want to updatre session qty.
every time I press add it shows 1 qty then I add plus 1 qty to it then it show 2 after then it again shows 1 qty :(.
You're never writing back into the Session...
If i understand your code correctly, the following should work. Check the correct Array-Nesting of my code in case it doesn't work out of the box.
// Somewhere on top, use this for your loops
$cartSession = $session->offsetGet('cartSession');
// Inside your matched loop
$currentQty = $sessionMenuVal['qty'];
$cartSession[$sessionKey][$sessionMenuKey]['qty'] = ++$currentQty;
// After your loops at the end
$session->offsetSet('cartSession', $cartSession);