0

I want to process a Json array returned by php file. The PHP code is below.

$pdo = new PDO('');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * from table';
foreach ($pdo->query($sql) as $row) 
{
    $results[] = array('row1' =>$row['col1'],
    'row2' => $row['col2'],
    'row3' => $row['col3']);
}
header('Content-Type: application/json');
echo json_encode($results);

The above code will return Json array like this one

[
   {"row1":"test","row2":"5.123456","row3":"6.123456"},
   {"row1":"test1","row2":"6.123456","row3":"8.123654"},
   {"row1":"test3","row2":"6.321456","row3":"8.964512"}
]

I want to process the above Json array and present it inside a table. I try many Jquery codes and spent hours in front of computer. But i can't get correct answer. Please help me. Help me by either give correct Jquery code to process the Json array or give correct PHP code to send correct Json string. I forgot to say another info. When I try to debug using FF, I am getting an error in console

https://i.sstatic.net/tnLcZ.jpg

(image shows: SyntaxError: JSON.parse: unexpected end of data)

UPDATE

I update your script like this. The script return nothing. Please help

var html="<table border=1 style='border-collapse:collapse'>";
$(document).ready(function () {
$.ajax({
   url: "http://10.44.0.160/service.php",
   contentType: "application/json; charset=utf-8",
   dataType: "jsonp",
   type: "POST",
success: function(data) 
{
data.forEach(function(obj,index){html+="<tr><td>"+index+"<td>"+obj.csc_name+"</td><td>"+obj.lat+"</td><td>"+obj.longi+"</td></tr>";});
html+="</table>";
jQuery(html).appendTo("body");
}
            });
        });
3
  • try looking at ExtJS , it accepts the data as JSON , will display in whatever format you want Commented Oct 4, 2013 at 10:34
  • I try many Jquery codes and spent hours in front of computer. Could you please post some of your attempts? Commented Oct 4, 2013 at 10:35
  • All of them are from stackoverflow. Please see the link. I have an error in consol also Commented Oct 4, 2013 at 10:43

2 Answers 2

0

use each to loop throw your json data

<script type="text/javascript">
    $.each(yourData, function(key, val) {

           //your code here
           alert(key + '=' + val)

     });
 </script>
Sign up to request clarification or add additional context in comments.

1 Comment

I am totally new to JS. Please post full code. Also see the update (Image Link) on my post.
0
var html="<table border=1 style='border-collapse:collapse'>";

var json=[
 {"row1":"test","row2":"5.123456","row3":"6.123456"},
 {"row1":"test1","row2":"6.123456","row3":"8.123654"},
 {"row1":"test3","row2":"6.321456","row3":"8.964512"}
];
json.forEach(function(obj,index){html+="<tr><td>"+index+"<td>"+obj.row1+"</td>    <td>"+obj.row2+"</td><td>"+obj.row3+"</td></tr>";});
html+="</table>";
jQuery(html).appendTo("body");

Check out this fiddle http://jsfiddle.net/TdM8s/

2 Comments

Thanks friend, it's working. This is the code I need. Please post full code ie, ajax request to server + jquery parser (Your Code). Why I am getting error in FF console 'SyntaxError: JSON.parse: unexpected end of data'
It seems that you have trouble getting data from server try using console.log(data) before using it. Also check out the following thread stackoverflow.com/questions/9321510/…

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.