[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Present data that change in PHP

Posted on 2014-02-19
2
Medium Priority
?
528 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 44

Accepted Solution

by:
Chris Stanyon earned 1600 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

649 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