[英]PHP Alternate Row Color Help
I've created a function that will display a list of pages for my CMS system. 我创建了一个函数,该函数将显示CMS系统的页面列表。
function build_pages(&$table, $pid, $sub=0) {
global $db;
if ($sub == 1) {
$class = "section-sub";
} else {
$class = "section-name";
}
$i = 0;
$query = $db->simple_select("pages", "title,section,name,id", "pid='" . $pid . "'");
while ($pages = $db->fetch_array($query)) {
if ($i % 2 == 0) {
$alt_row = "row1_alt"; // dark
$i++;
} else {
$alt_row = "row2_alt"; // light
$i++;
}
$table->construct_cell("<div class=\"" . $class . "\">" . $pages['title'] . "</div>", array("divstyle" => $alt_row));
$table->construct_cell("", array("divstyle" => $alt_row));
$table->construct_cell("", array("divstyle" => $alt_row));
$table->construct_cell("", array("divstyle" => $alt_row));
$table->construct_row();
build_pages(&$table, $pages['id'], 1);
}
}
However, this is what the alternate row coloring is doing (notice the row coloring is not perfectly alternating): http://i53.tinypic.com/2afj3mb.png 但是,这就是备用行着色的作用(请注意,行着色并非完全交替): http : //i53.tinypic.com/2afj3mb.png
Maybe someone can help me find a flaw in this. 也许有人可以帮助我找到一个缺陷。
Thanks. 谢谢。
It's because you are using a recursive function. 这是因为您正在使用递归函数。 The first iteration of the loop then goes into build_pages() again which starts a new loop and so forth.
然后,循环的第一次迭代再次进入build_pages(),这将启动一个新的循环,依此类推。 Maybe you could make $alt_row a static variable and simply toggle it each time it occurs.
也许您可以将$ alt_row设为静态变量,并在每次发生时简单地对其进行切换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.