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,

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

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