-
BeVirtual Newbie

-
Posts: 5
Threads: 1
Joined: Apr 2016
Reputation:
0
Hi,
Please can someone give me some advice, I have setup an API which is working but cant seem to get some of the json data.
this is the data being posted(cant be changed coming from out side source)
{
"code": "VET002",
"store": "DBNG0024",
"items": [
{
"code": "1",
"descr": "Yoghurt",
"value": "66",
"qty": "3",
"dept": "1"
},
{
"code": "25",
"descr": "Red Bull",
"value": "44",
"qty": "2",
"dept": "2"
},
{
"code": "23",
"descr": "Lipton Lemon",
"value": "72",
"qty": "6",
"dept": "2"
}
]
}
this is how I'm reading the data
$code = $this->post('code');
$store_code = $this->post('store');
$total = $this->post('total');
how would i read the items values.
Thank you
-
EricP Newbie

-
Posts: 4
Threads: 0
Joined: Nov 2015
Reputation:
0
(04-07-2016, 12:03 AM)Try Wrote: PHP Code: $input = $_REQUEST['your_request_data_name']; //Set this in your javascript (ajax request) $data = json_decode($input, true);
$code = $data->code; $store = $data->store;
"total" is not defined in your JSON.
-
BeVirtual Newbie

-
Posts: 5
Threads: 1
Joined: Apr 2016
Reputation:
0
(04-07-2016, 02:44 AM)EricP Wrote: (04-07-2016, 12:03 AM)Try Wrote: PHP Code: $input = $_REQUEST['your_request_data_name']; //Set this in your javascript (ajax request) $data = json_decode($input, true);
$code = $data->code; $store = $data->store;
"total" is not defined in your JSON.
thanks for your reply sorry but I'm still bit confused about this part
PHP Code: $input = $_REQUEST['your_request_data_name']; //Set this in your javascript (ajax request)
the data is coming from an external source that is connecting to out api. the api is working I just not sure how to access the items array.
when I echo items all it prints is the word "Array"
-
Tpojka Junior Member
 
-
Posts: 44
Threads: 6
Joined: Oct 2014
Reputation:
3
You can't "echo" an array. Language construct "echo" is used to output one or more strings.
Use print_r instead and you could see what is in array.
-
BeVirtual Newbie

-
Posts: 5
Threads: 1
Joined: Apr 2016
Reputation:
0
04-07-2016, 10:00 PM
(This post was last modified: 04-07-2016, 10:01 PM by BeVirtual.)
(04-07-2016, 05:11 PM)Tpojka Wrote: You can't "echo" an array. Language construct "echo" is used to output one or more strings.
Use print_r instead and you could see what is in array.
Hi Tpojka,
Sorry it I used the wrong term i dont mean echo, I'm just trying to figure out how to get the values within the "items" array.
I can get the code and the store, but when I try get "items" all i get is the word "Array"(when using print_r)
Hope I have explained myself correctly this time. please bare with me I'm new to all this.
-
paulkd Member
  
-
Posts: 67
Threads: 15
Joined: Oct 2015
Reputation:
0
04-08-2016, 01:17 AM
(This post was last modified: 04-08-2016, 01:18 AM by paulkd.)
Hope this helps.
PHP Code: $json = ' { "code": "VET002", "store": "DBNG0024", "items": [{ "code": "1", "descr": "Yoghurt", "value": "66", "qty": "3", "dept": "1" }, { "code": "25", "descr": "Red Bull", "value": "44", "qty": "2", "dept": "2" }, { "code": "23", "descr": "Lipton Lemon", "value": "72", "qty": "6", "dept": "2" }] } ';
$arr = json_decode($json, true); $total = 0;
?>
<p>Store: <?php echo $arr['store']; ?></p> <p>Code: <?php echo $arr['code']; ?></p> <p>Items:</p> <ul> <?php foreach($arr['items'] as $arr2): ?> <li> <?php foreach($arr2 as $itemKey => $itemValue): ?> <?php echo '<b>'.$itemKey.'</b>: '.$itemValue; ?> <?php endforeach; ?> </li> <?php $total += $arr2['qty']; ?> <?php endforeach; ?> </ul> <p>Total Qty: <?php echo $total; ?></p>
-
BeVirtual Newbie

-
Posts: 5
Threads: 1
Joined: Apr 2016
Reputation:
0
(04-08-2016, 01:17 AM)paulkd Wrote: Hope this helps.
PHP Code: $json = ' { "code": "VET002", "store": "DBNG0024", "items": [{ "code": "1", "descr": "Yoghurt", "value": "66", "qty": "3", "dept": "1" }, { "code": "25", "descr": "Red Bull", "value": "44", "qty": "2", "dept": "2" }, { "code": "23", "descr": "Lipton Lemon", "value": "72", "qty": "6", "dept": "2" }] } ';
$arr = json_decode($json, true); $total = 0;
?>
<p>Store: <?php echo $arr['store']; ?></p> <p>Code: <?php echo $arr['code']; ?></p> <p>Items:</p> <ul> <?php foreach($arr['items'] as $arr2): ?> <li> <?php foreach($arr2 as $itemKey => $itemValue): ?> <?php echo '<b>'.$itemKey.'</b>: '.$itemValue; ?> <?php endforeach; ?> </li> <?php $total += $arr2['qty']; ?> <?php endforeach; ?> </ul> <p>Total Qty: <?php echo $total; ?></p>
that does help a lot problem is that I dont have the json data its coming from an external source. so not sure what I would replace $json with.
might be helpful to mention im using rest server.
thanks again really appreciate the help
-
BeVirtual Newbie

-
Posts: 5
Threads: 1
Joined: Apr 2016
Reputation:
0
(04-08-2016, 02:11 AM)paulkd Wrote: Is the "external source" available publicly or is it private?
its private... basically a another company sends data in json array format to our api url
|