I'm building pizzeria app in CI using cart library.
For some products (pizza, salad...) there is an option to add ingredients.
This is what I have:
Add_to _cart buton
<button class="but_add_cart"
data-qty="1"
data-productid="<?php echo $food_item->item_id; ?>"
data-productname="<?php echo $food_item->item_name; ?>"
data-productprice="<?php echo $food_item->item_price; ?>"
data-adds="<?php echo $food_item->item_price; ?>
With jquery I'm calling ajax
$('.but_add_cart').click(function(){
allVals = []
$('#chk :checked').each(function() {
allVals.push($(this).val());
});
// here I'm getting all ingredients from checkboxes and push to array
var product_id = $(this).data("productid");
var description = $(this).data("description");
var product_name = $(this).data("productname");
var product_price = $(this).data("productprice");
var product_qty = $(this).data("qty");
if(product_id != '' && product_id > 0)
{
$.ajax({
url:"<?php echo base_url(); ?>menu/add",
type:"POST",
async: true,
data:{product_id:product_id, description:description, product_name:product_name, product_qty:product_qty, product_price:product_price, allVals:allVals},
success:function(data)
{
$('#cart_details').html(data);
}
});
}
else
{
alert("Please Enter quantity");
}
});
and add function is:
function add()
{
$data = array(
"id" => $_POST["product_id"],
"qty" => $_POST["product_qty"],
"price" => $_POST["product_price"],
"name" => $_POST["product_name"],
"description" => $_POST["description"],
"allVals" => $_POST["allVals"]
);
print_r($data);
//$this->cart->insert($data);
//echo $this->view();
}
from print_r I have this array
Array
(
[id] => 1
[qty] => 1
[price] => 19.90
[name] => Pepperoni Pizza
[description] => Extra-virgin olive oil, garlic, mozzarella cheese
[allVals] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 6
)
)
Now, in the cart, I want to group Pepperoni pizza with ingredients (2, 3, 4, 6) as one item. If I add one more Pepperoni pizza but with different ingredients, it should be separate item in the cart. I tried to add some random number to rowid but it gives me some error.
Also, every ingredients coast so I have to add these prices to subtotal price for each item in the basket.
I know problem is quite complex but I would like to know if my approach is good or there is a better way to to do this?
Is the answer here not to use the ingredients as the key?
asort($_POST['allVals']);
$key = implode('',$_POST['allVals']);
Combine the Product ID and $key and see if that product exists in the cart. If it does then update the quantity. Your ingredients are your unique key?
Related
so what i have is just some data that should be fixed for printing, and i can't wrap my head around this one...
i have some finished products separated for each batch code (int_lot) with their quantities. You can select and give that values to the invoice, but what i need to do is calculate total of same product name (in relationship called FinishedProduct) and i get all finished products with relation that has quantities with:
DeliveryProductQuantity::where('delivery_note_id', $packingList->delivery_note_id)->with('FinishedProduct')->get();
then i get object like this:
"id" => 12
"finishedid" => "24"
"quantity" => "116"
"old_quantity" => "116"
"delivery_note_id" => "52"
now when i load relationship with first() i get data like for each item in foreach:
"id" => 12
"finished_product_name" => "Product Name"
"shift" => "Prva"
"expiry_date" => "19042023/1"
"user_id" => "6"
What i want to do first is just put all products with same finished_product_name. But finished_product_name is in relation and quantity is in array that's not in relationship.
So i need foreach item of first array of objects to check if relationships product name exists and then if it doesn't add a new entry in temporary array, if does just add quantity to the last one with same product name. What have i tried (besides many else):
$total_data = [];
foreach ($test2 as $product) {
foreach ($product->FinishedProduct as $key => $value) {
if(!in_array($product->FinishedProduct->first()->finished_product_name, $total_data)) {
array_push($total_data,
[
'finished_product_name' => $product->FinishedProduct->first()->finished_product_name,
'expiry_date' => $product->FinishedProduct->first()->expiry_date,
'quantity' => $product->quantity,
'old_quantity' => $product->old_quantity,
'delivery_note_id' => $product->delivery_note_id
]
);
}
if(in_array($product->FinishedProduct->first()->finished_product_name, $total_data)) {
foreach ($total_data as $key => $value) {
if($value['finished_product_name'] == $product->FinishedProduct->first()->finished_product_name) {
$total_data[$key]['quantity'] += $product->quantity;
$total_data[$key]['old_quantity'] += $product->old_quantity;
}
}
}
}
}
I am trying to build a multi-dimensional array from a query return and ran into a problem. Here is a sanitized snippet similar to what I am trying to do. My problem is building the sub array. Any suggestions to get my desired output? I used JSON so it would be more obvious. I can parse an array or JSON.
<?php
// this is my query
global $wpdb;
$locationData = $wpdb->get_results("SELECT
locations.locationID,
locations.location_name,
locations.city,
locations.state,
locations.products,
locations.type,
locations.sku
");
// which returns something like
// [locationID] [location_name] [city] [state] [products] [type] [sku]
// 001 Tulsa Outlet Tulsa OK cakes chocolate 3763
// 001 Tulsa Outlet Tulsa OK cakes lemon 3765
// 001 Tulsa Outlet Tulsa OK sodas coke 4983
// 001 Tulsa Outlet Tulsa OK sodas sprite 4950
// and so on..
// I build an array to be consumed by another function that I created in my plugin:
foreach( $locationData as $key => $location ) {
$output[$row['locationID']] = array(
'locationID' => $row['locationID'],
'location_name' => $row['location_name'],
'products' => array(
// this is where my previous attempts broke down
)
);
$output[$key]['city'] = $location->city;
$output[$key]['state'] = $location->state;
}
// and the $output I need would look like this:
{
"locations" : [
{
"locationID" : "001",
"location_name" : "Tulsa Outlet",
"products" : {
"cakes" : {
"chocolate" : "3763",
"lemon" : "3765"
},
"sodas" : {
"coke" : "4983",
"sprite" : "4950"
}
},
"city" : "Tulsa",
"state" : "ok"
}
]
}
?>
These should help:
$output = [];
foreach ($locationData as $location) {
$locId = $location->locationID;
if (empty($output[$locId])) {
$output[$locId] = [
'locationID' => $location->locationID,
'location_name' => $location->location_name,
'city' => $location->city,
'state' => $location->state,
'products' => [],
];
}
$productType = $location->products;
if (empty($output[$locId]['products'][$productType])) {
$output[$locId]['products'][$productType] = [];
}
$output[$locId]['products'][$productType][$location->type] = $location->sku;
}
Not really sure what you use - $row or $location, so changed everything to $location.
To reindex $output to 0-index array, use array_values.
I have created a tree level menu but I wanted to separate all sub menus by simply adding -- to every sub menu like
main menu
-- 1st level
---- 2nd level
------ 3rd level
and so on
while doing it in LI it is so easy I can simply put tag before running my function but in select option I am unable to achive my target can anyone here would be able to help me out with this please
function fetch_menu($data) {
foreach($data as $menu) {
echo "<option value='".$menu->cid."'>".$menu->cname."</option>";
if(!empty($menu->sub)) {
fetch_sub_menu($menu->sub);
}
}
}
function fetch_sub_menu($sub_menu, $dash = '--'){
foreach($sub_menu as $menu){
echo "<option value='".$menu->cid."'>".$dash.$menu->cname."</option>";
if(!empty($menu->sub)) {
fetch_sub_menu($menu->sub, '--');
}
}
}
The problem is that while applying the above shown code that dash are not increasing for every 2nd or 3rd level menu
Here is how my data is organized in array form
array (
[cid] => 1,
[cname] => 'Main Menu',
[pcid] => 0,
[sub] => array(
[cid] => 2,
[cname] => '1st Level',
[pcid] => 1,
[sub] => array(
[cid] => 3,
[cname] => '2nd Level',
[pcid] => 2,
[sub] => array(
)
)
)
)
You need to add the new dashes to your $dash variable before recursivly calling the function again. That should work
function fetch_sub_menu($sub_menu, $dash = '--'){
foreach($sub_menu as $menu){
echo "<option value='".$menu->cid."'>".$dash.$menu->cname."</option>";
if(!empty($menu->sub)) {
fetch_sub_menu($menu->sub, $dash.'--'); // <-- adding two dashes to $dash
}
}
}
I'm working on an Online Store project using PHP and MySQLi. Most of this project is done and now I'm struggling with total price that a user must pay. This total price is showing in the cart.php page (where users can see all the product items that they have added earlier). So it must be set to the total price of all current items.
So here is the code of cart.php:
if(isset($_GET['cart_id']))
{
$cart_id = $_GET['cart_id'];
$get_add = "SELECT * FROM cart WHERE cart_id = '$cart_id'";
$run_add = mysqli_query($con,$get_add);
$cart_items = [];
while ($row_results = mysqli_fetch_array($run_add)){
$item = array(
'table_id' => $row_results['table_id'],
'cart_id' => $row_results['cart_id'],
'pro_id' => $row_results['product_id'],
'pro_title' => $row_results['product_title'],
'pro_price' => $row_results['product_price'],
'pro_img' => $row_results['product_image'],
'pro_supplier' => $row_results['product_supplier'],
'qty' => $row_results['qty'],
'cart_ip' => $row_results['cart_ip'],
);
$cart_items[] = $item;
}
foreach ($cart_items as $cart) {
echo $cart['pro_price']
}
}
The table cart structure goes like this:
see image here
So now I want to print the sum of all products as total_price, till now I tried several different ways but I could not get the result.
So if you know how to solve this question, please let me know.. I really really appreciate that...
Thanks in advance!
Assuming you need the sum of qty*product_price you could sum this way
$tot = 0;
while ($row_results = mysqli_fetch_array($run_add)){
$cart_items[] = array(
'table_id' => $row_results['table_id'],
'cart_id' => $row_results['cart_id'],
'pro_id' => $row_results['product_id'],
'pro_title' => $row_results['product_title'],
'pro_price' => $row_results['product_price'],
'pro_img' => $row_results['product_image'],
'pro_supplier' => $row_results['product_supplier'],
'qty' => $row_results['qty'],
'cart_ip' => $row_results['cart_ip'],
);
$tot += $row_results['qty']*$row_results['product_price'];
}
foreach ($cart_items as $cart) {
echo $cart['pro_price'];
}
echo $tot;
How to join Product and Product Variants to get every variant with product name. I'm getting all variants but my loop repeating product name each time with product variant.
My code is:
public function viewAllProducts($user_id){
$SelectProducts_Query = mysqli_query($this->conn, "SELECT smpr.product_id, smpr.user_id, smpr.product_name, smpr.product_sizes, smpr.product_photo, smpr.product_created, smpr.product_updated, smpq.quantity_id, smpq.quantity_product_id, smpq.quantity_user_id, smpq.quantity_available, smpq.quantity_price, smpq.quantity_cost, smpq.quantity_size_name FROM ssm_products_quantity as smpq inner join ssm_products as smpr on smpq.quantity_product_id=smpr.product_id WHERE smpr.user_id = $user_id order by smpr.product_id DESC");
if($SelectProducts_Query){
while($view_all_products = mysqli_fetch_array($SelectProducts_Query)){
$view_all_products_details[] = array(
"product_id" => $view_all_products["product_id"],
"product_name" => $view_all_products["product_name"],
"product_photo" => $view_all_products["product_photo"],
"product_sizes" => $view_all_products["product_sizes"],
"product_created_date" => $view_all_products["product_created"],
"product_updated_date" => $view_all_products["product_updated"],
"sizes" => array(
"size_id" => $view_all_products["quantity_id"],
"size_product_id" => $view_all_products["quantity_product_id"],
"size_available_id" => $view_all_products["quantity_available"]
)
);
}
return $view_all_products_details;
}else{
return $view_all_products_details = "false";
}
}
right now I'm getting records like this:
I want to get records like this:
{
"error":false,
"products_detail":[
{
"product_id":"24",
"product_name":"Miror",
"product_photo":"product_images\/1-dummy-17-07-11-10-26-06.jpg",
"product_sizes":"2",
"product_created_date":"2017-07-11 13:26:06",
"product_updated_date":"0000-00-00 00:00:00",
"sizes":{
"size_id":"1",
"size_product_id":"24",
"size_available_id":"50"
},{
"size_id":"2",
"size_product_id":"24",
"size_available_id":"20"
}
},
{
"product_id":"25",
"product_name":"Pipes",
"product_photo":"product_images\/1-pipes-17-07-11-10-22-08.jpg",
"product_sizes":"2",
"product_created_date":"2017-07-11 13:26:06",
"product_updated_date":"0000-00-00 00:00:00",
"sizes":{
"size_id":"3",
"size_product_id":"25",
"size_available_id":"20"
},{
"size_id":"4",
"size_product_id":"25",
"size_available_id":"20"
}
}
]
}
Any solution for me?
use your product id as an array index
try this:
public function viewAllProducts($user_id){
$SelectProducts_Query = mysqli_query($this->conn, "SELECT smpr.product_id, smpr.user_id, smpr.product_name, smpr.product_sizes, smpr.product_photo, smpr.product_created, smpr.product_updated, smpq.quantity_id, smpq.quantity_product_id, smpq.quantity_user_id, smpq.quantity_available, smpq.quantity_price, smpq.quantity_cost, smpq.quantity_size_name FROM ssm_products_quantity as smpq inner join ssm_products as smpr on smpq.quantity_product_id=smpr.product_id WHERE smpr.user_id = $user_id order by smpr.product_id DESC");
if($SelectProducts_Query){
while($view_all_products = mysqli_fetch_array($SelectProducts_Query)){
$pid = $view_all_products["product_id"];
//check the array for product with id is already exists,
//if not then add the data to array[pid] without the sizes
if(!isset($view_all_products_details[$pid]){
$view_all_products_details[$pid] = array(
"product_id" => $pid,
"product_name" => $view_all_products["product_name"],
"product_photo" => $view_all_products["product_photo"],
"product_sizes" => $view_all_products["product_sizes"],
"product_created_date" => $view_all_products["product_created"],
"product_updated_date" => $view_all_products["product_updated"]
);
}
//add the array "sizes" as multidimensional array
//you can set the size_id as index too but i think it is not necessary
$view_all_products_details[$pid]["sizes"][] = array(
"size_id" => $view_all_products["quantity_id"],
"size_product_id" => $view_all_products["quantity_product_id"],
"size_available_id" => $view_all_products["quantity_available"]
);
}
return $view_all_products_details;
}else{
return $view_all_products_details = "false";
}
}