Number each row in a table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cwjones
    New Member
    • Nov 2007
    • 4

    Number each row in a table

    Hi, Small headache problem with a highscore table.
    I have a table that displays a list of highscores. The code pulls the data from the sql table in the correct order highest score to the lowers score via the "order by score desc" tag.
    It repeats the region with no problems and all the information is populating the correct table cell.
    The scores are being displayed in descending order so I need to find a way to display the ranking of each row. Is there some simple mathematical coding I can place in the the first column that will add a place each time a new score is entered and rearragnge the ranking.

    eg.
    currently

    simon 22.4
    gary 22
    steven 21

    what i want is..

    1 simon 22.4
    2 gary 22
    3 steven 21

    and if paul comes along and scores 22.5

    1 paul 22.5
    2 simon 22.4
    3 gary 22
    4 steven 21

    any help would be much appreciated.

    Thanks
  • Lumpy
    New Member
    • Oct 2007
    • 69

    #2
    I am assuming that when a new score is entered, it is getting stored into the table, and then the query runs again and prints out the scores. In order to print out the scores you must have some kind of loop that you are using to keep cycling through the table to get all the entries. So can you just set up a variable to count for you, and just echo that variable in the place you want the number to appear.

    Start out with...
    [CODE=php]
    $i = 1;
    [/CODE]
    Before the loop begins, then at the end of the loop, by that I mean the last thing the loop does before starting over just increment that variable...
    [CODE=php]
    $i++;
    [/CODE]
    And then just echo out $i when or where you want that number to be.

    I would think something like this would work. Hope it helps!

    Comment

    • cwjones
      New Member
      • Nov 2007
      • 4

      #3
      Thats works on the first page. Thanks, although when the next page of results are pulled up it starts from 1 again.
      Is there a way the number can be carried on to the next page?
      thanks again

      Comment

      • Lumpy
        New Member
        • Oct 2007
        • 69

        #4
        Originally posted by cwjones
        Thats works on the first page. Thanks, although when the next page of results are pulled up it starts from 1 again.
        Is there a way the number can be carried on to the next page?
        thanks again

        Hey, no fair...you didn't say that in the first post.. :D

        So, you are displaying something like 25 per page or something like that? I don't know the code your using, but is it possible for you to pass the variable, or use a variable that you are already passing to setup your counter?

        I guess the question I have for you is how is your script knowing what part of the list to display, and is it possible to use that info to setup the counter.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, CW.

          Minor thing, but ++$i is a tad bit faster than $i++.

          Instead of having $i start at 1, have it start at $offset + 1, where $offset is the offset value you use in your SQL query.

          Comment

          • cwjones
            New Member
            • Nov 2007
            • 4

            #6
            Sorry to pass on the bad news about the multiple pages...
            Got some more news for you. Could be good might be bad.. Its written using Dreamweaver and its lovely Recordsets.
            I thought about passing the value through the Recordset Navigation bar but that may cause problems if users decide to view the last records (7 or 8 pages down the line). I suppose it may be possible to do a Count of the full amount of entries and the subtract the amount viewable on the last page and start the count from there...

            OR

            Get rid of the "View last" option.... (sounds good to me).

            Maybe if I passed the value from the previous page to the next it might work but that posses another problem. I haven't a clue how to go about it..

            Here's the trimmed down code anyways:
            [code=php]
            <table width="30%" border="0" cellspacing="0" cellpadding="0" >
            <tr>
            <td>Rank</td>
            <td>Score</td>
            <td>Name</td>
            </tr>
            <?php
            $i=1;
            do { ?>
            <tr>
            <td><? echo $i ?></td>
            <td><?php echo $row_Recordset1['score']; ?></td>
            <td><?php echo $row_Recordset1['name']; ?></td>
            </tr>
            <?php $i++;
            } while ($row_Recordset 1 = mysql_fetch_ass oc($Recordset1) ); ?>
            </table>

            //////////////And heres the Nav Bar...:

            <table border="0" width="50%" align="center">
            <tr>
            <td width="23%" align="center">
            <?php if ($pageNum_Recor dset1 > 0) { // Show if not first page ?>
            <a href="<?php printf("%s?page Num_Recordset1= %d%s", $currentPage, 0, $queryString_Re cordset1); ?>"><img src="First.gif" border=0></a>
            <?php } // Show if not first page ?>
            </td>
            <td width="31%" align="center">
            <?php if ($pageNum_Recor dset1 > 0) { // Show if not first page ?>
            <a href="<?php printf("%s?page Num_Recordset1= %d%s", $currentPage, max(0, $pageNum_Record set1 - 1), $queryString_Re cordset1); ?>"><img src="Previous.g if" border=0></a>
            <?php } // Show if not first page ?>
            </td>
            <td width="23%" align="center">
            <?php if ($pageNum_Recor dset1 < $totalPages_Rec ordset1) { // Show if not last page ?>
            <a href="<?php printf("%s?page Num_Recordset1= %d%s", $currentPage, min($totalPages _Recordset1, $pageNum_Record set1 + 1), $queryString_Re cordset1); ?>"><img src="Next.gif" border=0></a>
            <?php } // Show if not last page ?>
            </td>
            <td width="23%" align="center">
            <?php if ($pageNum_Recor dset1 < $totalPages_Rec ordset1) { // Show if not last page ?>
            <a href="<?php printf("%s?page Num_Recordset1= %d%s", $currentPage, $totalPages_Rec ordset1, $queryString_Re cordset1); ?>"><img src="Last.gif" border=0></a>
            <?php } // Show if not last page ?>
            </td>
            </tr>
            </table>
            [/code]
            Thanks for your help on this..
            Last edited by pbmods; Nov 27 '07, 10:36 PM. Reason: Added CODE tags.

            Comment

            • cwjones
              New Member
              • Nov 2007
              • 4

              #7
              Thanks for your help pbmods. When I tried the $offset it seems to miss the 1st place entry.
              I'm assuming that the $offset + 1; goes at the start of the loop in place of $i=1;
              Thanks again

              Comment

              Working...