Friends, the situation is as follows:

With AJAX, I make a request to upload data from a database.

var obj; $.ajax({ type: "POST", url: "/functions/dbfunctions.php", data: { type: 7, id: id, } }).done(function(msg){ obj = jQuery.parseJSON(msg); }); 

Here is php

 $result = mysql_query("SELECT * FROM multiplys WHERE sale = '$action'"); $data_multiplys = mysql_fetch_array($result); echo json_encode($data_multiplys); 

Since not one line is output from the database, but not a certain number, since I don’t know how many records it will find, as a result, I don’t know how to create a cycle on the processing side that could go through all the records that will be in the resulting line. Please help create a loop with which I can parse the json returned to me with arrays

EDITIONS

Here is the complete request code:

 $.ajax({ type: "POST", url: "/functions/dbfunctions.php", data: { type: 7, id: id, } 

}). done (function (msg) {

 ЗДЕСЬ НУЖНО ЦИКЛОМ ОБРАБОТАТЬ JSON 

}

});

PHP itself is in the current state

 //load Multiply data if($_POST['type']==7) { $action = $_POST['id']; $result = mysql_query("SELECT * FROM multiplys WHERE sale = '$action'"); // ЗДЕСЬ МОЖЕТ БЫТЬ 10 записей $data_multiplys = mysql_fetch_array($result); echo json_encode($data_multiplys); } 

    2 answers 2

    Here is an example of iterating over a two-dimensional JSON array in a for loop

     var json_string = [ {"name":"Will","age":"12"}, {"name":"John","age":"29"} ]; for(var i in json_string) { alert(json_string[i].name + ', ' + json_string[i].age); } 

    • Unfortunately did not work. In my question I edited the code so that it would be more clearly understandable what I have. I tried your version but for some reason it did not work. The cycle has started to go without interruption. - WhoIsDT
    • In this case, your answer is more consistent with the question, so I mark it as correct. Thank you - WhoIsDT

    Add a dataType: "JSON" to the request and you don’t even have to parse anything:

     $.ajax({ type: "POST", url: "/functions/dbfunctions.php", dataType: "JSON", success: function(data) { for(var i=0;i<data.length;i++){ console.log(data[i]); } } });