Page 1 of 1

Adding coloum at end of loop

Posted: Mon Oct 23, 2006 10:52 am
by reecec
Hi sorry another problem

This is hard to explain...

I have a while loop creating a table containing all my fields this part below checks if it is the last row of the field before the while loop ends to start the next field. so basicly its just adding a check box to every row of the table

Code: Select all

echo $i;
$fields=mysql_num_fields($result);
$newfield=$fields-1;
echo "<strong>$newfield</strong>";
if ($i==$newfield) 
{
echo "<td bgcolor='#C1C1C1'>";
?>

<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['0']; ?>">





<?
echo "</td>";
}
this checks if the loop count $i is the last one by checking with num_fields but i have had to take one away from the num count not sure why as i thought the last $i should be the same number as the num_field count is this someting to do with the $i starting at 0 not 1.


Anyway heres the problem this works fine but as soon as i get 5 it doesnt add the column i have no idea why

thanks reece

Posted: Mon Oct 23, 2006 1:19 pm
by Cameri
The code you are providing is not enough for us to help you out.

Make sure you are not counting the number of rows each time the while loop iterates, just do it once outside, it shouldn't change at any point. (This is not the problem imho).

About $i and mysql_num_fields, $i is a counter that probably starts at 0 in your script, meaning, the second row will be when $i equals 1. Now, mysql_num_fields returns the number of rows, now let's say that $n is the number of rows in your result, you'll be at the last row when $i equals $n - 1.

Posted: Mon Oct 23, 2006 2:01 pm
by reecec
Thanks thats what i thougth about the count


Dont worry i found out what was wrong i had two ID fields so it counted that once on one of my table which stoped the next column being added

thanks reece