Forum Moderators: coopster

Message Too Old, No Replies

Increment row numbers

         

Patrick Taylor

9:40 pm on Dec 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's part of my code:

// Do the loop
while ($row = mysql_fetch_assoc($result)) {

// Toggle the row colour id
$color_id = ($color_id=='#EBEBEB'? '#FFFFFF' : '#EBEBEB');

// Set the bar width
$bar_width = (($row['number'] / $total) * 100);
// Format the number
$bar_width = number_format ($bar_width, 0);

// Display the rows

echo '<tr><td width="300" valign="top"><p class="text1a">' . $i . '<a href="http://www.domain.com' . $row['page_on'] . '">' . $row['page_on'] . '</a></p></td><td width="60" valign="top"><p class="text1a"><b>' . $row['number'] . '</b></p></td><td width=" " valign="top" bgcolor="' . $color_id . '"><p class="bar"><img src="bar.jpg" height="11" width="' . $bar_width . '%" /></td></tr>';

}

I've included the variable $i at the start of each row of results and I would like this to be 1 on the first row, 2 on the second, and so forth - incrementing with each row so that each row is numbered.

I've tried a few ways without success, and would appreciate a pointer. Many thanks.

Gorilla

10:03 pm on Dec 4, 2004 (gmt 0)

10+ Year Member



One way to achieve incrementing $i for each row would be to replace the while statement [while ($row = mysql_fetch_assoc($result)) {] by a for statement:

for ( $i = 1; $row = mysql_fetch_assoc($result); $i++ ) {

[edited by: Gorilla at 10:20 pm (utc) on Dec. 4, 2004]

jatar_k

10:14 pm on Dec 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



initialize $i before the loop starts

$i = 1;

then in the end of the loop

$i++;

to increment it at the end of every iteration

Patrick Taylor

10:41 pm on Dec 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thankyou both! It worked a treat.

Patrick