Re: Stripping a JSON of extra characters in PHP
"J.O. Aho" <[email protected]> Mon, 6 Feb 2023 18:14:07 +0100
| Newsgroups | comp.lang.php |
|---|---|
| Message-ID | <[email protected]> |
On 2/6/23 16:28, The Doctor wrote: > In article <[email protected]>, > J.O. Aho <[email protected]> wrote: >> On 06/02/2023 02:31, The Doctor wrote: >>> And in the case of recurring items when being puleld from >>> a form getting parsed? >> >> If you are thinking of post value which is an array, then the simplest >> would be: >> $a->cart = (object)$_POST['cart']; // this may give you object you don't >> want >> >> Just keep in mind that the data is unsafe to use, you should do the long >> way and validate the data and as you validate you can then add the >> values to a temp variable and if all the cart items passes validation, >> then add it to the main json object (in this silly example the $a). >> >> If you get a new cart posted (updates), then replace the current one >> you have in the session, don't forget to validate the data before you >> replace the cart section. Only have the latest validated one >> $a->cart = $validated_shopping_cart; // validated_shopping_cart is a >> json object >> >> for duplicate entries you have to decide what to do, cast an error or >> merge them together, that is up to you, but do not have duplicate items >> in your json object. >> >> I hope that was the answer you was looking for. >> > > The cart is being derived from a previous form being passed into PHP! > > > Just an update > The cart section is now an issue . > > 'cart' => array( > (foreach ($_POST['quantity'] as $key => $value) { > > if( $value > 0){ > > if(isset($_POST['with_gst'][$key])){ > $unit_cost = 63.00; > }else{ > $unit_cost = 60.00; //no gst included > } > > $subtotal = ($unit_cost * $value); > $quantity = ($value); > > $cart['items'][] =array( > 'url' => "https://www.pdsolutions.ca/images/newwhiteheader.png", > 'description' => $_POST['description'][$key], > 'product_code' => $_POST['id'][$key], > 'unit_cost' => $unit_cost, > 'quantity' => $quantity, > ), > $newtotal += $subtotal; > $items_count++; > array_push(items,itemsubarray); > } > } > > 'subtotal' => $newtotal, > if(isset($_POST['with_gst'][$key])){ > 'tax'=>array( > 'amount' => ($newtotal * 0.05), > 'description' => "GST", > 'rate' => "5.00", > ), > }) > ), It's messy and a lot of hard coded things and I'm just guessing what you try to do, I think I would have done things differently, for example posting all the data maybe as a serialized json object from the client side, I would fetch the items value from the database together with description, I would have the tax rate in the database too, easy to change the the tax in the database when the the government changes it. I made an small example that take the cart part, not sure if it's what you want to have, but at least should give you some idea and the reason I didn't use $_POST values is that it's easier to test things when you can modify values based on what you want and fine tune things till they are right, then you can modify the constructor to not take any arguments, I would make the "cart" class to hold all the values you need for your json. If you need to use values between functions, then use private, for then they will not be included when you use the json_encode() --- simple example --- <?php class cart { public $total = 0; public $items = []; private $uber = "man"; // this will not be included in the json output public function __construct($quantityArray, $gstArray, $descArray, $idArray) { foreach($quantityArray as $key => $num_items) { if($num_items == 0) continue; $has_tax = isset($gstArray[$key]); $unit_cost = $has_tax ? 63 : 60; $sub_total = $num_items * $unit_cost; $this->total = $sub_total; $item = [ 'url' => 'https://www.pdsolutions.ca/images/newwhiteheader.png', 'description' => $descArray[$key], 'product_code' => $idArray[$key], 'unit_cost' => number_format($unit_cost, 2), 'quantity' => $num_items, 'subtotal' => number_format($sub_total, 2), ]; if($has_tax) { $item['tax'] = (object)[ 'amount' => number_format($sub_total * 0.05), 'description' => "GST", 'rate' => '5.00' ]; } $this->items[] = (object)$item; } } } $q = [1, 2, 0]; $g = [1, 1, 0]; $d = ['banana', 'apple', 'car']; $i = [22, 11, 33]; $cart = new cart($q, $g, $d, $i); echo json_encode($cart, JSON_PRETTY_PRINT); ?> --- eof --- You can also see the use of number_format(), that just so we get the right number of decimals, I haven't bothered about rounding which would be sane. And it's the "items" that should be something similar to your cart. -- //Aho