dongtu9823 2018-08-20 13:25 采纳率: 0%
浏览 98
已采纳

在脚本中为div id添加唯一的迭代器

I have a script called widgets.php which basically inserts tabs of content into several modals on my page. Visually this works, but since there are multiple modals on my page this is only 'functional' for the first modal on the page. I'm thinking I need to append a loop to this script below so that whatever modal it's in it will append a unique number to the div IDs

I've put an iterator id $n_id, echoed it on the div IDs and their call in the tab header, and incremented it at the end of the script, but now I'm stuck on how exactly I need to loop on this.

Basically once something is selected it goes into another div with it's own ID for database insert so I don't need to worry about that. I just need to make sure that if the page has 4 modals with this feature, each one's tab call will work for that individual modal.

The script:

widgets.php

//create iterator to add ID
<?php  $n_id = 0;>

<ul class="nav nav-tabs card-header-tabs">

  <li class="nav-item">
    <a data-toggle="tab" class="nav-link active" href="#images<?php echo $n_id?>"> 
      <h3>Images</h3></a>
  </li>

  <li class="nav-item">
  <a data-toggle="tab" class="nav-link" href="#widgets<?php echo $n_id?>"><h3>Widgets</h3></a>
  </li>

</ul>

<div class="tab-content">

  <div id="images<?php echo $n_id?>" class="tab-pane fade show active">

    <?php foreach ($imageResult as $im): ?>
    <?php if($im['type'] == 'content'){?>
    <img src="<?php echo $im['url']; ?>" style="max-width:200px; max-height:200px;">
    <?php } ?>
    <?php endforeach?>

  </div>

  <div id="widgets<?php echo $n_id?>" class="tab-pane fade">

  </div>

</div>

<?php $n_id++;?>

testPage.php

<div class="modal">
<?php include 'widgets.php'?>
</div>
  • 写回答

1条回答 默认 最新

  • dpaal28266 2018-08-20 13:30
    关注

    I'll think you can try this :

    //create iterator to add ID
    <?php  
    if( !empty($_SESSION['nb_id']) ){
    
         $_SESSION['nb_id']++;
    }
    else{
         $_SESSION['nb_id']=0;
    }
    
     $n_id =  $_SESSION['nb_id'];
    
    ?>
    
    <ul class="nav nav-tabs card-header-tabs">
    

    With this trick you have incremental id number if you include it several times.

    Because with your code you always restart your ID to 0 when you include your script.

    Or you can simply make this :

    <?php 
    //-- check if exists to avoid reset at each time the code was include
    if( !isset($n_id) ) 
         $n_id = 0;
    ?>
    

    Best regards,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效