1

I struggling print all my data from DB to webpage using JSON. But I not understand logical how it should work. My JSON script:

<script> 
$("document").ready(function() {
    $.getJSON("test1.php", function(data) {

        $("#div-my-table").text("<table>");

        $.each(data, function(i, item) {
            $("#div-my-table").append("<tr><td>" + item.code +"</td><td>" + item.name + "</td></tr>");
        });

        $("#div-my-table").append("</table>");

    });
});     
</script>

And test1.php file

<?php
require_once 'connection.php';

$sql = $conn -> prepare("SELECT * FROM DB_NAME");

$sql -> execute();

  while ($row = $sql -> fetch(PDO::FETCH_ASSOC)) 
  {
      $values = array('code'=>$row['code'],
                    'line'=>$row['line']);                  
}
echo json_encode($values);
?>

and part of HTML:

<body>

<table id="div-my-table">
</table>

</body>

And system return back only:

<table>
undefined undefined 
undefined undefined 
2
  • 1
    remove the line $("#div-my-table").text("<table>"); and $("#div-my-table").append("<table>"); Commented Dec 4, 2014 at 10:43
  • change $("document") to $(document) Commented Dec 4, 2014 at 10:45

4 Answers 4

2

First make below correction in your code

$values[] = array('code'=>$row['code'],'line'=>$row['line']);

Above change will append all database value to $value variable and will show all records instead of last record of db

Also Please check with your table name in below query

$sql = $conn -> prepare("SELECT * FROM DB_NAME");

It seems that you are taking db name constant here instead of table name as mentioned below.

$sql = $conn -> prepare("SELECT * FROM TABLE_NAME");

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

Comments

1

If you're expecting multiple rows, you need to gather the results properly. The $values gets overwritten every iteration.

while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
    // add another dimension
    $values[] = array(
        'code'=>$row['code'],
        'line'=>$row['line']
    );                  
}

echo json_encode($values);

Or for just one line:

echo json_encode($sql->fetchAll(PDO::FETCH_ASSOC));

So that they are properly nested.

Then on your JS:

<script type="text/javascript"> 
$(document).ready(function() {

    $.getJSON("test1.php", function(data) {

        var table_rows = '';

        $.each(data, function(i, item) {
            table_rows += "<tr><td>" + item.code +"</td><td>" + item.name + "</td></tr>");
        });

        $("#div-my-table").html(table_rows);

    });

});     
</script>

Comments

1

You're overwriting the same $values variable each time through the loop. At the end, it will just contain a single row, not an array of all the rows. You need to add each row to $values, not replace it. It should be:

$values = array();
while ($row = $sql -> fetch(PDO::FETCH_ASSOC)) 
{
    $values[] = $row;
}
echo json_encode($values);

Also, your jQuery is wrong. You shouldn't use .text() when you're creating HTML, you should use .html(). And you don't need to append </table> -- these functions operate on the DOM, not the HTML, and the DOM always has complete elements.

$("document").ready(function() {
    $.getJSON("test1.php", function(data) {

        var table = $("<table>");
        $("#div-my-table").empty().append(table);

        $.each(data, function(i, item) {
            table.append("<tr><td>" + item.code +"</td><td>" + item.name + "</td></tr>");
        });

    });
});     

Comments

0

Use last.after();

Instead of append();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.