0

What I have in MySQL table

|seller |product |weight |quantity|
|---------------------------------|
|Jacob  |Mobile  | 500g  |   2    |
|John   |Laptop  | 3500g |   1    |
|Kiki   |Charger | 1000g |        |
|Dani   |Keyboard| 1500g |   3    |

If one customer orders x2 items from Jacob seller, x1 item from John, and x3 from Dani, then this customer now ordered 6 items with total 9,000 gram weight from 3 sellers.

2 items from jacob = 500 x 2
1 item from John = 3500 x 1 
3 items from Dani = 1500 x 3

I want to put these values in php variable

$jacob_items_weight = 500 x 2;
$John_items_weight = 3500 x 1;
$Dani_items_weight = 1500 x 3;
$total_weight_customer_have = 9000;

I already tried using for each loop but I can get only the total weight value, but I want all the above variables so I can use them separately.

The main Aim is to calculate $shipping_price

<?php
 if($pro_weight <= 500){
    $shipping_price = 10;
 }else if($pro_weight <= 1000){
    $shipping_price = 15;
 }else if($pro_weight <= 2000){
    $shipping_price = 25;
 }else if($pro_weight <= 3000){
    $shipping_price = 35;
 }else if($pro_weight <= 4000){
    $shipping_price = 45;
 }else if($pro_weight <= 5000){
    $shipping_price = 55;
 }else if($pro_weight <= 6000){
    $shipping_price = 62.5;
 }else if($pro_weight <= 7000){
    $shipping_price = 70;
 }else if($pro_weight <= 8000){
    $shipping_price = 77.5;
 }else if($pro_weight <= 9000){
    $shipping_price = 95;
 }else if($pro_weight <= 10000){
    $shipping_price = 100;
 }else{
   $shipping_price = 'Sorry We can\'t deliver over 10 KGs';
  }
 ?>

Now

`$shipping_price` of `$jacob_items_weight` (500x 2) is 15 USD.

`$shipping_price` of `$John_items_weight` (3500x 1) is 45 USD.

`$shipping_price` of `$Dani_items_weight ` (1500x 3) is 55 USD.

Now finally I want 15 + 45 + 55 = 115 USD.

  $first_seller_shipping_price = 15; 

  $second_seller_shipping_price = 15; 

  $third_seller_shipping_price = 15;

  $total_shipping_price = 155;

If you get my point if I try to calculate for all 9000 g of items the shipping price will be 95 USD. But due difference in the sellers I have to calculate differently then add them to be 115 like the above. so now instead of 95 the shipping will be 115 USD in total.

How can I achieve this goal?

Thank you for your help.

4
  • Where is the quantity in the table? Commented Aug 26, 2021 at 0:06
  • 1
    Use an associative array with each seller name as the keys, not different variables. Commented Aug 26, 2021 at 0:07
  • Do you really store the number with its unit together? 500g? Commented Aug 26, 2021 at 10:57
  • @anan Is there any to put the values in individual variables ? Commented Aug 26, 2021 at 11:09

1 Answer 1

1
$weights = [];
$total_weight = 0;

while ($row = $result->fetch_assoc) {
    $item_weight = $row['quantity'] * $row['weight'];
    $total_weight += $item_weight;
    if (isset($weights[$row['seller']])) {
        $weights[$row['seller']] += $item_weight;
    } else {
        $weights[$row['seller']] = $item_weight;
    }
}
echo "Total weight = $total_weight<br>";
print_r($weights);
Sign up to request clarification or add additional context in comments.

10 Comments

Your answer is what I'm looking for, But there is one issue that I can't resolve, that is print_r($weights); excutes all sellers with their own weight, it is cool, But I want excute them all out in variable. thank you for your help.
It makes no sense to have separate variables, because the seller names could be anything -- how would you refer to the variables later in the code? Just use the associative array.
thank you for your help again but now I tried to update the question to be clear as much as possible. If you get my idea please help me.
What is $pro_weight?
Sorry I just copied from my main code But $pro_weight could be any of the sellers item weight that foreach through all selllers item weight
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.