PHP 只递增一次

发布于 2024-12-27 05:54:36 字数 581 浏览 0 评论 0原文

下面代码中 $i 的值始终为 2。看起来它会增加到第一次,但只是那一次。有什么想法吗?

 foreach ($records as $row){

    $i = 1;
    $i++

    if ($i % 2 != 0){
        $trClass = 'odd';               
    }else{
        $trClass = 'even';
    }

    echo '<tr class="' . $trClass . '"><td>' . 
        anchor("admin/delete/$row->id", 'delete') . '</td><td>' . 
        anchor("admin/edit/$row->id", 'Edit') . '</td>';

    foreach ($row as $key => $value){
        echo '<td>' . $value . '</td>';
    }

    echo '</tr>';
    $i++;
}

The value for $i in the code below is always 2. It seems it increments to the first time, but only that time. Any thoughts?

 foreach ($records as $row){

    $i = 1;
    $i++

    if ($i % 2 != 0){
        $trClass = 'odd';               
    }else{
        $trClass = 'even';
    }

    echo '<tr class="' . $trClass . '"><td>' . 
        anchor("admin/delete/$row->id", 'delete') . '</td><td>' . 
        anchor("admin/edit/$row->id", 'Edit') . '</td>';

    foreach ($row as $key => $value){
        echo '<td>' . $value . '</td>';
    }

    echo '</tr>';
    $i++;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扭转时空 2025-01-03 05:54:36

每次循环时,您都会将其重新分配给1。而是在循环外对其进行初始化。

$i = 1;
foreach ($records as $row){
    /*
     ...
    */

    $i++;
}

我还看到您在循环开始和结束时都在递增。我假设您只想执行一次(可能只保留最后的那个;删除开头的那个)。

You're reassigning it to 1 every time through the loop. Initialize it outside the loop instead.

$i = 1;
foreach ($records as $row){
    /*
     ...
    */

    $i++;
}

Also I see that you're incrementing both at the beginning of the loop and at the end. I assume you only want to do it once (probably keep only the one at the end; remove the one at the beginning).

笑饮青盏花 2025-01-03 05:54:36

您的代码存在逻辑错误,因为它在循环的每次迭代中都声明了 $i 。

这就是您要做的:

在每次迭代中声明 $i = 1,然后递增 $i。

所以在每次迭代中,你总是得到 $i = 2 。

在 foreach 循环之外声明 $i = 1 并仅增加 $i 一次,如下所示:

$i = 1;
foreach ($records as $row){

        if ($i % 2 != 0){
           $trClass = 'odd';               
        }else{
           $trClass = 'even';
        }

        echo '<tr class="' . $trClass . '"><td>' . anchor("admin/delete/$row->id", 'delete') . '</td>
            <td>' . anchor("admin/edit/$row->id", 'Edit') . '</td>';

                 foreach ($row as $key => $value){
                     echo '<td>' . $value . '</td>';
                 }
         echo '</tr>';
         $i++;

    }

Your code has a logical error in that it declares $i in every iteration of the loop.

This is what you do:

declare $i = 1 in every iteration and then increment that $i.

so in every iteration, you get $i = 2 all the time.

declare $i = 1 outside the foreach loop and increment $i just once like so:

$i = 1;
foreach ($records as $row){

        if ($i % 2 != 0){
           $trClass = 'odd';               
        }else{
           $trClass = 'even';
        }

        echo '<tr class="' . $trClass . '"><td>' . anchor("admin/delete/$row->id", 'delete') . '</td>
            <td>' . anchor("admin/edit/$row->id", 'Edit') . '</td>';

                 foreach ($row as $key => $value){
                     echo '<td>' . $value . '</td>';
                 }
         echo '</tr>';
         $i++;

    }
猫瑾少女 2025-01-03 05:54:36

这样开始

$i = 1;
foreach ($records as $row){
    $i++;
    ...

start it this way

$i = 1;
foreach ($records as $row){
    $i++;
    ...
睫毛溺水了 2025-01-03 05:54:36

请在 foreach 之前添加 $i = 1;

please put $i = 1; before foreach

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文