Solved

Present data that change in PHP

Posted on 2014-02-19
2
489 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 109

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

815 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

9 Experts available now in Live!

Get 1:1 Help Now