Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #19303
| From | "J.O. Aho" <user@example.net> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Stripping a JSON of extra characters in PHP |
| Date | 2023-02-06 18:14 +0100 |
| Message-ID | <k4cqqvFpoj5U1@mid.individual.net> (permalink) |
| References | <tre1ei$2lti$75@gallifrey.nk.ca> <k49veoF1ne8U3@mid.individual.net> <trpl8k$12si$72@gallifrey.nk.ca> <k4bnmkFffdeU1@mid.individual.net> <trr6aj$1v6p$58@gallifrey.nk.ca> |
On 2/6/23 16:28, The Doctor wrote:
> In article <k4bnmkFffdeU1@mid.individual.net>,
> J.O. Aho <user@example.net> 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
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar
Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-01 15:45 +0000
Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-01 16:09 +0000
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-03 18:12 +0100
Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-03 23:56 +0000
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-05 00:28 +0100
Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-05 00:06 +0000
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-05 14:17 +0100
Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-05 20:58 +0000
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-05 23:38 +0100
Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-04 11:23 +0100
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-05 00:30 +0100
Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-04 23:44 +0000
Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-05 16:14 +0100
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-06 01:31 +0000
Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-06 08:14 +0100
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-06 15:28 +0000
Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-06 18:14 +0100
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 00:56 +0000
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-06 20:40 +0100
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 00:57 +0000
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 01:28 +0000
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-07 09:16 +0100
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-07 09:31 +0100
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 16:10 +0000
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-09 20:22 +0100
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-09 21:01 +0000
Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-10 11:49 +0100
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-10 14:39 +0000
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 16:05 +0000
Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-07 09:13 +0100
Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 16:05 +0000
csiph-web