Forum Moderators: coopster

Message Too Old, No Replies

Adding incremental row numbers to query results

         

Patrick Taylor

3:55 am on Apr 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm using this:


$query = "SELECT CONCAT(name, phone, fax) AS entries FROM suppliers ORDER BY name ASC";

// Run the query.
$result = @mysql_query ($query);

// If it ran OK, display the records.
if ($result) {

echo '<table width="100%"><tr><td>Total listings - '. $row_count . ':</td></tr>';

// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td></tr>\n";
}

echo '</table>';

... and I would like the output to have each row numbered incrementally, irrespective of what order they're displayed in.

I've tried various things without success. Help would be appreciated.

Patrick

kniceguy2know

10:05 am on Apr 18, 2004 (gmt 0)

10+ Year Member



Here's a newbie solution:-

$query = "SELECT CONCAT(name, phone, fax) AS entries FROM suppliers ORDER BY name ASC";

// Run the query.
$result = @mysql_query ($query);

// If it ran OK, display the records.
if ($result) {
$rownum=1;//variable to keep track of row number
echo '<table width="100%"><tr><td>Total listings - '. $row_count . ':</td></tr>';

// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>".$rownum++." $row[0]</td></tr>\n";
}/*use of post increment to go to the next row number after displaying the current one*/

echo '</table>';

Patrick Taylor

1:17 pm on Apr 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the suggestion. It worked fine. In the end, I used this:


// Fetch and print all the records.
for ($x = 1; $row = mysql_fetch_array($result, MYSQL_NUM); $x++) {
echo "<tr><td>" . $x . ". " . $row[0] . "</td></tr>\n";
}

I'm a newbie too, or more accurately: an untalented mediumbie.

Patrick