只有最后一行在foreach循环后插入

时间:2016-02-25 13:30:44

标签: php jquery json

这是我的info.php文件。在这个文件中,我从steam_id表中获取scammers。问题是当我在首页显示结果时,所有玩家参数都是相同的,因为表scammers中的相同最后一行总是插入$steamids。为什么不一个一个地插入?这导致我的问题,我不知道如何解决这个问题    

$dbh = new PDO('mysql:host=localhost;dbname=csgoscam', 'root', '');

$sth = $dbh->prepare("SELECT * FROM scammers");
$sth->execute();
$row = $sth->fetchAll();
$data = [];

foreach($row as $r){

    $steamids = $r['steam_id'];

    $APIKEY = '***';
    $steamAPI = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids=$steamids&key=$APIKEY&format=json";
    $json_object= file_get_contents($steamAPI);
    $data[] = json_decode($json_object);

}
echo json_encode($data);

我的app.js

$(document).ready(function(){

    $.getJSON("/info.php", function(json){
        $.each(json, function(i, player){
            //console.log(player.response.players[0].avatarfull);
       $(".userinfo").html('<img style="width: 100px; height: 100px" style="width: 100px; height: 100px" src="'+player.response.players[0].avatarfull+'">');
       $(".steamProfile").html('<a href="'+player.response.players[0].profileurl+'">STEAM nuoroda</a>');
       $(".personaname").html(player.response.players[0].personaname);
       });
    });
});

info.php

info.php file

更新这是附加

的样子

append image

Html

<div class="profiles col-md-4">

<div class="userinfo"></div>
<p>Slapyvardis: <strong><span class="personaname"></span></strong></p>
<p>Vardas: <strong>{{$s->fname}} {{$s->lname}}</strong></p>
@if($s->facebook == "")
<p>Facebook: <strong>Nėra</strong></p>
@else
<p>Facebook: <strong>{{$s->facebook}}</strong></p>
@endif
<p>Skype ID: <strong>{{$s->skype_id}}</strong></p>
<p>Steam ID: <strong><span id="steamID">{{$s->steam_id}}</span></strong></p>-->
<p>Steam URL: <strong><span class="steamProfile"></span></strong></p>

<p style="font-family: 'Roboto', sans-serif;"><button id="reason" data-toggle="modal" data-target="#reasonText{{$s->id}}" class="btn btn-danger"><i class="glyphicon glyphicon-film"></i> Rodyti priežastį</button>

<button data-toggle="modal" data-target="#proofText{{$s->id}}" id="proof" class="btn btn-success">
<i class="glyphicon glyphicon-camera"></i> <strong>Įrodymai</strong></button>

<form id="voteUp" action="/minus-rep/{{$s->id}}" method="get">
<button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-thumbs-down"></i> <strong>Nerekomenduoti</strong></button>
</form>

</p>
<p>Nerekomenduoja: <strong><span id="votes" class="label label-success">{{$s->points}}</span></strong>

@if($s->points < 2 && $s->points > 0) 
    narys
@endif
@if($s->points > 1 && $s->points < 10)
    nariai
@endif
@if($s->points > 10)
    narių
@endif  
</p>

</div>  

1 个答案:

答案 0 :(得分:0)

用这个替换你的js(归功于@ roland-starke)

$(document).ready(function(){

    $.getJSON("/info.php", function(json){
        $.each(json, function(i, player){
            //console.log(player.response.players[0].avatarfull);
       $(".userinfo").append('<img style="width: 100px; height: 100px" style="width: 100px; height: 100px" src="'+player.response.players[0].avatarfull+'">');
       $(".steamProfile").append('<a href="'+player.response.players[0].profileurl+'">STEAM nuoroda</a>');
       $(".personaname").append(player.response.players[0].personaname);
       });
    });
});