0

Why this json encode format is not working .If I write it outside of the all brackets.Then it works,but get only one row.Plz help

$sql=mysqli_query($database,"SELECT * FROM com  ORDER by time DESC");
while($row=mysqli_fetch_array($sql)){
        $mid=$row['id'];
        $text=$row['text'];

        if($text) {
            $response=array();

            $response['msg']=$text;
            $response['id']=$id;
            echo json_encode($response);
        }
}
    //$response=array();

    // $response['msg']=$text;
    // $response['id']=$mid;
    // echo json_encode($response);

Jquery for getting above results.

function com(id){
    $.post('load.php', {tocom:id},function(data) {
        var json = eval('(' + data + ')');
        if(json['msg'] != "") {
            var msg=json['msg'];
            var fro=json['id'];
            $("#msg).html(fro+msg);
        }
      });
}
suresh gopal
  • 3,138
  • 6
  • 27
  • 58
Aana Saeed
  • 299
  • 1
  • 4
  • 14
  • You can tell something is wrong by looking at the syntax highlighting + messy indenting, whitespace!, `eval`? ... – elclanrs Dec 01 '12 at 07:48

2 Answers2

3

You are not giving one json response, but as much response as you have iterations in your while.

The good syntax should be :

PHP :

$sql=mysqli_query($database,"SELECT * FROM com  ORDER by time DESC");
$final_response = array();
while($row=mysqli_fetch_array($sql)){
    $mid=$row['id'];
    $text=$row['text'];

    if($text) {
        $response=array();
        $response['msg']=$text;
        $response['id']=$id;
        $final_response[] = $response;
    }

}
echo json_encode($final_response);

JS :

function com(id){

    $.post('load.php', {tocom:id},function(data) {
        $("#msg").html('');
        var json = eval('(' + data + ')');
        $.each(json, function(i, row) {
           if(row['msg'] != "") {
               var msg=row['msg'];
               var fro=row['fro'];
               $("#msg").append(fro+msg);
           }
        });
    });

}
Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
  • Yep, that is what I explained in my answer :-) – arkascha Dec 01 '12 at 07:55
  • 1
    Yes, I seen that but my answer took a bit more time to write so was slower :-) +1 anyway. – Alain Tiemblo Dec 01 '12 at 07:56
  • @Ninsuo Thank you.One more help plz.What would be the short version of this code.Like writting mysql results directly to json(etc) or using mysql assco instead of giving so many queries?Purpose is to make it short and use less queries....a smart code? – Aana Saeed Dec 01 '12 at 08:10
  • There is only one query here. If you don't want all results, you should add a `WHERE` clause to your request : something like `SELECT * FROM com WHERE column = 'value' ORDER by time DESC` (where column is a column from your com table and value the value you're looking for). – Alain Tiemblo Dec 01 '12 at 08:14
  • @Ninsuo ,Plz also review this.http://stackoverflow.com/questions/13761008/is-this-a-true-long-polling#comment18914635_13761008 – Aana Saeed Dec 07 '12 at 10:54
2

You are posting the json right after you read the first entry from the mysql result. That produces a json construct which terminates your reply for the querying client side.

Instead you have to collect all rows into an array inside php first and THEN convert the whole array into a json structure.

arkascha
  • 41,620
  • 7
  • 58
  • 90