Jump to content

[SOLVED] Numbering fields in a table


yogibear

Recommended Posts

Hi all I’m not total new to php but I still don’t understand a lot of php things so please go easy.

 

I have 3 fields from a database going into a table. username, car and credits and a rank column the table is ordered by the total of 3 different fields in the database. I want the rank column to start at 1 for row 1, and 2 for row 2 and so on. I have tried some things that i found in another post but couldnt get them to work so I removed it.

Rank

Username

Car

Credits

1

qwert

ford

100

2

test

renault

50

 

this is the code i have

<?php
$con = mysql_connect("localhost","***","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("***", $con);

$result = mysql_query("SELECT * FROM userinformation ORDER by credits + original + totalspent DESC");

echo "<table border='1'>
<tr>
<th>Rank</th>
<th>Username</th>
<th>Car</th>
<th>Credits</th>
</tr>";while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>". $row['rank'] . "</td>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['car'] . "</td>";
  echo "<td>" . $row['credits'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close($con);
?>

I hope someone can help

 

thanks in advanced

yogi

 

Link to comment
https://forums.phpfreaks.com/topic/52361-solved-numbering-fields-in-a-table/
Share on other sites

<?php
echo "<table border='1'>
<tr>
<th>Rank</th>
<th>Username</th>
<th>Car</th>
<th>Credits</th>
</tr>";
$rank = 1;
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>". $rank++ . "</td>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['car'] . "</td>";
  echo "<td>" . $row['credits'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close($con);
?>

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.