PHP - Print position of user in ranking page












0















I am making a ranking page where I am displaying the username by descending order. The names already appear in the correct order but I am having trouble showing the position on the user, like 1º place, 2º place...
This is my code (simplified):



I tried to do a for loop (in every place I could think of) and print the value of $i, but it doesn't seem to work, either $i has always the same value in every position or all of them in the same position.



<?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 }} ?>









share|improve this question

























  • You are executing the while 8 times. That's why $i is always 0 during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thus mysqli_fetch_assoc always returns false).

    – Gabriel
    Dec 30 '18 at 14:40


















0















I am making a ranking page where I am displaying the username by descending order. The names already appear in the correct order but I am having trouble showing the position on the user, like 1º place, 2º place...
This is my code (simplified):



I tried to do a for loop (in every place I could think of) and print the value of $i, but it doesn't seem to work, either $i has always the same value in every position or all of them in the same position.



<?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 }} ?>









share|improve this question

























  • You are executing the while 8 times. That's why $i is always 0 during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thus mysqli_fetch_assoc always returns false).

    – Gabriel
    Dec 30 '18 at 14:40
















0












0








0








I am making a ranking page where I am displaying the username by descending order. The names already appear in the correct order but I am having trouble showing the position on the user, like 1º place, 2º place...
This is my code (simplified):



I tried to do a for loop (in every place I could think of) and print the value of $i, but it doesn't seem to work, either $i has always the same value in every position or all of them in the same position.



<?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 }} ?>









share|improve this question
















I am making a ranking page where I am displaying the username by descending order. The names already appear in the correct order but I am having trouble showing the position on the user, like 1º place, 2º place...
This is my code (simplified):



I tried to do a for loop (in every place I could think of) and print the value of $i, but it doesn't seem to work, either $i has always the same value in every position or all of them in the same position.



<?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 }} ?>






php html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 15:08









Nigel Ren

26.6k61833




26.6k61833










asked Dec 30 '18 at 14:34









KateKate

323




323













  • You are executing the while 8 times. That's why $i is always 0 during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thus mysqli_fetch_assoc always returns false).

    – Gabriel
    Dec 30 '18 at 14:40





















  • You are executing the while 8 times. That's why $i is always 0 during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thus mysqli_fetch_assoc always returns false).

    – Gabriel
    Dec 30 '18 at 14:40



















You are executing the while 8 times. That's why $i is always 0 during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thus mysqli_fetch_assoc always returns false).

– Gabriel
Dec 30 '18 at 14:40







You are executing the while 8 times. That's why $i is always 0 during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thus mysqli_fetch_assoc always returns false).

– Gabriel
Dec 30 '18 at 14:40














2 Answers
2






active

oldest

votes


















1














If your SQL orders the users in the correct order, then instead of having a loop, just have a counter and increment it each time (using $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 } ?>





share|improve this answer































    0














    <?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 } ?>





    share|improve this answer





















    • 1





      Please tell why this anwers the question. Read How to answer.

      – Matthijs
      Dec 30 '18 at 14:51











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978488%2fphp-print-position-of-user-in-ranking-page%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If your SQL orders the users in the correct order, then instead of having a loop, just have a counter and increment it each time (using $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 } ?>





    share|improve this answer




























      1














      If your SQL orders the users in the correct order, then instead of having a loop, just have a counter and increment it each time (using $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 } ?>





      share|improve this answer


























        1












        1








        1







        If your SQL orders the users in the correct order, then instead of having a loop, just have a counter and increment it each time (using $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 } ?>





        share|improve this answer













        If your SQL orders the users in the correct order, then instead of having a loop, just have a counter and increment it each time (using $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 } ?>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 14:37









        Nigel RenNigel Ren

        26.6k61833




        26.6k61833

























            0














            <?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 } ?>





            share|improve this answer





















            • 1





              Please tell why this anwers the question. Read How to answer.

              – Matthijs
              Dec 30 '18 at 14:51
















            0














            <?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 } ?>





            share|improve this answer





















            • 1





              Please tell why this anwers the question. Read How to answer.

              – Matthijs
              Dec 30 '18 at 14:51














            0












            0








            0







            <?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 } ?>





            share|improve this answer















            <?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 } ?>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 30 '18 at 14:47

























            answered Dec 30 '18 at 14:45









            Ali HaiderAli Haider

            12




            12








            • 1





              Please tell why this anwers the question. Read How to answer.

              – Matthijs
              Dec 30 '18 at 14:51














            • 1





              Please tell why this anwers the question. Read How to answer.

              – Matthijs
              Dec 30 '18 at 14:51








            1




            1





            Please tell why this anwers the question. Read How to answer.

            – Matthijs
            Dec 30 '18 at 14:51





            Please tell why this anwers the question. Read How to answer.

            – Matthijs
            Dec 30 '18 at 14:51


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978488%2fphp-print-position-of-user-in-ranking-page%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas

            Can't read property showImagePicker of undefined in react native iOS