SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Numbering Table Rows

    Imagine a database table that features information on various geographic units, including oceans (oce), continents (con), nations (nat) and dependencies (dep). Suppose I display a table that lists only nations, ranked in order of size (geographic area), with Russia first.

    Is there a way to insert numerals to indicate each nation's rank? In other words, the table would look like this...

    Code:
    1. Russia
    2. Canada
    I might insert the numerals in a separate table column, or I might just insert them in the column holding the nations' names.

    Is there a PHP function that sequentially numbers table rows?

    Thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a counter?
    PHP Code:
    $i 1;
    foreach(
    $countries as $v) {
        echo 
    $i;
        echo 
    $v;
        
    $i++


  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $nat = array('Russia''Canada''New Zealand''Monaco''Singapore');
    $count=0;
    foreach(
    $nat as $n) {
       
    $count++;
       echo 
    "<tr>
           <td>
    $count</td>
           <td>
    $nat</td>
       </tr>\n"
    ;

    You're probably looping over a database result set. Just increment and echo a variable as shown.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tips. That'll beat adding extra numerical fields in my database table.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •