1

my jquery is

<script type="text/javascript">
// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); 
setInterval(function() {
$('#divToRefresh').load('get.php');
}, 100);  
});
// ]]></script>

the result of get.php is

{"dir":"down","sym":"oo","b":"1.35460","a":"1.35465"} {"dir":"down","sym":"pp","b":"1.64263","a":"1.64273"} {"dir":"down","sym":"qq","b":"104.498","a":"104.502"} {"dir":"up","sym":"rr","b":"0.88183","a":"0.88188"} {"dir":"down","sym":"ss","b":"1.09551","a":"1.09558"} 

i need to display like following

ONE : 1.35451 / 1.35454   |     TWO : 171.880 / 171.892   |     GBP / NZD : 1.97260 / 1.97316

any help..

2
  • Please add what code is generating output on get.php Commented Jan 21, 2014 at 5:40
  • 4
    You can format the desired output in get.php itself, Since you have used .load() Commented Jan 21, 2014 at 5:42

1 Answer 1

2

Instead of loading the data directly to 'divToRefresh' you can use a callback function to reformat the data. I am assuming that the data you are receiving is a PHP array so you will need to parse it to JSON format so you can conveniently access it via Javascript.

$('#mydiv').load('get.php',function(data) {
        var json = JSON.parse(data);
        var formatted_output = json.dir + '/' + json.sym;//do whatever formatting you want
        $('#mydiv').html(formatted_output);
    });

The result you are sending from the PHP file is not a valid JSON for a multi dimensions array. If you are sending it all at once it should be in the following format (or you will have problems parsing it):

[{"dir":"down","sym":"oo","b":"1.35460","a":"1.35465"},{"dir":"down","sym":"pp","b":"1.64263","a":"1.64273"},{"dir":"down","sym":"qq","b":"104.498","a":"104.502"},{"dir":"up","sym":"rr","b":"0.88183","a":"0.88188"},{"dir":"down","sym":"ss","b":"1.09551","a":"1.09558"}]

Basically every group of elements such as {element,element,element} need to be separated with a comma and the entire json should be wrapped with [] i.e [{element,element},{element,element},{element,element}]

You can then build a formatted text from the individual elements:

var formatted_output = json[0].dir + '/' + json[0].sym + ...

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Is this the result you receive ALL AT ONCE? or at different times, 1 row at a time? You can access them by 'json.dir', 'json.sim', etc.;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.