A problem with an If statement

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
yoda69
Forum Newbie
Posts: 16
Joined: Wed Jun 20, 2007 10:21 am

A problem with an If statement

Post 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
minorDemocritus
Forum Commoner
Posts: 96
Joined: Thu Apr 01, 2010 7:28 pm
Location: Chicagoland, IL, USA

Re: A problem with an If statement

Post 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
wurdup
Forum Commoner
Posts: 39
Joined: Thu Apr 01, 2010 11:36 am

Re: A problem with an If statement

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