Adding coloum at end of loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Adding coloum at end of loop

Post 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
Last edited by reecec on Mon Oct 23, 2006 1:42 pm, edited 1 time in total.
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post 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.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post 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
Post Reply