PHP - Print position of user in ranking page
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
add a comment |
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
You are executing thewhile
8 times. That's why$i
is always0
during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thusmysqli_fetch_assoc
always returnsfalse
).
– Gabriel
Dec 30 '18 at 14:40
add a comment |
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
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
php html
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 thewhile
8 times. That's why$i
is always0
during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thusmysqli_fetch_assoc
always returnsfalse
).
– Gabriel
Dec 30 '18 at 14:40
add a comment |
You are executing thewhile
8 times. That's why$i
is always0
during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thusmysqli_fetch_assoc
always returnsfalse
).
– 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
add a comment |
2 Answers
2
active
oldest
votes
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 } ?>
add a comment |
<?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 } ?>
1
Please tell why this anwers the question. Read How to answer.
– Matthijs
Dec 30 '18 at 14:51
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 } ?>
add a comment |
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 } ?>
add a comment |
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 } ?>
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 } ?>
answered Dec 30 '18 at 14:37
Nigel RenNigel Ren
26.6k61833
26.6k61833
add a comment |
add a comment |
<?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 } ?>
1
Please tell why this anwers the question. Read How to answer.
– Matthijs
Dec 30 '18 at 14:51
add a comment |
<?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 } ?>
1
Please tell why this anwers the question. Read How to answer.
– Matthijs
Dec 30 '18 at 14:51
add a comment |
<?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 } ?>
<?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 } ?>
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You are executing the
while
8 times. That's why$i
is always0
during the first loop. You don't see the output from the following iterations because the mysql resulset is already at its end (thusmysqli_fetch_assoc
always returnsfalse
).– Gabriel
Dec 30 '18 at 14:40