Only change the quantity when product already exists in database - php

I have a problem with some kind of 'webshop' I'm building
I add some products in the database but I want to only change the quantity when the product already exists.
I only don't know how to achieve this. Can somebody maybe help me with a function to check if the product already exists so I can add a +1 to ['aantal'] everytime I add a product that already exists?
the post is:
if(isset($_POST['submit-bonbon'])){
$postGewicht = $_POST['gewicht'];
$bonbonName = $_POST['product'];
$image = $_POST['afbeelding'];
$quantity = $_POST['aantal'];
$jsonData = unserialize($_SESSION['json_data']);
if(count($jsonData,1) >= 1){
$jsonData['bonbons'][] = array('gewicht' => $postGewicht,'name' => $bonbonName,'afbeelding' => $image, 'aantal' => $quantity);
} else {
$bonbonsArray[] = array('gewicht' => $postGewicht,'name' => $bonbonName,'afbeelding' => $image, 'aantal' => $quantity);
$order = array('userId' => $bonbonUser, 'bonbons' => $bonbonsArray);
$jsonData = $order;
}
$_SESSION['json_data'] = serialize($jsonData);
$orderData = $_SESSION['json_data'];
$query = mysql_query("SELECT * FROM CMS_bonbonOrder WHERE userId='$bonbonUser'");
mysql_query("UPDATE CMS_bonbonOrder SET jsonData='$orderData' WHERE userId='$bonbonUser';");
}
So I put a Json string in the database that looks like this for example:
Array
(
[bonbons] => Array
(
[0] => Array
(
[gewicht] => 16
[name] => test
[afbeelding] => amarena%2020g1.jpg
[aantal] => 1
)
[1] => Array
(
[gewicht] => 16
[name] => test
[afbeelding] => amarena%2020g1.jpg
[aantal] => 1
)
[2] => Array
(
[gewicht] => 16
[name] => test
[afbeelding] => amarena%2020g1.jpg
[aantal] => 1
)
[3] => Array
(
[gewicht] => 18
[name] => Testbonbon 2
[afbeelding] => bresil%2017g1.jpg
[aantal] => 1
)
[4] => Array
(
[gewicht] => 16
[name] => test
[afbeelding] => amarena%2020g1.jpg
[aantal] => 1
)
)
)
I really hope I made myself clear enough!
Thanks

Related

fetch result from database by array codeigniter

