Solved

Present data that change in PHP

Posted on 2014-02-19
2
502 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

737 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