如何使用PHP语言显示HTML列中的最高值

时间:2018-09-15 17:47:48

标签: php html mysql database list

    <div class="col-sm-5 col-md-12 col-lg-6">
      <div class="panel panel-inverse">
        <div class="panel-heading">
          <h4 class="panel-title">TOP 5 BUNNY KILLERS</h4>
        </div>
        <div class="panel-body">
          <ul class="media-list user-list">
           <?php echo $output ?>
            <?php echo $output ?>
             <?php echo $output ?>
              <?php echo $output ?>
               <?php echo $output ?>
          </ul>
        </div>
      </div>
    </div>

$sql = "SELECT Username, BunnyKilled FROM users order by BunnyKilled DESC LIMIT 5;";
$con=mysqli_connect("yes","yes","yes","yes");
$results =  array();
$result = mysqli_query($con,$sql);
$results = mysqli_fetch_all($result, MYSQLI_ASSOC);

$output = '';

foreach ($results as $result) {
$output = '  <li class="media"> <div class="media-left"> <a href="#">  <img class="media-object img-circle" src="../images/photos/user9.png" alt=""> </a> </div> <div class="media-body"> <h4 class="media-heading"><a href="">'. $result['Username'] . '</a></h4> <span>' . $result['BunnyKilled'] . '</span> bunny killed</div> </li>' ;
    }

我也在使用

if(isset($_SESSION['username']))

用于显示兔子杀手的个人信息,但我想显示排名前5的兔子杀手,以按照被杀兔子的数量排序。在什么都没用之后,我把var dump放了,这就是我得到的

array(5) {
  [0]=>
  array(2) {
    ["Username"]=>
    string(19) "Abdulhamit_Yildirim"
    ["BunnyKilled"]=>
    string(2) "15"
  }
  [1]=>
  array(2) {
    ["Username"]=>
    string(4) "Ampo"
    ["BunnyKilled"]=>
    string(1) "5"
  }
  [2]=>
  array(2) {
    ["Username"]=>
    string(15) "Timothy_Bronson"
    ["BunnyKilled"]=>
    string(1) "5"
  }
  [3]=>
  array(2) {
    ["Username"]=>
    string(6) "Rvmses"
    ["BunnyKilled"]=>
    string(1) "4"
  }
  [4]=>
  array(2) {
    ["Username"]=>
    string(3) "Esy"
    ["BunnyKilled"]=>
    string(1) "3"
  }
}

只是更新后的var_dump结果,我将其放在获取后

1 个答案:

答案 0 :(得分:0)

您的MySQL查询应类似于:

选择*来自BunnyKilled DESC LIMIT 5的looneytunes顺序;

前5名按顺序排列,上限为5。

您的php应该看起来像这样:

[ {\"to\": [{\"email\": \"example1@gmail.com \"}]} , {\"to\": [{\"email\": \"example2@gmail.com\"}]} ]