• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Numbering Table Rows (ASC) in PHP

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
fcruz5
Asked:
fcruz5
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
<?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
 
fcruz5Author Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
hernst42Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now