I have a table of food where I store category ids as coma separated like in the below image
food table
I made a method in model which takes array (category ids) as argument and fetches the food items which have ids matching to the array from argument.
I made the below query
if(count($categoryIds) > 0 && count($allergyIds) == 0 ){
$tempArr = array();
foreach($categoryIds as $eachCategoryId){
$sql = "Select food.food_id,food_name,food_image,food_thumbnail,description,food_variations.price as price,is_active
from food
join food_variations on food_variations.food_id = food.food_id
where FIND_IN_SET($eachCategoryId,category_id)
and food.restaurant_id = $restaurantId
and food.is_active = 1
and food.is_deleted = 0
and food.food_status = 3
and food_variations.food_variation_id = food.default_food_variation_id";
$result = $this->db->query($sql)->result_array();
array_push($tempArr, $result);
}
echo "<pre>";print_r($tempArr);
}
Below is the result of above query
Array
(
[0] => Array
(
[0] => Array
(
[food_id] => 10
[food_name] => Rama Mckee
[food_image] =>
[food_thumbnail] =>
[description] => asdfs
[price] => 34
[is_active] => 1
)
[1] => Array
(
[food_id] => 6
[food_name] => Rishi
[food_image] =>
[food_thumbnail] =>
[description] => test
[price] => 120
[is_active] => 1
)
[2] => Array
(
[food_id] => 5
[food_name] => test
[food_image] => http://localhost/gastroapp/assets/uploads/food_images/a5918726b920e7cbfc7f90e1afc48091.jpg
[food_thumbnail] => http://localhost/gastroapp/assets/uploads/food_images/thumb/a5918726b920e7cbfc7f90e1afc48091.jpg
[description] => test
[price] => 120
[is_active] => 1
)
)
[1] => Array
(
[0] => Array
(
[food_id] => 10
[food_name] => Rama Mckee
[food_image] =>
[food_thumbnail] =>
[description] => asdfs
[price] => 34
[is_active] => 1
)
[1] => Array
(
[food_id] => 7
[food_name] => ezhva
[food_image] =>
[food_thumbnail] =>
[description] => ddsfsd
[price] => 20
[is_active] => 1
)
[2] => Array
(
[food_id] => 8
[food_name] => test
[food_image] =>
[food_thumbnail] =>
[description] => test
[price] => 22
[is_active] => 1
)
[3] => Array
(
[food_id] => 6
[food_name] => Rishi
[food_image] =>
[food_thumbnail] =>
[description] => test
[price] => 120
[is_active] => 1
)
)
)
I am getting duplicate results and I think this might also cause performance issues.
The below was the query when I had only one category per food which was giving me desired result.
return $this->db->select('food.food_id,food_name,food_image,food_thumbnail,description,food_variations.price as price,is_active')
->from('food')
->join('food_variations', 'food_variations.food_id = food.food_id')
->where_in('category_id',$categoryIds)
->where('food.restaurant_id', $restaurantId)
->where('food.is_active', '1')
->where('food.is_deleted', '0')
->where('food.food_status','3')
->where('food_variations.food_variation_id IN( select food_variation_id from food_variations where food_variation_id = food.default_food_variation_id )')
->get()
->result_array();
Please help.
First You need to insert your list in an array
$list = {ids column}
$list = array_map("intval", explode(",", str_replace(',' , '', $list)));
Now your list is saved in an array $list
Now you can call it
foreach($list as $value) {
$sql = "SELECT * FROM menu WHERE user_id = '$user_id' AND id = '$value' AND deleted = '0' AND active = '1';";
$result = mysqli_query($dBconnection, $sql);
$check = mysqli_num_rows($result);
if ($check>0) {
while($row = mysqli_fetch_assoc($result)) {
cho $row['id'];
}
} else {
echo 'empty list';
}
}
For mysql, string column is recommended for saving static data instead of adding just IDs. Instead of just integer, you will have to store the object with relevant values. If you want it's relational data, its better to use an intermediate table.
like U need an extra table category_products table to store category_id and product_id since You have many-to-Many relationships(a product have many category and a category have many products)

NetSuite: SuiteTalk: PHP: Receive item from purchase order

I am trying to create ItemReceipt which is created from PurchaseOrder. I am adding my inventory items manually in purchase order during its creation. When I am receiving this order NetSuite web service is throwing following error:
Adding new line to sublist item is not allowed.
while i have tried replaceAll attribute for ItemReceiptItemList as well.
Here is my code:
$tranDate = new \DateTime();
$tranDate = $tranDate->format('Y-m-d\TH:i:s');
$nsInventoryDetail = new InventoryDetail();
$nsInventoryAssignmentList = new InventoryAssignmentList();
$nsInventoryAssignmentList->replaceAll = true;
$nsInventoryAssignment[0] = new InventoryAssignment();
$nsInventoryAssignment[0]->receiptInventoryNumber = 3310; //NS inventory item id
$nsInventoryAssignment[0]->quantity = 1;
$nsInventoryAssignmentList->inventoryAssignment = $nsInventoryAssignment;
$nsInventoryDetail->inventoryAssignmentList = $nsInventoryAssignmentList;
$nsItemReceiptItem = new ItemReceiptItem();
$nsItemReceiptItem->item = new RecordRef();
$nsItemReceiptItem->item->internalId = 3310;
$nsItemReceiptItem->location = new RecordRef();
$nsItemReceiptItem->location->internalId = 1; // NS inventory location id
$nsItemReceiptItem->itemReceive = true;
$nsItemReceiptItem->rate = $purchasePrice->getAmount();
$nsItemReceiptItem->currency = $purchasePrice->getCurrency();
$nsItemReceiptItem->inventoryDetail = $nsInventoryDetail;
$itemList = new ItemReceiptItemList();
$itemList->item = [$nsItemReceiptItem];
$itemList->replaceAll = true;
$nsItemReceipt = new ItemReceipt();
$nsItemReceipt->createdFrom = new RecordRef();
$nsItemReceipt->createdFrom->internalId = 1223; // NS purchase order id
$nsItemReceipt->tranDate = $tranDate;
$nsItemReceipt->itemList = $itemList;
print_r($nsItemReceipt);
$nsItemReceipt = $this->itemReceiptService->create($nsItemReceipt);
Throws the following: Adding new line to sublist item is not allowed.
Following is my ItemReceipt object being sent:
NetSuite\Sdk\ItemReceipt Object
(
[createdDate] =>
[lastModifiedDate] =>
[customForm] =>
[exchangeRate] =>
[entity] =>
[currencyName] =>
[subsidiary] =>
[createdFrom] => NetSuite\Sdk\RecordRef Object
(
[internalId] => 1223
[externalId] =>
[type] => purchaseOrder
[name] =>
)
[tranDate] => 2020-01-02T13:45:36
[partner] =>
[postingPeriod] =>
[tranId] =>
[inboundShipment] =>
[memo] =>
[itemFulfillment] =>
[currency] =>
[landedCostMethod] =>
[landedCostPerLine] =>
[itemList] => NetSuite\Sdk\ItemReceiptItemList Object
(
[item] => Array
(
[0] => NetSuite\Sdk\ItemReceiptItem Object
(
[itemReceive] => 1
[jobName] =>
[item] => NetSuite\Sdk\RecordRef Object
(
[internalId] => 3310
[externalId] =>
[type] => inventoryItem
[name] =>
)
[orderLine] =>
[line] =>
[itemName] =>
[description] =>
[location] => NetSuite\Sdk\RecordRef Object
(
[internalId] => 3
[externalId] =>
[type] => location
[name] =>
)
[onHand] =>
[quantityRemaining] =>
[quantity] =>
[unitsDisplay] =>
[unitCostOverride] =>
[inventoryDetail] => NetSuite\Sdk\InventoryDetail Object
(
[inventoryAssignmentList] => NetSuite\Sdk\InventoryAssignmentList Object
(
[inventoryAssignment] => Array
(
[0] => NetSuite\Sdk\InventoryAssignment Object
(
[internalId] =>
[issueInventoryNumber] =>
[receiptInventoryNumber] =>
[binNumber] =>
[toBinNumber] =>
[quantity] => 1
[expirationDate] =>
[quantityAvailable] =>
)
)
[replaceAll] => 1
)
[customForm] =>
[nullFieldList] =>
)
[serialNumbers] =>
[binNumbers] =>
[expirationDate] =>
[rate] => 7900
[currency] => AED
[restock] =>
[billVarianceStatus] =>
[isDropShipment] =>
[options] =>
[landedCost] =>
[customFieldList] =>
)
)
[replaceAll] => 1
)
[expenseList] =>
[landedCostsList] =>
[accountingBookDetailList] =>
[customFieldList] =>
[internalId] =>
[externalId] =>
[nullFieldList] =>
)
Any help will be much appreciated.
I found this working example here
https://www.reddit.com/r/Netsuite/comments/ewlen8/suitetalk_issue_creating_itemreceipt/
-soap:Body
-
-
<q1:createdFrom internalId="35023" type="purchaseOrder"/>
-<q1:itemList>
-<q1:item>
<q1:itemReceive>true</q1:itemReceive>
<q1:item internalId="1528"/>
<q1:orderLine>1</q1:orderLine>
<q1:quantity>100</q1:quantity>
</q1:item>
</q1:itemList>
</soap:Body>
All I had to do was change $item->line to $item->orderLine and that fixed the issue - looks like you need to reference the original purchase order line number

Arrange values in array php / mysql

