PHP foreach fetch只显示1个值数组

时间:2018-03-19 18:00:07

标签: php html mysql foreach

如何将fetch设置为每个用户只显示一个值?

现在,我的fetch显示了所有行,但我想只预测1行

Ex:爆头排:foreach爆头排 死亡排:Foreach死亡排

目前我的代码显示每一行,我不能将它们放入html

PHP。

<?php  
    include_once 'config.php';  

    $top_users = [];
    $columns = ['Humanity', 'Headshots', 'Murders', 'BanditsKilled', 'ZombiesKilled', 'pAliveTime'];
    foreach ($columns as $column) {
        $query = $config->prepare("SELECT UserID, UserName, $column as num FROM users ORDER BY $column DESC LIMIT 1");
        if($query->execute()) {
            $top_users[$column] = $query->fetch();
        }
    }

    ?>

HTML

      <div class="colw_3 spec-l border-right">
          <p></p><p><strong><em><font color="white">Humanity</font></em></strong><br>
          </p>
          <p><strong><em><font color="white">Headshots:</font></em></strong><br>
          </p>
          <p><strong><em><font color="white">Bandits Killed:</font></em></strong><br>
          </p>
          <p><strong><em><font color="white">Murders</font></em></strong><br>
          </p>
          <p><strong><em><font color="white">Zombies Killed</font></em></strong><br>
          </p>
          <p><em><strong><font color="white">Alive Time:</font></strong></em><br>
          </p>
        </div>
        <!-- END col_6 -->

        <div class="colw_3 paddbott100 spec-r">
          <p></p><p><strong><font color="white"> <?php foreach ($top_users as $column => $data) {
    echo $data['UserName'] . '&nbsp;' . $data['num'] ;
}?></font></strong><br>
          </p>
          <p><strong><font color="white"> <?php echo $query_result["Headshots"]; ?></font></strong><br>
          </p>
          <p><strong><font color="white"> <?php echo $query_result["BanditsKilled"]; ?></font></strong><br>
          </p>
          <p><strong><font color="white"> <?php echo $query_result["Murders"]; ?></font></strong><br>
          </p>
          <p><strong><font color="white"> <?php echo $query_result["ZombiesKilled"]; ?></font></strong><br>
          </p>
          <p><strong><font color="white"> <?php echo $query_result["pAliveTime"]; ?></font></strong><br>
          </p>
        </div>

而不是?php echo $query_result["text"];我想添加foreach,但它会显示所有行。

我想添加

foreach ($top_users as $column => $data) 
{{ $row->Humanity }}

但我不知道如何输入它。

我试图这样做:

<?php  
    include_once 'config.php';  

$top_users = [];
$Humanity = ['Humanity'];
$Headshots = ['Headshots'];
$Murders = ['Murders'];
$BanditsKilled = ['BanditsKilled'];
$ZombiesKilled = ['ZombiesKilled'];
$pAliveTime = ['pAliveTime'];
foreach ($Humanity as $Humanity) {
    $query = $config->prepare("SELECT UserName, $Humanity as num FROM users ORDER BY $Humanity DESC LIMIT 1");
    if($query->execute()) {
        $top_users[$Humanity] = $query->fetch();
    }
        $query = $config->prepare("SELECT UserName, $Headshots as num FROM users ORDER BY $Headshots DESC LIMIT 1");
    if($query->execute()) {
        $top_users[$Headshots] = $query->fetch();
    }


}

?>

HTML

 <div class="colw_3 paddbott100 spec-r">
          <p></p><p><strong><font color="white"> <?php foreach ($top_users as $Humanity => $data) {
    echo $data['UserName'] . '&nbsp;' . $data['num'] . '<br>' ;
}?></font></strong><br>
          </p>
          <p><strong><font color="white"> <?php foreach ($top_users as $Headshots => $data) {
    echo $data['UserName'] . '&nbsp;' . $data['num'] . '<br>' ;
}?></font></strong><br>

0 个答案:

没有答案