2017-08-18 34 views
1

我收到的所有來自數據庫的結果,並使用該查詢顯示一個DataTable他們在HTML徹底改變它如何通過1增加ID或使用PHP代碼

$result['getTableGroup'] = $this->getTableGroup(); 
    return $this->load->view('users/index', $result); 

,這裏是我的看法

代碼

<table class="table"> 
 
    <thead> 
 
     <tr> 
 
     <th>Name</th> 
 
     <th>Group Users</th> 
 
     <th>Status</th> 
 
     <th>Action</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <?php 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="example1" name="group-status"> 
 
        <label class="onoffswitch-label" for="example1"> 
 
        <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="example2" name="group-status"> 
 
        <label class="onoffswitch-label" for="example2"> 
 
        <span class="onoffswitch-inner"></span> 
 
        <span class="onoffswitch-switch"></span> 
 
        </label> 
 
       </div> 
 
      </div> 
 
      <?php endif; ?> 
 
     </td> 
 
     </tr> 
 
     <?php endforeach; ?> 
 
    </tbody> 
 
</table>

代碼工作正常,問題是這裏使用onoff switch現在這是一個列表,並在id IM input and label將是相同的,所以這部分的代碼不工作正常我想要生成一些random numbers or loop增加一個我可以把該變化器在id and label因此每次每個<td>運行,因此id在標籤將更改,然後只有代碼將做工精細

<td> 
 
    <?php if ($value->group_status == 'on'): ?> 
 
    <div class="switch"> 
 
     <div class="onoffswitch"> 
 
     <input type="checkbox" checked class="onoffswitch-checkbox" id="example1" name="group-status"> 
 
     <label class="onoffswitch-label" for="example1"> 
 
     <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="example2" name="group-status"> 
 
     <label class="onoffswitch-label" for="example2"> 
 
     <span class="onoffswitch-inner"></span> 
 
     <span class="onoffswitch-switch"></span> 
 
     </label> 
 
     </div> 
 
    </div> 
 
    <?php endif; ?> 
 
</td>

+0

我的建議是這樣的任務,總是重複鍵與值:'的foreach($改編爲$ k => $ V){//使用$ K表是基於零,但如果你需要一個自然順序,你可以輸出$ k + 1}'。 – Tpojka

回答

0
如果你想設置ID動態再設置那麼變量使用

。我使用$我第一個1然後增加它,所以ID將unique.if你需要別的東西然後通知。我使用$ 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> 
+0

超級dooper它的作品完美。謝謝@Shafiqul伊斯蘭教 –

+0

謝謝你,並樂意提供幫助,你能接受我的答案嗎? –

+0

是的,我已經做了投票 –

0

簡單使用增量變量如下面

1:聲明變量$ i = 1;

第二:循環增量的一個$ 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; ?> 
0

使用自動增量。使用代碼如下

<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>