如何使用PHP代码将id增加1或在HTML中完全更改它

如何使用PHP代码将id增加1或在HTML中完全更改它,php,codeigniter,Php,Codeigniter,我从数据库中获取所有结果,并使用此查询将它们显示在数据表中 $result['getTableGroup'] = $this->getTableGroup(); return $this->load->view('users/index', $result); 这是我的看法 名称 组用户 地位 行动 如果要设置id动态,请设置一个变量,然后使用它。我先使用$i 1,然后增加它,这样id将是唯一的。如果您需要其他信息,请通知。我使用$i=1;然后最后增加它,使其成为

我从数据库中获取所有结果,并使用此查询将它们显示在数据表中

  $result['getTableGroup'] = $this->getTableGroup();
  return $this->load->view('users/index', $result);
这是我的看法


名称
组用户
地位
行动

如果要设置id动态,请设置一个变量,然后使用它。我先使用$i 1,然后增加它,这样id将是唯一的。如果您需要其他信息,请通知。我使用$i=1;然后最后增加它,使其成为动态的

<table class="table">
    <thead>
    <tr>
        <th>Name</th>
        <th>Group Users</th>
        <th>Status</th>
        <th>Action</th>
    </tr>
    </thead>
    <tbody>
    <?php
    $i = 1;
    foreach ($getTableGroup as $value) { ?>

        <tr>
            <td>
                <?= $value->group_name ?>
            </td>
            <td>
                <?php if ($value->group_status == 'on'): ?>
                    <div class="switch">
                        <div class="onoffswitch">
                            <input type="checkbox" checked class="onoffswitch-checkbox" id="example<?= $i ?>"
                                   name="group-status">

                            <label class="onoffswitch-label" for="example<?= $i ?>">
                                <span class="onoffswitch-inner"></span>
                                <span class="onoffswitch-switch"></span>
                            </label>
                        </div>
                    </div>
                <?php elseif ($value->group_status == 'off'): ?>
                    <div class="switch">
                        <div class="onoffswitch">
                            <input type="checkbox" checked class="onoffswitch-checkbox" id="example<?= $i ?>"
                                   name="group-status">

                            <label class="onoffswitch-label" for="example<?= $i ?>">
                                <span class="onoffswitch-inner"></span>
                                <span class="onoffswitch-switch"></span>
                            </label>
                        </div>
                    </div>

                <?php endif; ?>
            </td>

        </tr>

        <?php
        $i++;
    } ?>
    </tbody>
</table>

名称
组用户
地位
行动

如果要设置id动态,请设置一个变量,然后使用它。我先使用$i 1,然后增加它,这样id将是唯一的。如果您需要其他信息,请通知。我使用$i=1;然后最后增加它,使其成为动态的

<table class="table">
    <thead>
    <tr>
        <th>Name</th>
        <th>Group Users</th>
        <th>Status</th>
        <th>Action</th>
    </tr>
    </thead>
    <tbody>
    <?php
    $i = 1;
    foreach ($getTableGroup as $value) { ?>

        <tr>
            <td>
                <?= $value->group_name ?>
            </td>
            <td>
                <?php if ($value->group_status == 'on'): ?>
                    <div class="switch">
                        <div class="onoffswitch">
                            <input type="checkbox" checked class="onoffswitch-checkbox" id="example<?= $i ?>"
                                   name="group-status">

                            <label class="onoffswitch-label" for="example<?= $i ?>">
                                <span class="onoffswitch-inner"></span>
                                <span class="onoffswitch-switch"></span>
                            </label>
                        </div>
                    </div>
                <?php elseif ($value->group_status == 'off'): ?>
                    <div class="switch">
                        <div class="onoffswitch">
                            <input type="checkbox" checked class="onoffswitch-checkbox" id="example<?= $i ?>"
                                   name="group-status">

                            <label class="onoffswitch-label" for="example<?= $i ?>">
                                <span class="onoffswitch-inner"></span>
                                <span class="onoffswitch-switch"></span>
                            </label>
                        </div>
                    </div>

                <?php endif; ?>
            </td>

        </tr>

        <?php
        $i++;
    } ?>
    </tbody>
</table>

名称
组用户
地位
行动

简单使用递增变量,如下所示

1st:声明变量$i=1

2nd:每次循环增加一个$i++

<?php $i=1; foreach ($getTableGroup as $value) : ?>
  ......
  <input type="checkbox" checked class="onoffswitch-checkbox" id="checkbox<?php echo $i; ?>" name="group-status">
  <label class="onoffswitch-label" for="example<?php echo $i; ?>">
  ......
<?php $i++; endforeach; ?>

......

简单使用递增变量,如下所示

1st:声明变量$i=1

2nd:每次循环增加一个$i++

<?php $i=1; foreach ($getTableGroup as $value) : ?>
  ......
  <input type="checkbox" checked class="onoffswitch-checkbox" id="checkbox<?php echo $i; ?>" name="group-status">
  <label class="onoffswitch-label" for="example<?php echo $i; ?>">
  ......
<?php $i++; endforeach; ?>

......

使用自动递增。使用如下代码

 <table class="table">
                                        <thead>
                                            <tr>
                                                <th>Name</th>
                                                <th>Group Users</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php 
                                            $i = 0;
                                            foreach ($getTableGroup as $value) : 
                                            $i++;
                                            ?>

                                                <tr>
                                                    <td>
                                                        <?= $value->group_name ?>
                                                    </td>
                                                    <td>
                                                        <?php if ($value->group_status == 'on'): ?>
                                                            <div class="switch">
                                                                <div class="onoffswitch">
                                                                    <input type="checkbox" checked class="onoffswitch-checkbox" id="example<?php echo $i; ?>" name="group-status">

                                                                    <label class="onoffswitch-label" for="example<?php echo $i; ?>">
                                                                        <span class="onoffswitch-inner"></span>
                                                                        <span class="onoffswitch-switch"></span>
                                                                    </label>
                                                                </div>
                                                            </div>
                                                        <?php elseif ($value->group_status == 'off'): ?>
                                                            <div class="switch">
                                                                <div class="onoffswitch">
                                                                    <input type="checkbox" checked class="onoffswitch-checkbox" id="example<?php echo $i; ?>" name="group-status">

                                                                    <label class="onoffswitch-label" for="example<?php echo $i; ?>">
                                                                        <span class="onoffswitch-inner"></span>
                                                                        <span class="onoffswitch-switch"></span>
                                                                    </label>
                                                                </div>
                                                            </div>

                                                        <?php endif; ?>
                                                    </td>

                                                </tr>

                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>

名称
组用户
地位
行动

使用自动递增。使用如下代码

 <table class="table">
                                        <thead>
                                            <tr>
                                                <th>Name</th>
                                                <th>Group Users</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php 
                                            $i = 0;
                                            foreach ($getTableGroup as $value) : 
                                            $i++;
                                            ?>

                                                <tr>
                                                    <td>
                                                        <?= $value->group_name ?>
                                                    </td>
                                                    <td>
                                                        <?php if ($value->group_status == 'on'): ?>
                                                            <div class="switch">
                                                                <div class="onoffswitch">
                                                                    <input type="checkbox" checked class="onoffswitch-checkbox" id="example<?php echo $i; ?>" name="group-status">

                                                                    <label class="onoffswitch-label" for="example<?php echo $i; ?>">
                                                                        <span class="onoffswitch-inner"></span>
                                                                        <span class="onoffswitch-switch"></span>
                                                                    </label>
                                                                </div>
                                                            </div>
                                                        <?php elseif ($value->group_status == 'off'): ?>
                                                            <div class="switch">
                                                                <div class="onoffswitch">
                                                                    <input type="checkbox" checked class="onoffswitch-checkbox" id="example<?php echo $i; ?>" name="group-status">

                                                                    <label class="onoffswitch-label" for="example<?php echo $i; ?>">
                                                                        <span class="onoffswitch-inner"></span>
                                                                        <span class="onoffswitch-switch"></span>
                                                                    </label>
                                                                </div>
                                                            </div>

                                                        <?php endif; ?>
                                                    </td>

                                                </tr>

                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>

名称
组用户
地位
行动

超级嘟嘟它的工作非常完美。谢谢@Shafiql Islam谢谢你,很乐意帮忙,你能接受我的答案吗?@AbdullahFraz你不能投票,你可以接受答案。如果你接受答案,你会得到5分,也可以me@AbdullahFraz你接受另一个答案,我的答案是对的。谢谢@Shafiql Islam谢谢你,很乐意帮忙,你能接受我的答案吗?@AbdullahFraz你不能投票,你可以接受答案。如果你接受答案,你会得到5分,也可以me@AbdullahFraz你接受另一个答案,我的答案是正确的。我的建议是,对于这类任务,总是使用值迭代键:
foreach($arr as$k=>$v){//使用基于零的$k,但如果需要自然顺序,则可以输出$k+1}
。对于此类任务,我的建议是,始终使用值迭代键:
foreach($arr as$k=>$v){//使用基于零的$k,但如果需要自然顺序,则可以输出$k+1}