OneEyedWillie Posted June 29, 2011 Share Posted June 29, 2011 Well, I'm pretty sure I'm close here. I have a ranking system for a game that me and my friend are designing. What I've done here is get the data from the table and order it by score with the highest score at the top. This way I can automatically create a ranking based on the score. In the code there is a line that says: echo ''; there is where I need the number to be. Starting at 1 and going until there are no more rows to get. I tried using a while loop, but I had no luck. Hopefully someone here can help! Here's the code: <?php $query = "select * from gamedata ORDER BY gamedata . score DESC"; $result = mysql_query($query); $rank = mysql_num_rows($result); ?> </header> <h2>Rankings</h2> <div> <table width="100%"> <tr> <th align="left">Rank</th> <th align="left">Username</th> <th align="left">Score</th> </tr> <?php while($row = mysql_fetch_array($result)) { echo '<tr>'; echo '<td>'; echo ''; echo '</td>'; echo '<td>'; echo '<form name=view method=get action=viewprofile.php>'; echo "<input type=hidden name=username value="; echo $row['username']; echo "/>"; echo "<a href=viewprofile.php?username="; echo $row['username']; echo ">"; echo $row['username']; echo "</a></td></form>"; echo '<td>'; echo $row['score']; echo '</td>'; echo '</tr>'; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/240670-automatically-increase-number-in-table/ Share on other sites More sharing options...
Alex Posted June 29, 2011 Share Posted June 29, 2011 Create a variable initialized at 1, then increment it every loop. Something like this: <?php $i = 1; while($row = mysql_fetch_array($result)) { echo '<tr>'; echo '<td>'; echo $i; echo '</td>'; echo '<td>'; echo '<form name=view method=get action=viewprofile.php>'; echo "<input type=hidden name=username value="; echo $row['username']; echo "/>"; echo "<a href=viewprofile.php?username="; echo $row['username']; echo ">"; echo $row['username']; echo "</a></td></form>"; echo '<td>'; echo $row['score']; echo '</td>'; echo '</tr>'; ++$i; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240670-automatically-increase-number-in-table/#findComment-1236118 Share on other sites More sharing options...
jcbones Posted June 29, 2011 Share Posted June 29, 2011 <?php $query = "select * from gamedata ORDER BY gamedata . score DESC"; $result = mysql_query($query); $rank = mysql_num_rows($result); $i = 0; //set $i as 0; ?> </header> <h2>Rankings</h2> <div> <table width="100%"> <tr> <th align="left">Rank</th> <th align="left">Username</th> <th align="left">Score</th> </tr> <?php while($row = mysql_fetch_array($result)) { echo '<tr>'; echo '<td>'; echo ++$i; //increment $i and then echo it out. echo '</td>'; echo '<td>'; echo '<form name=view method=get action=viewprofile.php>'; echo "<input type=hidden name=username value="; echo $row['username']; echo "/>"; echo "<a href=viewprofile.php?username="; echo $row['username']; echo ">"; echo $row['username']; echo "</a></td></form>"; echo '<td>'; echo $row['score']; echo '</td>'; echo '</tr>'; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/240670-automatically-increase-number-in-table/#findComment-1236121 Share on other sites More sharing options...
OneEyedWillie Posted June 29, 2011 Author Share Posted June 29, 2011 Wow.. I figured it was something easy like that! Thanks for your guys help! It's greatly appreciated. I love this forum.. I always get quick responses. :3 Quote Link to comment https://forums.phpfreaks.com/topic/240670-automatically-increase-number-in-table/#findComment-1236123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.