-1

I have code which return loop from php to ajax via json_encode()

Let me know what I want to do. there is one table call SMTP. Assume that it has 3 value and I want to fetch that 3 value from table, store in array () and Display it to HTML via AJAX in table format.

So I'm confused where I place my loop, in AJAX or PHP ? Here is my code.

PHP

$result=mysql_query("select * from ".$db.".smtp WHERE id = '$user' ");
if($result === FALSE) {
    die(mysql_error());
}
while($data = mysql_fetch_row($result))
{   
    $array = array($data[2],$data[3]);
}
echo json_encode($array);

JS

    $(document).ready(function() {
    GolbalURL = $.session.get('URL');
    $.ajax({
        type: "GET",
        url: GolbalURL+"smtp.php",             
        dataType: "html",      
        success: function(response){                    
                $("#divsmtp").html(response); 
        }
    });
});

HTML

<div id = "divsmtp"></div>

This code return only last value. inarray like ["data2","data3"]

My Longest way to do

success: function(response){    
        resultObj = eval (response);
        var i = Object.keys(resultObj).length;
        i /=2;
        //$("#divsmtp").html(i);
        var content = "<table>"
        for(j=0; j<i; j++){
            k = j+1;
            if (j % 2 === 0)
            {
                alert("j="+j+" k="+k );
                content += '<tr><td>' + resultObj[j] +  resultObj[k] + '</td></tr>';
            }
            else
            {
                k = k+1;
                var m = j+1;
                alert("m="+m+" k="+k );
                content += '<tr><td>' + resultObj[m] +  resultObj[k] + '</td></tr>';                    
            }
        }
        content += "</table>"

        $('#divsmtp').append(content);
    }
Shivam Pandya
  • 1,019
  • 3
  • 27
  • 62

3 Answers3

3

Because you are always overwrite the $array variable with an array. Use: $array[] = array($data[2], $data[3]);

vaso123
  • 12,223
  • 4
  • 32
  • 63
  • Will want to create the array *somewhere*, though. – T.J. Crowder Oct 14 '14 at 12:39
  • Yes, It is possible to do If I place my whole table HTML in PHP file's loop, but I do not want to do that – Shivam Pandya Oct 14 '14 at 12:40
  • So any other method to do that ? – Shivam Pandya Oct 14 '14 at 12:40
  • I do not understand your question. You have several ways. Use associative array, and then json_encode that, and then you can loop through your array in success function. Or, you can build your output in the PHP, and then just set the html of #divsmtp in the ajax success. – vaso123 Oct 14 '14 at 12:43
2

Use json decoding at jquery end

EDIT Small way

        $.each($.parseJSON(response), function( index, value ) {
            //Loop  using key value LIKE:  index => value
        });

//Old

    success: function(response){    
        var jsonDecoded = $.parseJSON(response);            
        console.log(jsonDecoded );   
            $.each(jsonDecoded, function( index, value ) {
                //Loop  using key value LIKE:  index => value
            });


       $("#divsmtp").html(response); 
    }
Pratik
  • 11,183
  • 7
  • 34
  • 72
1

Create the array on the PHP side like this -

$array = array();
while($data = mysql_fetch_row($result))
{   
    array_push($array, $data);
}
echo json_encode($array);
Jay Blanchard
  • 33,530
  • 16
  • 73
  • 113