Re: Replace punctuation in an associative array
[email protected] (The Doctor) Sun, 16 Apr 2023 12:40:07 -0000 (UTC)
| Newsgroups | comp.lang.php |
|---|---|
| Organization | NetKnow News |
| Message-ID | <[email protected]> |
In article <[email protected]>, The Doctor <[email protected]> wrote: >In article <[email protected]>, >J.O. Aho <[email protected]> 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){ 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 [tax] => TaxItem Object ( [amount] => 16 [description] => GST [rate] => 5.00 ) ) Array ( [0] => items [1] => subtotal [2] => tax ) quantity 1 of AA training and exam quantity 1 of AA training and exam >-- >Member - Liberal International This is [email protected] Ici [email protected] >Yahweh, King & country!Never Satan President Republic!Beware AntiChrist rising! >Look at Psalms 14 and 53 on Atheism https://www.empire.kred/ROOTNK?t=94a1f39b >She didn't want to be evil, but by denying her nature she adopted it. >-unknown Beware https://mindspring.com -- Member - Liberal International This is [email protected] Ici [email protected] Yahweh, King & country!Never Satan President Republic!Beware AntiChrist rising! Look at Psalms 14 and 53 on Atheism https://www.empire.kred/ROOTNK?t=94a1f39b Compulsory enjoyment is unenjoyable. -unknown Beware https://mindspring.com