phpBB2Refugees.com Logo
Not affiliated with or endorsed by the phpBB Group

Register •  Login 

Continue the legacy...

Welcome to all phpBB2 Refugees!Wave Smilie

This site is intended to continue support for the legacy 2.x line of the phpBB2 bulletin board package. If you are a fan of phpBB2, please, by all means register, post, and help us out by offering your suggestions. We are primarily a community and support network. Our secondary goal is to provide a phpBB2 MOD Author and Styles area.

JSON PHP Question


 
Search this topic... | Search phpBB2 Discussion... | Search Box
Register or Login to Post    Index » phpBB2 Discussion  Previous TopicPrint TopicNext Topic
Author Message
JLA
Board Member



Joined: 30 Apr 2009

Posts: 298
Location: U.S.A


flag
PostPosted: Wed Jul 04, 2012 4:56 pm 
Post subject: JSON PHP Question

Having a bit of confusion with this.

Using this test received JSON result (there is only 1 result in this example but other uses might have multiple results)

{
"success": true,
"numResults": 1,
"execTimeMs": 10,
"results":
[
{
{
"TestAccount": "testingaccount1",
"testnotes": {
"test_department": "creative",
"test_office_location": "98th Floor"
},
"testdescription": "creative test description\n \n",
"testaccountcat": "intl",
"deptimages":
[
"http://www.test.com/creativedeptlogo.jpg",
"http://www.test.com/creativedepthead.jpg.jpg"
],
"officenumber": 8531212
}
]
}

trying to figure out a way to have all of these items assigned as PHP variables for another script that would insert them into a MYSQL database.

So far have this

$url = "[we would have the url where the JSON data is available here that would produce the output seen in the example above]"
$json = file_get_contents($url);
$out = json_decode($json,true);

If I output $out to a file, I get this
Array
(
[success] => 1
[numResults] => 1
[execTimeMs] => 10
[results] => Array
(
[0] => Array
(
[testAccount] => testingaccount1
[testnotes] => Array
(
[test_department] => creative
[test_office_location] => 98th Floor
)

[testdescription] =>
creative test description\n \n
[testaccountcat] => intl
[deptimages] => Array
(
[0] => http://www.test.com/creativedeptlogo.jpg

[1] => http://www.test.com/creativedepthead.jpg.
)

[officenumber] => 8531212
)

)

)

Thanks in advance for any input

_________________
http://www.jlaforums.com
Back to top
JLA
Board Member



Joined: 30 Apr 2009

Posts: 298
Location: U.S.A


flag
PostPosted: Thu Jul 05, 2012 5:04 pm 
Post subject: Re: JSON PHP Question

Wanted to clarify a bit

Sorry, here are the items in code brackets

Using this test received JSON result (there is only 1 result in this example but other uses might have multiple results)

Code:
{
"success": true,
"numResults": 1,
"execTimeMs": 10,
"results":
[
{
{
"TestAccount": "testingaccount1",
"testnotes": {
"test_department": "creative",
"test_office_location": "98th Floor"
},
"testdescription": "creative test description\n \n",
"testaccountcat": "intl",
"deptimages":
[
"http://www.test.com/creativedeptlogo.jpg",
"http://www.test.com/creativedepthead.jpg.jpg"
],
"officenumber": 8531212
}
]
}


trying to figure out a way to have all of these items assigned as PHP variables for another script that would insert them into a MYSQL database.

So far have this
Code:
$url = "[we would have the url where the JSON data is available here that would produce the output seen in the example above]"
$json = file_get_contents($url);
$out = json_decode($json,true);


If I output $out to a file, I get this

Code:
Array
(
[success] => 1
[numResults] => 1
[execTimeMs] => 10
[results] => Array
(
[0] => Array
(
[testAccount] => testingaccount1
[testnotes] => Array
(
[test_department] => creative
[test_office_location] => 98th Floor
)

[testdescription] =>
creative test description\n \n
[testaccountcat] => intl
[deptimages] => Array
(
[0] => http://www.test.com/creativedeptlogo.jpg

[1] => http://www.test.com/creativedepthead.jpg.
)

[officenumber] => 8531212
)

)

)


Someone made a comment

Quote:
They are already in array variables, so there is really no need to assign them to other variables.


Quote:
but for example, if you want to reference the "testingaccount1" value, it would appear to be:
Code:
$out['results'][0]['testAccount']


I understand what was said there. I should have used the multiple result example as it is more relevant to the point of my question - so I'll provide and example output for $out with a multiple result JSON decode

Code:

            [0] => Array
                (                   
                    [testAccount] => testingaccount1
                    [testnotes] => Array
                        (
                            [test_department] => creative
                            [test_office_location] => 98th Floor                           
                        )

                    [testdescription] =>
creative test description\n \n

                    [testaccountcat] => intl                 
                    [deptimages] => Array
                        (
                            [0] => http://www.test.com/creativedeptlogo.jpg
                            [1] =>  http://www.test.com/creativedepthead.jpg
                           
                        )
                   
                    [officenumber] => 8531212
                )

            [1] => Array
                (                   
                    [testAccount] => testingaccount2
                    [testnotes] => Array
                        (
                            [test_department] => security
                            [test_office_location] => 97th Floor                           
                        )

                    [testdescription] =>
security test description\n \n

                    [testaccountcat] => govt                 
                    [deptimages] => Array
                        (
                            [0] => http://www.test.com/securitydeptlogo.jpg
                            [1] =>  http://www.test.com/securitydepthead.jpg
                           
                        )
                   
                    [officenumber] => 8765309
                )

        )

)


So when we have these decoded JSON results, there may be anywhere from 1 to 200 results. So each individual result set starts with

Code:
[0] => Array

then the next result set is
[1] => Array

and the next
[2] => Array

and so on.....


We need to take each of the items from each result set and inject them into a MYSQL Table that contains matching fields. Just not sure the easiest way to do this since the result set numbers change and the number of results can be different each time.

Thanks again.

_________________
http://www.jlaforums.com
Back to top
drathbun
Board Member



Joined: 24 Jul 2008

Posts: 653
Location: Texas


flag
PostPosted: Mon Jul 09, 2012 1:43 pm 
Post subject: Re: JSON PHP Question

I've never used this technique, so I am not familiar with it. Sorry...
_________________
phpBBDoctor Blog
Back to top
JLA
Board Member



Joined: 30 Apr 2009

Posts: 298
Location: U.S.A


flag
PostPosted: Wed Jul 18, 2012 4:21 pm 
Post subject: Re: JSON PHP Question

No problem. We figured it out.

Thanks

_________________
http://www.jlaforums.com
Back to top
JLA
Board Member



Joined: 30 Apr 2009

Posts: 298
Location: U.S.A


flag
PostPosted: Tue Nov 20, 2012 3:37 pm 
Post subject: Re: JSON PHP Question

Figured we would share a bit of code that starts this all out in case anyone else is working on this issue

Code:

//JSON Code Snipped - JLA
//Using PHP 5.2.X

//After you make a query of your stored JSON URLs

foreach($jsonurls as $jsonstuff)
   {
             echo "-->Assigning URL variable for Json query\n";
         $jsonurl = $jsonstuff['URL'];
         echo "-->Successfully assigned URL variable\n";
         echo "-->Now querying Json URL $jsonurl\n\n";
         $json = file_get_contents($jsonurl);
         $out = json_decode($json,true);

              foreach($out['results'] as $record)
            {
            echo "-->Assigning variables from Json data URL $jsonurl\n";
            $yourvariable   = $record['someJsonrecord'];            
            $yourvariable1   = $record['someJsonrecord1'];            
            echo "-->Variables assigned successfully\n";
                      }
          }

//END JSON Code Snippet - JLA


_________________
http://www.jlaforums.com
Back to top
Holger
Board Member



Joined: 19 Jan 2009

Posts: 509
Location: Hanover


flag
PostPosted: Wed Nov 21, 2012 7:49 am 
Post subject: Re: JSON PHP Question

Ummmm, what does this actually do?
_________________
Love your data! Back it up!
Back to top
JLA
Board Member



Joined: 30 Apr 2009

Posts: 298
Location: U.S.A


flag
PostPosted: Wed Nov 21, 2012 2:41 pm 
Post subject: Re: JSON PHP Question

Holger wrote:
Ummmm, what does this actually do?


Allows you to use PHP to take a JSON source http://www.json.org/ and use and/or manipulate the data for your choosing.

_________________
http://www.jlaforums.com
Back to top
Display posts from previous:   
Register or Login to Post    Index » phpBB2 Discussion  Previous TopicPrint TopicNext Topic
Page 1 of 1 All times are GMT
 
Jump to:  

Index • About • FAQ • Rules • Privacy • Search •  Register •  Login 
Not affiliated with or endorsed by the phpBB Group
Powered by phpBB2 © phpBB Group
Generated in 0.0158 seconds using 15 queries. (SQL 0.0025 Parse 0.0003 Other 0.0130)
phpBB Customizations by the phpBBDoctor.com
Template Design by DeLFlo and MomentsOfLight.com Moments of Light Logo