-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,061
  • 3
  • 29
  • 64
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 14 '14 at 12:36
  • 2
    @ShivamPandya — And yet you are doing it anyway… – Quentin Oct 14 '14 at 12:38
  • 1
    @Quentin its an old project and my company do not want to upgrad. What I do ?? :( I also want to change it to PDO. but its not in my hand – Shivam Pandya Oct 14 '14 at 12:38
  • @ShivamPandya You should tell your company!!! dont use these outdated code understand – Mohammad Sadiq Shaikh Oct 14 '14 at 12:54
  • @sadiqxs Ya, I allready do that. I'll use PDO from next project :) (y) – Shivam Pandya Oct 14 '14 at 12:55
  • @ShivamPandya which company you work? – Mohammad Sadiq Shaikh Oct 14 '14 at 12:59
  • @sadiqxs Sorry I can't say.:| – Shivam Pandya Oct 14 '14 at 13:07
  • @ShivamPandya , did you check my answer? – Pratik Joshi Oct 14 '14 at 13:07
  • @jQueryAngryBird I am currently work with Jay's answer. I'll also try your once and after I vote. :) And how can I get Index ? I only pass value – Shivam Pandya Oct 14 '14 at 13:09
  • @ShivamPandya 1)sure , you will need it at jQuery end while Decoding hson value , that u encoded in php. 2)You get json encoded data in array form so to loop thru it , we use `$.each` , you automatically get index=> value pair , in it. – Pratik Joshi Oct 14 '14 at 13:14
  • @jQueryAngryBird +1 for your help :) – Shivam Pandya Oct 14 '14 at 13:24

3 Answers3

3

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

vaso123
  • 12,347
  • 4
  • 34
  • 64
  • 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 Joshi
  • 11,485
  • 7
  • 41
  • 73
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
  • 34,243
  • 16
  • 77
  • 119