Solved

Present data that change in PHP

Posted on 2014-02-19
2
476 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 42

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

14 Experts available now in Live!

Get 1:1 Help Now