douzhao2047 2014-02-16 09:37
浏览 54
已采纳

PHP foreach循环外循环

I'm trying to wrap my head around PHP and variable scope. Take a look at the following:

<?php  foreach ($data as $tip) { ?>              
  <tr>
    <td><?php echo $tip['id']; ?></td>
    <td><?php echo $tip['title']; ?></td>                  
    <td class="delete"><a href="#deleteModal" class="modal"><i class="icon-cross"></a></i></td> 
  </tr>                                                
<?php } ?>

This just runs a foreach loop that pulls some information out of the database and displays it in a table. The last table cell has an icon in it for deleting that article. What I'm trying to do is have a modal popup that asks for conformation to delete that specific article but I cannot tie the tip id with the delete button because the modal window sits outside the loop. How can I go about accessing the individual id?

  • 写回答

2条回答 默认 最新

  • dongqu3623 2014-02-16 09:44
    关注

    Do this :

    <?php  foreach ($data as $tip) { ?>              
      <tr>
        <td><?php echo $tip['id']; ?></td>
        <td><?php echo $tip['title']; ?></td>                  
        <td class="delete" onclick="deleteArticle(<?php echo $tip['id'] ?>)">
             <a href="#deleteModal" class="modal">
               <i class="icon-cross"></i>
             </a>
        </td> 
      </tr>                                                
    <?php } ?>
    
    <script>
       function deleteArticle(id){
         // now you can do what ever you want to do with this id
       }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失