PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 19, 2022

[FIXED] how to get data from 2 fields json in table css

 January 19, 2022     database, json, php, phpmyadmin     No comments   

Issue

hi sir sorry i want to ask again

how to if get 2 fields in json?

if 1 fields, like this :

$json = json_decode($row['orders'], true);  // 2nd argument `true` converts json object into array.
$email = $json['email'];

and then add this :

<td>'.$json["email"].'</td>

how to get 2 fields from json sir, i mean like this

fields 1 : Orders

========
orders
======== 
{email":"tst@gmail.com","name":"myname","phone":"+123123123"}

fields 2 : Country

=========
country
=========
[{id":"001","country:"english","code:"123123"}]

see the country fields diferent code from orders fields

in order fields have code {}

and in counrty fields have code [{}]

i try like this not work :

$json_orders = json_decode($row['orders'], true);
$email = $json_orders['email'];
$json_country = json_decode($row['country'], true);
$country = $json_country['country'];

and then this in table css :

<td>'.$json['email'].'</td>

<td>'.$json['country'].'</td>

how to get data from 2 fields in json sir?

thank you


Solution

Your json input data is malformed because there are several missing double quotes.

Specifically, email and id need a double quote before them and country and code need a double quote immediately after.

Initially, I assumed that this was a code posting error, but if this is the problem in your actual project, then something is not generating valid json strings. If you are building these strings, I hope you are converting arrays using json_encode() and not trying to write these quasi-json strings manually with concatenation.

If you fix your json strings, you can decode and access the data like this:

Code: (Demo)

$row['orders'] = '{"email":"tst@gmail.com","name":"myname","phone":"+123123123"}';  // assoc array
$row['country'] = '[{"id":"001","country":"english","code":"123123"}]';  // indexed array containing an assoc array

$json_orders = json_decode($row['orders'], true);
echo $json_orders['email'];
echo "\n---\n";
$json_country = json_decode($row['country'], true);
echo $json_country[0]['country'];

Output:

tst@gmail.com
---
english

*Note, that $row['country'] has a different structure (multi-dimensional) than $row['orders'], so you will have to access the first level with [0].



Answered By - mickmackusa
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing