如何使用循环从数据库中获取数据?

时间:2016-07-24 08:03:50

标签: arrays json ajax loops

在我的数据库中,我有不同的用户名:" fabio97"," antonino"," lauretta"。我想让他们从DB获得这样的数组:

$dati = array("str"=>array("n1"=>"fabio97", "n2"=>"antonino", "n3"=>"lauretta"));

是否正确写:

...
    "str" => array("n".$i=>"Ti sei incrociato con ".$array_db[username]),
...

进入循环

...
$i = 0;
$array_user = mysql_fetch_array($query);
while ($array_db = mysql_fetch_array ($query_db)) {
    ...
        if ($array_user[square] == $array_db[square]) {
            $dati = array(
                "data" => array(
                    'address_complete'=>$data->results[0]->formatted_address, 
                    'address_square'=>$data->results[0]->address_components[1]->long_name,
                    'location'=>$data->results[0]->address_components[2]->long_name,
                    'postal_code'=>$data->results[0]->address_components[7]->long_name,
                    'data_ora'=>$tmp_date
                ),
                "str" => array("n".$i=>"Ti sei incrociato con ".$array_db[username]),
                "index" => $i
            );
            $i++;
        }
}

并用Ajax调用它们:

... success:function(msg){
    if(msg){
       $("#location").html(Object.keys(msg.str).map(x => msg.str[x]).join(", "));
    }else{
        $("#location").html('Not Available');
    }

0 个答案:

没有答案