Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅在foreach循环后插入最后一行_Php_Jquery_Json - Fatal编程技术网

Php 仅在foreach循环后插入最后一行

Php 仅在foreach循环后插入最后一行,php,jquery,json,Php,Jquery,Json,这是我的info.php文件。在这个文件中,我从scammers表中获取steam\u id。问题是,当我在首页显示结果时,所有玩家的参数都是相同的,因为表骗子中的最后一行总是插入$steamids。为什么不一个接一个地插入?这导致了我的问题,我不知道如何解决这个问题。 $dbh = new PDO('mysql:host=localhost;dbname=csgoscam', 'root', ''); $sth = $dbh->prepare("SELECT * FROM scamme

这是我的
info.php
文件。在这个文件中,我从
scammers
表中获取
steam\u id
。问题是,当我在首页显示结果时,所有玩家的参数都是相同的,因为表
骗子
中的最后一行总是插入
$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);
       });
    });
});
用这个替换你的js(归功于@roland starke)

$(文档).ready(函数(){
$.getJSON(“/info.php”,函数(json){
$.each(json、函数(i、播放器){
//console.log(player.response.players[0].avatarfull);
$(“.userinfo”).append(“”);
$(“.streamprofile”)。追加(“”);
$(.personName”).append(player.response.players[0].personName);
});
});
});

@SearchAndResQ我已经更新了我的答案。你能做什么并查看输出是什么?如果它实际获得所有行,为什么在每次迭代中使用$r['steam\u id']?您不应该在foreach循环中使用$row吗?如果你真的想得到所有的身份证,从foreach中拿出来。@nisn我能得到所有的身份证吗steamids@omegasbk而不是如何在没有
$row
的情况下获取其余代码?我在我的问题中添加了一个屏幕截图,我不知道您的底层html是什么样子。似乎在该页面上有两个
.streamprofile
div或span,结果是所有内容的复制。在我看来,两个div中的每一个都有两个图像(一个是绿色的,一个不是绿色的)和两个链接(都有文本“STEAM nuoroda”)。但是我想我应该把他全部写在json所在的JS文件中,然后附加
profiles
div。但是如何从
{{$s->}}
中获取值呢?你的代码基本上是正常的。整个
在您发送的屏幕截图中是重复的,但每个截图都显示两个图像、两个用户名和两个链接,由javascript设置。因此,您的javascript获得了正确的信息。你只需要决定如何显示它。我正在尝试用javascript代码编写所有
代码。但是从那里很难得到变量
<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>  
$(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);
       });
    });
});