Trying to implement a JSON array in PHP
Using
Just wonder how to supress repeats.
On 28/12/2022 01.34, The Doctor wrote:
Trying to implement a JSON array in PHP
Using
<too much code so removed, use a code repository and link to it>
Just wonder how to supress repeats.
Don't insert the same data multiple times into an array.
Here is a short code demonstrating what you are doing
--- example.php ---
<?PHP
for($i = 0; $i < 3; $i++) { // this replaces the foreach in the original
code
$MyObj[] = array("same each time", "this from round $i");
var_dump($MyObj);
}
--- eof ---
You can see how the array grows each iteration of the loop.
Some tips:
Separate display and logic into two files, one with only the logic and
one with only showing the resulting data, this makes it far cleaner and >simpler to update the design of the page without affecting the logic.
Do not have variable with the same name but different case, it makes
your code far more difficult to follow and more mistakes can sneak in
Validate input data, always go with whitelisting of what is allowed to
be in a input, this way you always know the data is safe to use. Regular >expression can make this simpler.
Always calculate the total, do not trust that from a post or else
customers gets all the items for free. Also item prices should always be >fetched from the stored price list, not from a post value.
Store data that you don't want to be manipulated but you need no the
next page in the session, as this data can't be manipulated by the user
and you don't need to revalidate the data on each page.
--
//Aho
Trying to implement a JSON array in PHP
Using[...]
$myObj = [];[...]
if (isset($_POST["submit"])){
(array_push($myObj,[
'bill_first_name' =>[$_POST['bill_first_name']],
Just wonder how to supress repeats.
In article <k12emkF4sbjU3@mid.individual.net>,
J.O. Aho <user@example.net> wrote:
On 28/12/2022 01.34, The Doctor wrote:
Trying to implement a JSON array in PHP
Using
<too much code so removed, use a code repository and link to it>
Just wonder how to supress repeats.
Don't insert the same data multiple times into an array.
Here is a short code demonstrating what you are doing
--- example.php ---
<?PHP
for($i = 0; $i < 3; $i++) { // this replaces the foreach in the original
code
$MyObj[] = array("same each time", "this from round $i");
var_dump($MyObj);
}
--- eof ---
You can see how the array grows each iteration of the loop.
Just that we are converting from php to php/json.
Further, the foreach is due to a line that is coming is from
a previous session being passed on.
Trying to implement a JSON array in PHP
Using
<?=session_start();
error_reporting(E_ALL);
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="index, follow" />
<link rel="stylesheet" type="text/css" href="css/css.css"/>
<link rel="stylesheet" type="text/css" href="css/css2.css"/>
<link rel="stylesheet" href="css/uniform.default.css" type="text/css" media="screen">
<link rel="stylesheet" href="css/uniform.agent.css" type="text/css" media="screen">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
</head>
<body>
<?php
if(!empty($_SESSION['sessiondata'])){
}
<div id="wrapper">
<!--header begins-->
<?php
include("includes/header.php");
<!--header ends-->
<FORM ACTION="https://gatewayt.moneris.com/chkt/request/request.php" method=post>
<div id="outerDiv" style="width:400px"; height"300px">`
<div id="monerisCheckout"></div></div>
<script src="https://gatewayt.moneris.com/chkt/js/chkt_v1.00.js"></script> <script>
var myCheckout = new monerisCheckout();
myCheckout.setMode("qa");
myCheckout.setCheckoutDiv("monerisCheckout");
myCheckout.setCheckout([ticket #]);
myCheckout.setCallback("page_loaded", myPageLoad); myCheckout.setCallback("cancel_transaction", myCancelTransaction); myCheckout.setCallback("error_event", myErrorEvent); myCheckout.setCallback("payment_receipt", myPaymentReceipt); myCheckout.setCallback("payment_complete", myPaymentComplete);
</script>
<?php
<center>
<!-- Store Settings-->
<INPUT TYPE="HIDDEN" NAME="ps_store_id" VALUE="<?=$myObj->store_id?>"> <INPUT TYPE="HIDDEN" NAME="checkout_id" VALUE="<?=$myObj->checkout_id?>"> <!------- DEFINE CHARGE TOTAL HERE --->
<br/><br/><h3>Your Purchase Total Is: </h3> <br/> <h2>$<?=$myObj->txn_total?></h2>
<INPUT TYPE="hidden" NAME="charge_total" VALUE="<?=$myObj->txn_total?>"><br><br>
<!-- Unique Order ID -->
<!--INPUT TYPE="hidden" NAME="order_id" VALUE="<?=$myObj->sessValue?>"--> <!-- Additional Optional Details -->
<input type="hidden" name="cust_id" value="<?=$MyObj->sessValue?>">
<input type="hidden" name="email" value="<?=$myObj->email?>">
<input type="hidden" name="note" value="">
<!-- Item Information -->
<?php
$items_count = 1;
$subtotal = 0;
$arr = 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);
<input name="quantity<?=$items_count?>" value="<?=$quantity?>">
<input name="description<?=$items_count?>" value="<?=$_POST['description'][$key]?>">
<input name="id<?=$items_count?>" value="<?=$_POST['id'][$key]?>">
<input name="price<?=$items_count?>" value="<?=$unit_cost?>">
<input name="subtotal<?=$items_count?>" value="<?=$subtotal?>">
<?php
$myObj = [];
if (isset($_POST["submit"])){
(array_push($myObj,[
'sessValue' =>[ $_POST['sessionid1']],
'username'=> ["demouser"],
'password' => ["password"],
'store_id' => ["store3"],
'checkout_id' => ["chkt23NGFtore3"],
'api_token' => ["yesguy"],
'txn_total' => [$_POST['charge_total']],
'bill_first_name' =>[$_POST['bill_first_name']],
'bill_last_name' => [$_POST['bill_last_name']],
'bill_company_name' => [$_POST['bill_company_name']],
'bill_address_one' => [$_POST['bill_address_one']],
'bill_city' => [$_POST['bill_city']],
'bill_state_or_province' => [$_POST['bill_state_or_province']], 'bill_postal_code' => [$_POST['bill_postal_code']],
'bill_phone' => [$_POST['bill_phone']],
'email' => [$_POST['email']],
$MyObj[] = array(
"cart"=>array(
"items"=>array(
'url' => ['https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png'], 'description' => [$_POST['description'][$key]],
'unit_cost' => $unit_cost,
'quantity' => $value,
),
'subtotal' => $subtotal,
),
),
array_push($arr, $MyObj),
]));
$items_count++;
}
}
<!-- Billing Information -->
<input type="hidden" name="bill_first_name" value="<?=$MyObj->bill_first_name?>">
<input type="hidden" name="bill_last_name" value="<?=$bill_last_name?>"> <input type="hidden" name="bill_company_name" value="<?=$bill_company_name?>">
<input type="hidden" name="bill_address_one" value="<?=$bill_address_one?>"> <input type="hidden" name="bill_city" value="<?=$bill_city?>">
<input type="hidden" name="bill_state_or_province" value="<?=$bill_state_or_province?>">
<input type="hidden" name="bill_postal_code" value="<?=$bill_postal_code?>"> <input type="hidden" name="bill_country" value="<?=$bill_country?>">
<input type="hidden" name="bill_phone" value="<?=$bill_phone?>">
<input type="hidden" name="bill_fax" value="<?=$bill_fax?>">
<?php
$myJSON = json_encode($myObj);
echo $myJSON;
}
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Click to proceed to Secure Page"> </center>
</FORM>
<?php
include("includes/footer.php");
</div>
</body>
</html>
I am getting an output of
YOUR PURCHASE TOTAL IS:
$
null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullnull null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null
1bill_address_one":[""],"bill_city":[""],"bill_state_or_province":[""],"bill_postal_code":[""],"bill_phone":["7804734587"],"email":["ro...@nk.ca"],"0":{"cart":{"items":{"url":["https:\\\/\\\/www.pdsolutions.ca\\\/images\\\/newwhiteheader.png"],"
Fatigue Management
FM-1301
60
60 [{"sessValue":[null],"username":["demouser"],"password":["password"],"store_id":["store3"],"checkout_id":["chkt23NGFtore3"],"api_token":["yesguy"],"txn_total":["126.00"],"bill_first_name":["Dave"],"bill_last_name":["Yadallee"],"bill_company_name":[""],"
1bill_address_one":[""],"bill_city":[""],"bill_state_or_province":[""],"bill_postal_code":[""],"bill_phone":["7804734587"],"email":["ro...@nk.ca"],"0":{"cart":{"items":{"url":["https:\\\/\\\/www.pdsolutions.ca\\\/images\\\/newwhiteheader.png"],"
Pesticide Applicator Records
PAR-1302
60
60 [{"sessValue":[null],"username":["demouser"],"password":["password"],"store_id":["store3"],"checkout_id":["chkt23NGFtore3"],"api_token":["yesguy"],"txn_total":["126.00"],"bill_first_name":["Dave"],"bill_last_name":["Yadallee"],"bill_company_name":[""],"
Just wonder how to supress repeats.
--
Member - Liberal International This is doc...@nk.ca Ici doc...@nk.ca
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
Happy Christmas 2022 and Merry New Year 2023 Beware https://mindspring.com
On 28/12/2022 15.42, The Doctor wrote:
In article <k12emk...@mid.individual.net>,
J.O. Aho <us...@example.net> wrote:
On 28/12/2022 01.34, The Doctor wrote:
Trying to implement a JSON array in PHP
Using
<too much code so removed, use a code repository and link to it>
Just wonder how to supress repeats.
Don't insert the same data multiple times into an array.
Here is a short code demonstrating what you are doing
--- example.php ---
<?PHP
for($i = 0; $i < 3; $i++) { // this replaces the foreach in the original >> code
$MyObj[] = array("same each time", "this from round $i");
var_dump($MyObj);
}
--- eof ---
You can see how the array grows each iteration of the loop.
Just that we are converting from php to php/json.The issue ain't related to the conversion, it's you logic that is flawed.
Further, the foreach is due to a line that is coming is fromBut it's your foreach loop that makes you to have duplicates in your
a previous session being passed on.
example in the same way as in the small example file. Take some time and think about what happens in each iteration, when you know what "$MyObj[]
=" means, then you can make the proper changes in your own code.
I would also think you could simplify the whole thing for example a lot
of the data is static, just set that data once not each time in a
foreach loop and also as Arno already pointed out, don't add a single
value as an array, you just make things more complex than they need to be.
Remember to not use input type="hidden" for information a user already
have posted to you, those goes into the session, the only thing would be
an identifier that is unique for this transaction. Prices you should
never take from post values, only the amount of items, for if you start
trust the posted values, I will be able to buy everything you have for 0 bucks.
--
//Aho
Hey, can U help me ------> https://groups.fakeusergroupagent..example.com/g/magic/c/CRDqi-wyQJU
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 349 |
Nodes: | 16 (2 / 14) |
Uptime: | 119:46:15 |
Calls: | 7,612 |
Files: | 12,787 |
Messages: | 5,684,114 |