簡體   English   中英

PHP-用戶在排名頁面中的打印位置

[英]PHP - Print position of user in ranking page

我正在制作一個排名頁面,在其中我按降序顯示用戶名。 名稱已經按照正確的順序顯示,但是我在顯示用戶位置時遇到了麻煩,例如1º位置,2º位置...這是我的代碼(簡體):

我試圖做一個for循環(在我能想到的每個地方)並打印$ i的值,但它似乎不起作用,要么$ i在每個位置始終具有相同的值,要么在位置中都具有相同的值相同的位置。

<?php 
for($i=0;$i<8;$i++){
while($row = mysqli_fetch_assoc($result)) {
            $names =$row['username'];
            ?>
        <li>
            <a href="#">
                <div class="container">
                    <div class="image">
                        <svg></svg>
                    </div>
                    <div class="content">
                        <h2><?php print $i; // print the positions ?></h2>
                        <p><?php print $names ?></p>
                    </div>
                </div>
            </a>
        </li>

<?php }} ?>

如果您的SQL以正確的順序對用戶進行排序,則無需使用循環,而只需使用一個計數器並在每次(使用$i++ )時對其進行遞增即可。

<?php 
$i=1;
while($row = mysqli_fetch_assoc($result)) {
            $names =$row['username'];
            ?>
        <li>
            <a href="#">
                <div class="container">
                    <div class="image">
                        <svg></svg>
                    </div>
                    <div class="content">
                        <h2><?php print $i++; // print the positions ?></h2>
                        <p><?php print $names ?></p>
                    </div>
                </div>
            </a>
        </li>

<?php } ?>
<?php 
$counter=0;
while($row = mysqli_fetch_assoc($result)) {
            $names =$row['username'];
            $counter++;
            ?>
        <li>
            <a href="#">
                <div class="container">
                    <div class="image">
                        <svg></svg>
                    </div>
                    <div class="content">
                        <h2><?php echo $counter; ?></h2>
                        <p><?php print $names ?></p>
                    </div>
                </div>
            </a>
        </li>

<?php } ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM