Page 1 of 1

'while' not working as expected

Posted: Wed May 19, 2010 9:52 am
by IGGt
Hi Guys,

I have a script that gets a MySQL result consisting of an ID field (1-25) and a colour.

I then put those into a html table. I am trying to get a situation whereby ID's 1-5 are a row (<tr>), then I start a new row (</tr><tr>) at ID 6. (and going forward also row 11,16,21). But at present it doesn't work. It seems to be assigning 6 as every ID, and so it is creating a new row at each stage.

Code: Select all

$result = mysql_query("SELECT `id`, `colour` FROM `db1`.`squares` ORDER BY `id` ASC");

echo '<table>';
echo '<tr>';
while($row = mysql_fetch_array($result)) {
        							if($row['id'] = 6) {
	        									echo '</tr><tr>';
        										}
        echo '<td bgcolor=' . $row['colour'] . '>'.$row['id'].'</td>';
}

</tr>
</table>
As you can see I have put the $row['id'] in each table field (<td>) to see what it is returning.

Any ideas as to why it is returning 6 for every result, and not the ID as per the MySQL statement?

Re: 'while' not working as expected

Posted: Wed May 19, 2010 10:38 am
by mikosiko
IGGt wrote:.....
while($row = mysql_fetch_array($result)) {
if($row['id'] = 6) {
echo '</tr><tr>';
}
echo '<td bgcolor=' . $row['colour'] . '>'.$row['id'].'</td>';
}

</tr>
</table>
[/syntax]

As you can see I have put the $row['id'] in each table field (<td>) to see what it is returning.

Any ideas as to why it is returning 6 for every result, and not the ID as per the MySQL statement?
try this

Code: Select all

while($row = mysql_fetch_array($result)) {
        echo '<td bgcolor=' . $row['colour'] . '>'.$row['id'].'</td>';
        if (!($row['id'] % 5)) {
            echo '</tr><tr>';
        }
}

Re: 'while' not working as expected

Posted: Wed May 19, 2010 10:52 am
by IGGt
That's great, fixed both problems in one go.

cheers.