Jump to content

Automatically increase number in table


OneEyedWillie

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/240670-automatically-increase-number-in-table/
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.