How to create a table in PHP with MySQL?

HOw do I get PHP/MySQL output in a table to be numbered?

  • have a MySQL DB that has a DB w/ one table, that having two rows. I use this PHP to retrieve it and put it into a table (It's for hi-scores): $fetch = mysql_query("SELECT * FROM scores ORDER BY score DESC LIMIT 10"); while($row = mysql_fetch_array($fetch)) { echo "<table border='1' width=300><tr><td>&nbsp $row[name] &nbsp</td><td>&nbsp $row[score] &nbsp</td></tr></table>"; } I would like to have each horizontal row have a number before it, numbered 1 - 10 (it's a hi-score platform). How would I do that? Keep in mind that I don't want them numbered as they are in the DB, but as they are sorted and listed on the page. Help? The page is here: http://pennyontherail.com/score10.php . Thank you very much!

  • Answer:

    First thing, it's not a good idea to do SELECT *, just select the fields that you want, even if * is all of them. Why? Because it causes strain on the db engine. Another thing, you do not need to create a table for each iteration. Just one table, then loop through the rows. To make your numbers, you will want to add a number to the loop, eg: <table> <thead> <tr> <th>Number</th><th>Person</th><th>Scor… </tr> </thead> <tbody> <?php while($row = mysql_fetch_array($fetch)) { $i = $i+1 $name = $row[0]; $score = $row[1]; ?> <tr> <td><?php echo $i; ?></td><td><?php echo $name; ?></td><td><?php echo $score; ?></td> </tr> <?php } mysql_free_result($fetch); ?> </tbody> </table>

m3thus3l... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Just make a counter. Pseudocode i = 1 echo table foreach row echo i echo field name i++ end foreach echo /table

LordSam

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.