duanbinmi8970 2017-10-30 20:58
浏览 142
已采纳

PHP循环输出重复

public function select(){
    $rows = [];
    $connection = $this->connect();
    $result = $connection->query("SELECT username FROM users");
    while ($row = $result->fetch_assoc()){
        $rows[] = $row;
    }

    $userlist = 0;
    foreach($rows as $username){
        $userlist .= $username['username'];
    }

    $get_rankings = [1,2,3,4];
    $get_image_path = "images/";
    $total = 0;

    for ($x = 0; $x < count($get_rankings); $x++){
        $total = $get_rankings[$x];
        $path .=  "<img src = '" . $get_image_path . $total . ".png'>
" . $userlist . "<br/>";
        // echo "<span  class = 'align-down'>{$path}";
        // echo "<p class = 'user-name'> {$rows['0']}</p>";
        // echo "</span>";
    }

    echo $path;
}

I'm trying to output a simple ranking but using the number index as images to display them.

In the past i've tried to do something similar but couldn't figure out how to match player it with images on the side.

The output im getting is this:

enter image description here

It's outputting each entry 4 times(I get why, its in a loop) but I can't figure out the correct solution to write it outside of a loop or properly

The desired output is:

enter image description here

My DataBase reads as:

[id][username][password]

If there is an easier solution, i'm all ears. I don't know how to approach this.

  • 写回答

1条回答 默认 最新

  • dongshao8125 2017-10-30 21:11
    关注

    There's no need for $userlist. Output the username from $rows[$x].

    $path = "";
    $max = min(count($rows), count($get_rankings));
    for ($x = 0; $x < $max; $x++){
        $total = $get_rankings[$x];
        $path .=  "<img src = '" . $get_image_path . $total . ".png'>
    " . $rows[$x]['username'] . "<br/>";
        // echo "<span  class = 'align-down'>{$path}";
        // echo "<p class = 'user-name'> {$rows['0']}</p>";
        // echo "</span>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入