Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

It is not well to be thought of as one who meekly submits to insolence and intimidation.


devel / comp.lang.php / Re: Replace punctuation in an associative array

Re: Replace punctuation in an associative array

<ka32kmF6nfjU1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=842&group=comp.lang.php#842

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: user@example.net (J.O. Aho)
Newsgroups: comp.lang.php
Subject: Re: Replace punctuation in an associative array
Date: Sun, 16 Apr 2023 22:03:34 +0200
Lines: 282
Message-ID: <ka32kmF6nfjU1@mid.individual.net>
References: <ttq5np$i9r$50@gallifrey.nk.ca> <u1a99j$1gfe$15@gallifrey.nk.ca>
<k9th4vFauniU1@mid.individual.net> <u1cjle$k24$48@gallifrey.nk.ca>
<u1gqb7$h84$56@gallifrey.nk.ca>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net R09t8rLDN3bACnwU+inVZwzxBihEPjdy5BvO+ZRw0zuEIMT7If
Cancel-Lock: sha1:VpwmXoKfsZIEFQHvs9rCXJbYq2c=
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.10.0
Content-Language: en-US-large
In-Reply-To: <u1gqb7$h84$56@gallifrey.nk.ca>
 by: J.O. Aho - Sun, 16 Apr 2023 20:03 UTC

