Solved

Present data that change in PHP

Posted on 2014-02-19
2
484 Views
Last Modified: 2014-02-20
Hello,

I have an API which when called it posts back a JSON array of some data. I need to present these data on HTML either using PHP or Javascript or any combination of them.

The thing is that these data change over time from the first call, until about 2 minutes have passed. For example you can have this array if you call for the first time:

{
"records": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" }
]
}

and then if you call it in 20 seconds the array would return as:

{
"records": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}

I need to tie these changes to my PHP page so that if a new call brings up more data they should be displayed on the page.

I am open to any suggestions like javascript frameworks for example. I just need to figure the best solution to achieve this.

Thanks a lot for your time,
John
0
Comment
Question by:infodigger
2 Comments
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 400 total points
ID: 39871003
There's a few ways to do it, but probably the easiest it to just set a javascript timer in your page, to call your API at a set interval - say every 5 minutes (300000 milliseconds).

<script>
$(document).ready(function() {
   getInfo();
});

function getInfo() {
   $.getJSON( "yourAPI", function( data ) {
      $.each( data.records, function(key, info) {
         //do something with info
         alert(info.firstName);
      });

      //run again in 5 minutes - 1 sec = 1000 milliseconds
      setTimeout(getInfo,300000);
   });
}
</script>

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 39871153
If you do not need the data to be stored on your server, the jQuery (JavaScript) solution is probably the easiest and cleanest.  If you need to use the data in PHP, this shows how to access it.  You can read from the API using PHP file_get_contents() or cURL.
http://iconoun.com/demo/temp_infodigger.php

<?php // demo/temp_infodigger.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28368800.html
// PROCESSING JSON STRINGS IN PHP

// SIMULATED TEST DATA FROM THE POST AT EE (ACQUIRED FROM API)
$jso = <<<EOD
{
"records": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
EOD;

// MAKE AN OBJECT
$obj = json_decode($jso);

// ACTIVATE THIS TO SHOW THE DATA
// var_dump($obj);

// USE AN ITERATOR TO PROOCESS THE ARRAY OF OBJECTS
foreach ($obj->records as $person)
{
    echo "<br>$person->firstName $person->lastName" . PHP_EOL;
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now