I am exporting data from mysqli to google firebase DB. In firebase table one of the column named siteGeoCode (latitude and longitude) having values like [17.426083,78.439241] in array format. But in mysqli table two separate columns are there for latitude and longitude. I am trying to fetch records like below and converting those to array format.
$emp_array = array();
$sel_query = "select companyId,countryCode,createdBy,createdDat,`latitude`,`longitude`,siteName,`status` from customers limit 0,3";
$res = mysqli_query($mysqliConn,$sel_query);
while($sel_row = mysqli_fetch_assoc($res))
{
$emp_array[] = $sel_row;
$lat_array['siteGeoCode'][0] = $sel_row['latitude'];
$lat_array['siteGeoCode'][1] = $sel_row['longitude'];
array_push($emp_array,$lat_array);
}
// output
[0] => Array
(
[companyId] => iArwfGLC1lE6x3lO24cb
[countryCode] => 91
[createdBy] => X54P6gVJ7dA4Fi2fjEmc
[createdDate] => 2019-08-20 00:58:08
[latitude] => 15.6070347
[longitude] => 79.6146273
[siteName] => Ongole
[status] => 1
)
[1] => Array
(
[siteGeoCode] => Array
(
[0] => 15.6070347
[1] => 79.6146273
)
)
[2] => Array
(
[companyId] => YPbhSLWQfAR6ThhszSAf
[countryCode] => 91
[createdBy] => iArwfGLC1lE6x3lO24cb
[createdDate] => 2019-09-10 22:37:08
[latitude] =>
[longitude] =>
[siteName] => Madhap
[status] =>
)
[3] => Array
(
[siteGeoCode] => Array
(
[0] =>
[1] =>
)
)
but my desired output should be like below
[0] => Array
(
[companyId] => iArwfGLC1lE6x3lO24cb
[countryCode] => 91
[createdBy] => X54P6gVJ7dA4Fi2fjEmc
[createdDate] => 2019-08-20 00:58:08
[siteGeoPoint] => Array
(
[0] => 15.6070347
[1] => 79.6146273
)
[siteName] => Ongole
[status] => 1
)
[1] => Array
(
[companyId] => YPbhSLWQfAR6ThhszSAf
[countryCode] => 91
[createdBy] => iArwfGLC1lE6x3lO24cb
[createdDate] => 2019-09-10 22:37:08
[siteGeoPoint] => Array
(
[0] =>
[1] =>
)
[siteName] => Madhap
[status] =>
)
How can I achieve this ? Any help would be greatly appreciated. Thanks in advance
If you are wanting to place the result of the latitude/longitude columns into the result grouped, do the manipulations to the row, then add it to your result set, abit like below.
$result = [];
while ($row = mysqli_fetch_assoc($res)) {
// group geo
$row['siteGeoCode'] = [
$row['latitude'],
$row['longitude']
];
// unset uneeded
unset($row['latitude'], $row['longitude']);
// place in result
$result[] = $row;
}
You can use following code.
$output_array = array();
while($sel_row = mysqli_fetch_assoc($res)) {
$output_array[] = array(
'companyId' => $sel_row['companyId'],
'countryCode' => $sel_row['countryCode'],
'createdBy' => $sel_row['createdBy'],
'createdDate' => $sel_row['createdDate'],
'siteGeoPoint' => array($sel_row['latitude'], $sel_row['longitude']),
'siteName' => $sel_row['siteName'],
'status' => $sel_row['status'],
);
}
print_r($output_array);
you can see the difference you've used the array and I did. From you output I guess you've print different arrays.
Hope the code get you desire result!

Cannot get item from array

hi im new to api calling and i seem to have a problem with getting an item called price from my array. The following is the array that I am supposed to extract price from.
Array
(
[prodId] => ROC-PRD-2
[prodName] => iphone 6
[projectId] => 8
[categoryIds] => Array
(
[0] => ROC-CAT-1
)
[prodParentSku] => iph6a1
[prodMetaTitle] => iphone 6
[visible] => 1
[prodStatus] => 1
[modifiedDate] => 1443472415
[createDate] => 1443472193
[productImages] => Array
(
[0] => Array
(
[id] => 89
[imageName] => iphone-ipad hi res.png
[imagePath] => http://tos-staging-web-server-s3.s3.amazonaws.com/8/products/ROC-PRD-2/iphone_ipad_hi_res.png
[visible] => 1
[featured] =>
[modifiedDate] => 1443472390
[createDate] => 1443472390
)
[1] => Array
(
[id] => 90
[imageName] => ipad 2.jpg
[imagePath] => http://tos-staging-web-server-s3.s3.amazonaws.com/8/products/ROC-PRD-2/ipad_2.jpg
[visible] => 1
[featured] =>
[modifiedDate] => 1443472397
[createDate] => 1443472397
)
)
[pricing] => Array
(
[price] => 1000
[memberGroupPrices] => Array
(
)
)
)
I am able to get the product images information such as id, imagepath, using the following for loop
foreach ( $product['productImages'] as $key => $data){
foreach ($data as $key => $eachImage){
}
}
However for price my code is as follow:
foreach ( $product['pricing'] as $key => $price){
}
If i echo the $price i would get "1000Array"
If i echo $price['price'], nothing comes out.
You Dont need to look for $pricing as its its single;
$price = $product["pricing"]["price"];
$memberGroupPrices = $product["pricing"]["memberGroupPrices"];
Just print the price with out looping
$price = $array["pricing"]["price"];
Please check with this
foreach ( $product['pricing'] as $key => $price){
if($key=='price')
$price_val=$price;
}

How to group elements of array using Primary and secondary key?

I've got this array:
Array
(
[0] => Array
(
[id]=>1
[account_id] => 1
[object_id] => 43
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
[1] => Array
(
[id] => 1
[account_id] => 1
[object_id] => 42
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
[2] => Array
(
[id] => 1
[account_id] => 1
[object_id] => 41
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
[3] => Array
(
[id] => 2
[account_id] => 2
[object_id] => 1
[object_type] => USER
[action_type] => FOLLOW_USER
)
[4] => Array
(
[id] => 1
[account_id] => 1
[object_id] => 2
[object_type] => USER
[action_type] => FOLLOW_USER
)
[5] => Array
(
[id] => 1
[account_id] => 1
[object_id] => 1
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
)
Now I want to group elements have same value (for example UPLOAD_PHOTO) by id as Primary Key and action_type as Secondary Key, like:
Array
(
[0] => Array(
[id] => 1
[account_id] => 1
[actions] => array(
[1] => array(
[object_id] => 42
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
[2] => array(
[object_id] => 43
[object_type] => PHOTO
[action_type] => UPLOAD_PHOTO
)
)
)
[2] => Array
(
[id] => 1
[account_id] => 1
[actions] = array(
[0] => Array
(
[object_id] => 3
[object_type] => USER
[action_type] => FOLLOW_USER
)
[1] => Array
(
[object_id] => 4
[object_type] => USER
[action_type] => FOLLOW_USER
)
)
)
)
I tried some solutions but didn't succeed.
When constructing your output array, you'll want to use meaningful array keys. That way you can find the out element that the in element needs to be added to:
$in = (...your array...);
$out = array();
foreach($in as $element) {
$id = $element['id'];
//If that id hasn't been seen yet, create the out element.
if (!isset($out[$id])) {
$out[$id] = array(
'id'=>$element['id'],
'account_id' => $element['account_id'],
'actions' => array()
);
}
//Add that action to the out element
$out[$id]['actions'][] = array(
'object_id' => $element['object_id'],
'object_type' => $element['object_type'],
'action_type' => $element['action_type']
);
}
I'm not sure why your input elements have different fields... If this is a desired feature, where one set of possible fields belongs to the id group and another set belongs to the action, you can do this instead (slightly less readable, but more flexible):
$in = (...your array...);
$out = array();
//These define which fields from an
//input element belong to the id,
//and which to the action.
$id_fields = array_flip(array('id','account_id','object_user_id');
$action_fields = array_flip(array('object_id','object_type','action_type');
foreach($in as $element) {
$id = $element['id'];
//If that id hasn't been seen yet, create the out element.
if (!isset($out[$id])) {
$out[$id] = array_intersect_key($element, $id_fields);
$out[$id]['actions'] = array();
}
//Add that action to the out element
$out[$id]['actions'][] = array_intersect_key($element, $action_fields);
}
This is a better solution if the input elements can be different, because if an expected field (other than 'id') is missing, the script will cope with it easily.

Categories