Json_encode in While Loop

Refresh

March 2019

Views

58 time

1

I need to get data from the database with ajax and put that data in the 'select' tag. I need to have every name in a different 'option'... View the code:

Index.php:

<label>Select:</label>
<select id="users"></select>

JS:

$(document).ready(function() {
setInterval(function() {
    $.get("frombase.php", function(data) {
        data = JSON.parse(data);
        for (var id in data) {
            $("#users").empty();
            $("#users").append("<option value='"+ id +"'>"+ data[id] +"</option>")
        }
    });
}, 1000);

});

And frombase.php:

$sql = "SELECT * FROM `users`";
$result = mysqli_query($db, $sql);

$name = array();

while ($row = mysqli_fetch_assoc($result)) {
$name[] = $row['name'];
}
echo json_encode(array("name" => $name));

mysqli_close($db);

Look at the result (I do not need this)

enter image description here

(My english is not good, because I use Google Translate)

3 answers

2

I would do in this way...

JS:

        $(document).ready(function() {
            $.ajax({
                url :'frombase.php',
                type: "POST",
                dataType: "json",
                success : function(data){
                    $("#users").empty();
                    $(data['options']).each(function(k,v){
                        $("#users").append("<option value='"+ v['id'] +"'>"+ v['name'] +"</option>");
                    });
                },
                error:function(){
                    alert('Error of server comunication');
                }
            });
        });

PHP:

    $db = 'YOUR CONNECTION';
    $query = $db->prepare("SELECT id,name FROM users");
    $query->execute();        
    $query->bind_result($id,$name);

    while ($query->fetch()) {
        $result[] = array('id'=>$id,'name'=>$name);
    }
    $root['options'] = $result;
    $root = json_encode($root);     
    $db->close();
    echo $root; 
2

Я хотел бы сделать таким образом ...

JS:

        $(document).ready(function() {
            $.ajax({
                url :'frombase.php',
                type: "POST",
                dataType: "json",
                success : function(data){
                    $("#users").empty();
                    $(data['options']).each(function(k,v){
                        $("#users").append("<option value='"+ v['id'] +"'>"+ v['name'] +"</option>");
                    });
                },
                error:function(){
                    alert('Error of server comunication');
                }
            });
        });

PHP:

    $db = 'YOUR CONNECTION';
    $query = $db->prepare("SELECT id,name FROM users");
    $query->execute();        
    $query->bind_result($id,$name);

    while ($query->fetch()) {
        $result[] = array('id'=>$id,'name'=>$name);
    }
    $root['options'] = $result;
    $root = json_encode($root);     
    $db->close();
    echo $root; 
2

Я хотел бы сделать таким образом ...

JS:

        $(document).ready(function() {
            $.ajax({
                url :'frombase.php',
                type: "POST",
                dataType: "json",
                success : function(data){
                    $("#users").empty();
                    $(data['options']).each(function(k,v){
                        $("#users").append("<option value='"+ v['id'] +"'>"+ v['name'] +"</option>");
                    });
                },
                error:function(){
                    alert('Error of server comunication');
                }
            });
        });

PHP:

    $db = 'YOUR CONNECTION';
    $query = $db->prepare("SELECT id,name FROM users");
    $query->execute();        
    $query->bind_result($id,$name);

    while ($query->fetch()) {
        $result[] = array('id'=>$id,'name'=>$name);
    }
    $root['options'] = $result;
    $root = json_encode($root);     
    $db->close();
    echo $root;