0

Hello i'm having problems adding numbered rows/serial numbers to my database query result. I've used $number to collect the actual number of rows. Then another problem i'm trying to avoid is: Numbering of Column Headers.

Thanks for the help.

<?php
$number = mysql_num_rows($query);

for ($serial = 0; $serial < $number; $serial++)
{
    echo "<tr>". $serial ."</tr>";
}

for ($i = 0; $i < $number_cols; $i++)
{  
    echo "<th>" . mysql_field_name($query, $i) . "</th>\n";
}

while ($row = mysql_fetch_row($query))
{    
    echo "<tr align=center>\n";
    for ($i = 0; $i < $number_cols; $i++)
    {   
        echo "<td>";
        if (!isset($row[$i])) 
        {
            echo "NULL";
        }
        else
        {
            echo $row[$i];
        } 
        echo "</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>"; 
echo "</span>";
echo "</div>";
?>
4
  • 1
    you are trying to avoid numbering of Column Headers? Do not number them. Commented Mar 3, 2012 at 6:30
  • @col. Shrapnel I'm not trying to number the Column Headers. It's part of the problem i'm facing. How do you advice i go about adding serial numbers to the dataset? Commented Mar 3, 2012 at 6:40
  • just add a variable and increment it with each iteration Commented Mar 3, 2012 at 6:42
  • next time please post your WORKING code and ask a question "how to add some feature to it". Not the code you come to dead end with Commented Mar 3, 2012 at 6:48

1 Answer 1

3

trying my best to get something sane out of your terrific code.

<?php
$serial = 1;
while ($row = mysql_fetch_row($query))
{    
  echo "<tr align=center>\n";
  echo "<td>";
  echo $serial++;
  echo "</td>\n";
  foreach ($row as $value)
  {   
    echo "<td>$value</td>\n";
  }
  echo "</tr>\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, the code added a column (from the left) to the table with "1++" in each of the rows, also it shifted the column header to the left, thereby leaving leaving a column (extreme right) without any header. Thanks
I HOPE you can fix such a "problem"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.