简体   繁体   中英

PHP Alternate Row Color Help

I've created a function that will display a list of pages for my CMS system.

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

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. Maybe you could make $alt_row a static variable and simply toggle it each time it occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM