Page 1 of 1

A problem with an If statement

Posted: Sat Apr 10, 2010 3:03 am
by yoda69
Hey people,

I'm can't get an IF statement to do what I want. It's a simple FOR loop which pulls data from a database and puts it into a table. The IF statement is basically putting the <tr></tr> at the end of every 10 rows. I can't get it to put only an </tr> at the end of the loop.

Here's my code:

Code: Select all

$x = 0;
$y = 0;

for($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
$background = $row[color];
$id = $row[id];
$taken = $row[taken];

echo "<td class=\"cell\"><a class=\"test\" ";

if ( $taken == '0' ){ 
echo "style=\"background:#".$background."\" href=\"http://www.mysite.com/1.php?cell=".$id."\"></a></td>\n";
}

else {
echo "style=\"background:#".$background."\" href=\"http://www.mysite.com/1.php?cell=".$id."\"></a></td>\n";
}

$x++;

if( $x == 10 &&  $y < 10)
{
  echo "
    </tr>
    <tr>\n";
   }
elseif ( $x < 10 &&  $y < 10)
{
}
elseif (  $x = $y )
{
echo "</tr>\n";

}
else
{
echo "</tr>\n";

}   
}


Thanks

Re: A problem with an If statement

Posted: Sat Apr 10, 2010 11:44 am
by minorDemocritus

Code: Select all

elseif (  $x = $y )
...Should probably be...

Code: Select all

elseif (  $x == $y )
A common mistake.

But, it looks like you can just do away with that whole elseif block... the last else portion will include $x == $y if $x >= 10

Re: A problem with an If statement

Posted: Sat Apr 10, 2010 12:23 pm
by wurdup
altough your code works just for reference a modular command would be normal procedure for this:

if ($x % 10 == 0) { //do every 10 steps}