如何使用PHP以编程方式增加CSS ID

时间:2011-06-16 01:38:37

标签: php css

如何以编程方式使用php递增我的css ID?

foreach ($query_cat->result() as $row_cat)
{
    $row_cat_id = $row_cat->id;

    echo '<div class="product-wrapper">';
    echo '<div id="product-header" class="product-header">';
    echo $row_cat->title;
    echo '</div>';
    echo '</div>';

    $query_prod = $this->db->get_where('products', array('category_id'=>$row_cat_id));

    foreach ($query_prod->result() as $row_prod)
    {
        $row_prod_id = $row_prod->id;
        echo '<div id="product-contents" class="product-contents">';
        echo $row_prod->title.'&nbsp;'.$row_prod->id;
        echo '</div>';

    }
}

我想要发生的是增加ID product-header product-contents ,具体取决于生成的行数

类似这样的事情

product-header1,product-header2,product-header3 .... product-contents1,product-contents2,product-contents3 ....

谢谢!

2 个答案:

答案 0 :(得分:0)

只需在foreach之前初始化增量变量。

$i = $j = 0;
foreach ($query_cat->result() as $row_cat) {
    $i++;
    $row_cat_id = $row_cat->id;
    echo '<div class="product-wrapper">';
    echo '<div id="product-header'.$i.'" class="product-header">';
    ...

    foreach ($query_prod->result() as $row_prod) {
        $j++;
        $row_prod_id = $row_prod->id;
        echo '<div id="product-contents'.$j.'" class="product-contents">';
        ...
    }
}

答案 1 :(得分:0)

$i = $j = 1;
foreach (...) {
    printf('<div id="product-header-%u" class="product-header">', $i++);

    foreach (...) {
        printf('<div id="product-contents-%u" class="product-contents">', $j++);
    }
}