?
Solved

Numbering Table Rows (ASC) in PHP

Posted on 2007-10-09
4
Medium Priority
?
239 Views
Last Modified: 2013-12-13
Hi,

I am looking to add a new column in front of first column displaying numbers in ascending order like this:

New Column          Col1           Col2
1                            Y                N              
2                            N                N
3                            Y                Y
4                            N                N
5                            Y                N

I just need it to display a number for each row in ascending order. How would I do something like that?

Here is the PHP code:

<?php

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";

echo "<tr>";
echo "<td class=\"th\" >Col1</td>";
echo "<td class=\"th\" >Col2</td>";

if($result && mysql_num_rows($result) > 0)
{    for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $resultArray[$i] = mysql_fetch_array($result);
}

foreach ($resultArray as $val) {
   echo ('<tr>');//first row
   if (($rowcount%2) == 0) {
        $css_class = "\"row\"";
    } else {
        $css_class = "\"alt\"";
    }

      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'</td>');
    echo ('</tr>');


} // end if results
echo ('</table>');

?>
0
Comment
Question by:fcruz5
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20043448
<?php

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";

echo "<tr>";
echo "<td class=\"th\" >Row #</td>";
echo "<td class=\"th\" >Col1</td>";
echo "<td class=\"th\" >Col2</td>";

if($result && mysql_num_rows($result) > 0)
{    for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $resultArray[$i] = mysql_fetch_array($result);
}

foreach ($resultArray as $val) {
   echo ('<tr>');//first row
   if (($rowcount%2) == 0) {
        $css_class = "\"row\"";
    } else {
        $css_class = "\"alt\"";
    }

      echo ('<td class=' . $css_class . ' align=default >'.($i+1).'</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'</td>');
    echo ('</tr>');


} // end if results
echo ('</table>');

?>
0
 

Author Comment

by:fcruz5
ID: 20043514
Hi,

It didn't display like in the example above. It kept repeating the number of results over and over again which was 111.

So it came out like this:

111
111
111
111
111
and so on...

instead of like this:

1
2
3
4
5
and so on...

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20043566
sorry, I was missing a small detail:

<?php

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";

echo "<tr>";
echo "<td class=\"th\" >Row #</td>";
echo "<td class=\"th\" >Col1</td>";
echo "<td class=\"th\" >Col2</td>";

if($result && mysql_num_rows($result) > 0)
{    for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $resultArray[$i] = mysql_fetch_array($result);
}

$i = 0;
foreach ($resultArray as $val) {
   echo ('<tr>');//first row
   if (($rowcount%2) == 0) {
        $css_class = "\"row\"";
    } else {
        $css_class = "\"alt\"";
    }

      echo ('<td class=' . $css_class . ' align=default >'.(++$i).'</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'</td>');
    echo ('</tr>');


} // end if results
echo ('</table>');

?>
0
 
LVL 48

Expert Comment

by:hernst42
ID: 20043583
use

$i = 0;
foreach ($resultArray as $val) {
 echo ('<tr>');//first row
   if (($rowcount%2) == 0) {
        $css_class = "\"row\"";
    } else {
        $css_class = "\"alt\"";
    }

    echo ('<td class=' . $css_class . ' align=default >'.(++$i).'</td>');

      echo ('<td class=' . $css_class . ' align=default >'.$val[1].'</td>');
      echo ('<td class=' . $css_class . ' align=default >'.$val[2].'</td>');
    echo ('</tr>');


} // end if results
echo ('</table>');
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question