如何:
$counter = 0;
while ($subject = mysql_fetch_array($subject_qry))
{
if ($counter % 2 == 0)
{
$output = "<div class=\"left\" id=\"{$subject['id']}\"></div>";
}
else
{
$output = "<div class=\"right\" id=\"{$subject['id']}\"></div>";
}
// do something with $output here...
$counter ++;
}
或者你可以实现一个布尔值相同的结果:
$left = true;
while ($subject = mysql_fetch_array($subject_qry))
{
if ($left)
{
$output = "<div class=\"left\" id=\"{$subject['id']}\"></div>";
}
else
{
$output = "<div class=\"right\" id=\"{$subject['id']}\"></div>";
}
// do something with $output here...
$left = !$left;
}
来源
2012-06-10 20:29:47
Ben
请停止与古'mysql_ *'函数编写新的代码。他们不再被维护,社区已经开始[弃用流程](http://goo.gl/KJveJ)。相反,您应该了解[准备好的语句](http://goo.gl/vn8zQ)并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能决定,[本文](http://goo.gl/3gqF9)将有助于选择。如果你关心学习,[这里是一个很好的PDO相关教程](http://goo.gl/vFWnC)。 –