Jump to content

Search the Community

Showing results for tags 'calculate'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (PhpStorm, VS Code, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Age


Donation Link

Found 4 results

  1. Hi, I have a json data, below which contains an item with tax: "date": "2015-05-05 12:41", "shop": "Toto", "products": [ { "price": "2.00", "quantity": "1", "description": "chocolat", "tax": [ { "price": "1.00", "quantity": "1", "description": "tax1" }, { "price": "2.00", "quantity": "1", "description": "tax2" } ] } ] } I am able to calculate the price ($amountAll) with the following code: $datajs = "myJsonSimple2.json"; $datajs = file_get_contents($datajs); $decodageDatajs = json_decode($datajs); $dateBiling = $decodageDatajs->date; $shopName = $decodageDatajs->shop; foreach ($decodageDatajs->products as $obj) { $ItemPrice = $obj->price; $ItemQuantity = $obj->quantity; $ItemDescription = $obj->description; $taxes = (array)$obj->tax; $taxesPrice1 = !empty($taxes[0]) ? $taxes[0]->price : 0.00; $taxesPrice2 = !empty($taxes[1]) ? $taxes[1]->price : 0.00; $taxesQuantity1 = !empty($taxes[0]) ? $taxes[0]->quantity : 0; $taxesQuantity2 = !empty($taxes[1]) ? $taxes[1]->quantity : 0; $taxesDescription1 = !empty($taxes[0]) ? $taxes[0]->description : '1er tax null'; $taxesDescription2 = !empty($taxes[1]) ? $taxes[1]->description : '2e tax null'; $amountAll = $ItemPrice * $ItemQuantity + $taxesPrice1 + $taxesPrice2; } But how I calculate if there are several items (2 or 3 or 5...) { "date": "2015-05-05 12:41", "shop": "Toto", "products": [ { "price": "2.00", "quantity": "1", "description": "chocolat", "tax": [ { "price": "1.00", "quantity": "1", "description": "tax1" }, { "price": "2.00", "quantity": "1", "description": "tax2" } ] }, { "price": "3.00", "quantity": "1", "description": "bonbon", "tax": [ { "price": "2.00", "quantity": "1", "description": "tax1" }, { "price": "3.00", "quantity": "1", "description": "tax2" } ] }, { "price": "2.00", "quantity": "1", "description": "gateaux", "tax": [ { "price": "1.00", "quantity": "1", "description": "tax1" }, { "price": "2.00", "quantity": "1", "description": "tax2" } ] } ] } I need like as : $amountAll = $ItemPrice1 * $ItemQuantity1 + $ItemPrice2 * $ItemQuantity2 + $ItemPrice3 * $ItemQuantity3 + $taxesPrice1a + $taxesPrice2a + $taxesPrice1b + $taxesPrice2b + $taxesPrice1c + $taxesPrice2c; but this is wrong : I must counter and use while statement but I don,t know how I can write ? $calculeItems = count($decodageDatajs->products); $i = 0; while ($i < $calculeItems) { foreach ($decodageDatajs->products as $obj) { $ItemPrice = $obj->price; $ItemQuantity = $obj->quantity; $ItemDescription = $obj->description; $taxes = (array)$obj->tax; $taxesPrice1 = !empty($taxes[0]) ? $taxes[0]->price : 0.00; $taxesPrice2 = !empty($taxes[1]) ? $taxes[1]->price : 0.00; $taxesQuantity1 = !empty($taxes[0]) ? $taxes[0]->quantity : 0; $taxesQuantity2 = !empty($taxes[1]) ? $taxes[1]->quantity : 0; $taxesDescription1 = !empty($taxes[0]) ? $taxes[0]->description : '1er tax null'; $taxesDescription2 = !empty($taxes[1]) ? $taxes[1]->description : '2e tax null'; $amountAll = $ItemPrice * $ItemQuantity + $taxesPrice1 + $taxesPrice2; /* this si wrong !!!!! $amountAll = $ItemPrice1 * $ItemQuantity1 + $ItemPrice2 * $ItemQuantity2 + $ItemPrice3 * $ItemQuantity3 + $taxesPrice1a + $taxesPrice2a + $taxesPrice1b + $taxesPrice2b + $taxesPrice1c + $taxesPrice2c; */ } $i++; } can you help me please Thanks
  2. Hey guys, I need some help with a simple script a friend wrote for me but isn't returning the correct values. I'm looking to get this script working but my PHP skills are abysmal and I can't for the life of me figure out whats the best way to do this. I've tried contacting my friend but he's moving cross-country so I'm hoping you guys can give me some advice. The script is supposed to return a total shipping price based on the weight of products that are passed to it via an API and the location of the visitor. There are a total of 3 different products (Product A, Product B, Product C) each with it's own weight in grams and it's own shipping rate. The script is needs to follow the following rules: When 1 product (either Product A, B, or C) is in the cart it returns the full fixed shipping price for either US or International Shipping based on $isInternational When 2 or more of Product A or B are added to the cart the full shipping is taken for the heaviest item and a fixed price is added for each additional item (Product and B each have their own fixed prices) When Product C is added to the cart it returns a fixed based on US or International (Product C ships separately and doesn't depend on the weight/prices of Product A or B) The product weights/prices are: Product A: Weight (grams): 250 Us Shipping for first instance: $6.00 International Shipping for first instance: $10.00 Each additional instance: $1.00 Product B: Weight (grams): 300 Us Shipping for first instance: $8.00 International Shipping for first instance: $11.00 Each additional instance: $2.50 Product C: Weight (grams): 120 Us Shipping for each instance: $8.00 International Shipping for each instance: $20.00 An example would be if the cart contained 2 of Product B, 1 of Product A, and 2 of Product C shipping to the US: The price would be 8+2.50+1+8+8=27.50 The script currently ignores the addition of more instances of the items and doesn't take into account US vs International: <? header('Content-Type: application/json'); $json = file_get_contents('php://input'); $request_body = json_decode($json, true); if (is_null($request_body) or !isset($request_body['eventName'])) { header('HTTP/1.1 400 Bad Request'); $response = array( "rates" => "" ); die(json_encode($response)); } switch ($request_body['eventName']) { case 'shippingrates.fetch': // ============= recieve data from cart ============== $country_code = $request_body["content"]["billingAddressCountry"]; $state_code = $request_body["content"]["billingAddressProvince"]; $isInternational = ($country_code != "US"); $quantity = 0; $maxWeight = 0; $cost = 0; $description = "International Shipping"; if($request_body["content"] && $request_body["content"]["items"]){ foreach ($request_body["content"]["items"] as $cart_item){ $quantity += $cart_item["quantity"]; if($cart_item["weight"] >= 300){ $cost += 2.50; } else if($cart_item["weight"] >= 200){ $cost += 1.0; } else if($cart_item >= 100){ $cost += ($isInternational)? 15.0 : 8.0; } if($cart_item["weight"] > $maxWeight){ $maxWeight = $cart_item["weight"]; } } } if($maxWeight >= 300) { $cost += ($isInternational) ? 11.0 : 8.0; $cost -= 2.50; } else if ($maxWeight >= 200) { $cost += ($isInternational) ? 10.0 : 6.0; $cost -= 1; } if($isInternational){ $description = "International Shipping"; } else { $description = "USPS Standard Shipping"; } // =========== return data to cart =============== $rates = array( array( "cost" => $cost, "description" => $description ), ); $response = array( "rates" => $rates ); header('HTTP/1.1 200 OK'); echo(json_encode($response)); break; default: header('HTTP/1.1 200 OK'); break; } ?> Thanks for any help you guys can give!
  3. Hello, I need some help and information about how to calculate. is it possible to make a calculation directly from database and output ? here below an example. database : Expense table: Money ---------------- | Gas | Gasoline | Diesel | total | ................................................................................................... $50 $20 $10 = $80 So what i want is to make the calculation like here above directly from database like today whe used ................................................................................................... $10 $5 $10 = $25 Thank in advance/
  4. This one is a doozy for me. My brain is fried after spending a couple days trying to figure this out. None of my formulas have seemed to work out enough. So I'm waiving my white flag and asking for some help. Below is a simplified version of my code. The purpose of this page is an estimate calculator. It is for a window cleaning service, and they have pricing for doing the insides of windows and the outsides. Each style of window has different pricing for each inside and outside. What is supposed to happen in the form is this: If they want an estimate for say basic window cleaning for both inside and outside, they will check the chechboxes for both inside and outside. Then enter a number for the amount of windows to clean. The formula for this would look something like this: Amount = Inside Price x Outside Price (if inside price and outside price checkboxes are ticked). And for each entry, it needs to add up the total. But none of the foreach or while loops i try to create seem to do the trick. Basic Form Setup: $query = "SELECT * FROM estimates WHERE 1 ORDER BY id ASC"; $result = mysql_query($query); while ($estimates = mysql_fetch_object($result)) { $style = $estimates->style; $insidePrice = $estimates->price; $outsidePrice = $estimates->out; echo $style.' <input type="checkbox" name="insidePrice[]" value ="'.$insidePrice,'" /> $'.$insidePrice.' <input type="checkbox" name="outsidePrice[]" value ="'.$outsidePrice,'" /> $'.$outsidePrice.' <input type="text" name="amount[]" />'; } And an idea of how i want this to work: foreach ($_POST['amount'] as $key => $a){ if ($a != "" or $a !="0"){ //Now to llustrate what Im trying to do, I know this is wrong though foreach ($_POST['insideAmount'] as $i){ $i = $i*$a; } foreach ($_POST['outsideAmount'] as $o){ $o = $o*$a; } $a = $i+$o; } //Trying to add each row as they build to make the estimate total $prev = $a[$key-1]; $a = $a+$prev; $total = $a; } echo "$total";
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.