On 4/16/23 14:40, The Doctor wrote:
> In article <u1cjle$k24$48@gallifrey.nk.ca>,
> The Doctor <doctor@doctor.nl2k.ab.ca> wrote:
>> In article <k9th4vFauniU1@mid.individual.net>,
>> J.O. Aho <user@example.net> wrote:
>>> On 4/14/23 03:12, The Doctor wrote:
>>>> 95% there.
>>>>
>>>>
>>>> Now I have split the form successfully I need to get
>>>> the cartitem in a printable format.
>>>>
>>>> So given when I have worked on
>>>>
>>>>
>>>> <?=session_start();
>>>> error_reporting(E_ALL);
>>>> ?>
>>>> <!DOCTYPE html>
>>>> <html lang="en">
>>>> <head>
>>>> <meta name="viewport" content="width=device-width, initial-scale=1,
>>> maximum-scale=1.0, user-scalable=no">
>>>> <link rel="stylesheet" type="text/css" href="css/css.css"/>
>>>> <link rel="stylesheet" type="text/css" href="css/css2.css"/>
>>>> </head>
>>>> <?php
>>>> if(!empty($_SESSION['sessiondata'])){
>>>> }
>>>> ?>
>>>> <?php
>>>> $myObj = [];
>>>> $storevalues= array();
>>>> $cartarray= array();
>>>> $cart= array();
>>>> $contact1=array();
>>>> $contact_details=array();
>>>> $shipping1=array();
>>>> $shipping_details=array();
>>>> $billing1=array();
>>>> $billing_details=array();
>>>> $arr2 =array();
>>>
>>> Try to avoid all these arrays, they don't make life easier.
>>>
>>>> class Storevs{
>>>> }
>>>> class Item
>>>> {
>>>> }
>>>> class TaxItem
>>>> {
>>>> }
>>>>
>>>> class CartItem
>>>> {
>>>> public $items;
>>>> public $subtotal;
>>>> public $tax;
>>>> };
>>>>
>>>> class Contact
>>>> {
>>>> }
>>>>
>>>> class Shipping
>>>> {
>>>> }
>>>>
>>>> class Billing
>>>> {
>>>> }
>>>
>>> Don't define the classes here, then you need to do it each file you use,
>>> you create an php script that just includes the classes and then use include
>>> https://www.php.net/manual/en/function.include.php
>>>
>>>> $items_count = 0;
>>>> $newsubtotal = 0;
>>>> $subtotal = 0;
>>>> $arr = array();
>>>>
>>>> if (isset($_POST["submit"])){
>>>> $storevalues=(array) $storev;
>>>> }
>>>>
>>>> if (isset($_POST["submit"])){
>>>> $cartitem= new CartItem();
>>>>
>>>> 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
>>>> }
>>>
>>> prices shouldn't be hard coded, you don't want to change the code just
>>> for the price went up due of inflation, you change the price in a
>>> database of some sort.
>>>
>>>> $item = new Item();
>>>> $item->url = "https://image";
>>>> $item->description = $_POST['description'][$key];
>>>> $item->product_code = $_POST['id'][$key];
>>>> $item->unit_cost = $unit_cost;
>>>> $item->quantity = $value;
>>>> $cartitem->items[] = $item;
>>>>
>>>> }
>>>> }
>>>>
>>>> $cartitem->subtotal = ($_POST['charge_total']/1.05);
>>>
>>> Hey, this ain't a value you can trust, you need to go trough all the
>>> items in the cart, check up the price for each item and calculate the total.
>>>
>>> Say a user with one of the many browser plugins that allows you to
>>> modify post data, the user just adds all the items it wants and then
>>> change the value for charge_total to 0, please calculate how pissed your
>>> boss will be.
>>>
>>>>
>>>> $taxItem = new TaxItem();
>>>> $taxItem->amount = (($_POST['charge_total']/1.05) * 0.05);
>>>> $taxItem->description = "GST";
>>>> $taxItem->rate = "5.00";
>>>> $cartitem->tax = $taxItem;
>>>> $cart= (array) $cartitem ;
>>>> $cartarray = array( "cart" => $cartitem);
>>>> }
>>>>
>>>> if (isset($_POST["submit"])){
>>>> $contact=new Contact;
>>>> $contact1= (array) $contact ;
>>>> $contact_details=array ( "contact_details" => $contact1 );
>>>> }
>>>> if (isset($_POST["submit"])){
>>>> $shipping = new Shipping;
>>>> $shipping1 = (array) $shipping;
>>>> $shipping_details=array( "shipping_details" => $shipping1 );
>>>> }
>>>> if (isset($_POST["submit"])){
>>>> $billing = new Billing;
>>>> $billing_details=array( "billing_details" => $billing1 );
>>>>
>>>> }
>>>>
>>>> $_SESSION['arr'] = serialize($arr);
>>>> $_SESSION['serialized_Obj'] = serialize($myObj);
>>>>
>>>> ?>
>>>>
>>>> <body>
>>>> <form action="https://www.nk.ca/pdsolutions/step5b.php" method="post" >
>>>>
>>>> <h2> Please review your order!</h2>
>>>> <?php
>>>> echo "<br /><br />";
>>>
>>> why do you echo the html code? leave those outside the php code, you can
>>> have multiple <?php ?> in a page.
>>>
>>>> echo "<h3> Contact Information </h3>";
>>>>
>>>> echo "<br /> My name is " . $contact->first_name . " " .
>>> $contact->last_name . "<br />";
>>>> echo "My E-mail address is " . $contact->email . " <br />";
>>>> echo "You can call me at " . $contact->phone . " <br /> <br />";
>>>>
>>>> echo "<h3> Billing Information </h3>";
>>>>
>>>> echo "<br /> Address :" . $billing->address_1 . "<br />";
>>>> echo $billing->address_2 . "<br />";
>>>> echo "Municipality:" . $billing->city . "<br />";
>>>> echo "Province:" . $billing->province . "<br />";
>>>> echo "Postal Code:" . $billing->postal_code . "<br /><br />";
>>>>
>>>> echo "<h3> Shipping Information </h3>";
>>>>
>>>> echo "<br /> Address :" . $shipping->address_1 . "<br />";
>>>> echo $shipping->address_2 . "<br />";
>>>> echo "Municipality:" . $shipping->city . "<br />";
>>>> echo "Province:" . $shipping->province . "<br />";
>>>> echo "Postal Code:" . $shipping->postal_code . "<br /><br />";
>>>>
>>>>
>>>>
>>>> echo "<h3> Course Information </h3>";
>>>
>>> Here you use a foreach loop that goes over the cart items, of course you
>>> need to deserialize the $_SESSION['serialized_Obj'].
>>> https://www.php.net/manual/en/control-structures.foreach.php
>>>
>>
>> Got you on all accounts!
>>
>>>
>>>> ?>
>>>> <input type="submit" value="Place Your Order" name="submit" id="submit" >
>>>> <input onclick="history.back()" type="reset" value="Clear the
>>> Form" id="reset" />
>>>> </form>
>>>> </body>
>>>> </html>
>>>
>>> --
>>> //Aho
>>
>>
>
>
> All right, trying to debug this here is what is happening:
>
> code snippet
>
> echo "<h3> Course Information </h3>";
> echo '<pre>'; print_r($cart); echo '</pre>';
> $keys = array_keys($cart);
> print_r($keys);
>
> foreach ($_POST['quantity'] as $key => $value) {
> if( $value > 0){

I think this is unnecessary, I wouldn't add an item with quantity of 0
to the cart.

> echo "quantity " . $item->quantity . " of " . $item->description
> . " <br />";}
> }
> ?>
>
>
> REsult:
>
> COURSE INFORMATION
> Array
> (
> [items] => Array
> (
> [0] => Item Object
> (
> [url] => https://www.pdsolutions.ca/images/newwhiteheader.png
> [description] => Alberta Legislation Update
> [product_code] => ALU-1304
> [unit_cost] => 60
> [quantity] => 1
> )
>
> [1] => Item Object
> (
> [url] => https://www.pdsolutions.ca/images/newwhiteheader.png
> [description] => AA training and exam
> [product_code] => AAtt-1306
> [unit_cost] => 60
> [quantity] => 1
> )
> )
>
> [subtotal] => 320
For me it seems you have something wrong with your calculation (60*1 +
60*1) * 1.05 ain't 320.
> [tax] => TaxItem Object
> (
> [amount] => 16
> [description] => GST
> [rate] => 5.00
> )

Also 16 an't 5% of 120, you need to look into your summary and tax
calculation.

> )
> Array ( [0] => items [1] => subtotal [2] => tax ) quantity 1 of AA training and exam quantity 1 of AA training and exam

If the quantities are not what you expected, then you maybe didn't
update the quantity before storing values to the session. Keep in mind
that session values may need to be updated if someone posts new values.

--

//Aho

SubjectRepliesAuthor
o Re: Replace punctuation in an associative array

By: The Doctor on Thu, 6 Apr 2023

6The Doctor
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor