php - 获取迭代的 ul 内容的唯一 id

标签 php html codeigniter

我在 Codeigniter 中使用 PHP,而且还是一名新生。过去 2 天我一直遇到问题。

我有一个页面来显示从数据库中获取并使用循环显示的事件列表。我已经成功地做到了。现在我想提供一个选项来编辑或删除应该进行数据库更改的单个事件。为此,我使用了 2 个按钮;编辑和删除。注意所有都在一个循环中,因此相同的按钮在每个事件(项目)中重复。单击按钮后,它会显示一个模态。例如,当点击删除按钮时,它会显示一个要求确认删除的模态。该模式中的删除按钮提交我的表单并调用应删除相应数据库条目的 Controller 函数。

我如何确定该特定项目的 ID 以便我可以删除或更新它?我总是得到第一项的 ID。我提到了这个:passing php variable to modal window with click function

但我无法将这些与我的问题联系起来

这是我的观点:

<ul class="timeline">

    <?php  
    if(isset($eventlist)){ 
      foreach ($eventlist as $events) { 
        <li class="yellow timeline-noline' > 
            <div class="timeline-time">
                        <span class="date" id="date" >
                        <?php if(!empty($events->event_date)){echo $events-   >event_date ;} ?> </span>
                                        <span class="time" id="time" >
                                        <?php if(!empty($events->event_time)){echo $events->event_time ;}?> </span>
                                    </div>
                                    <div class="timeline-icon">
                                        <i class="icon-trophy" style="margin-top:12px;"></i>
                                    </div>
                                    <div class="timeline-body">
                                    <div>
                                    <button type="button" title="Remove" id="remove" name="remove" href="#portlet-remove" data-toggle="modal" style="float:right;background-color: transparent;border: 0px;" ><i class="fa fa-trash-o"></i></button>
                                    <button type="button" title="edit" id="edit" name="edit"  href="#portlet-edit"  data-toggle="modal"style="float:right;background-color: transparent;border: 0px;" ><i class="fa fa-pencil-square-o"></i></button>
                                        <h2 id="title" ><?php if(!empty($events->event_title)){echo $events->event_title ;}?></h2>
                                    </div>

                                        <div class="timeline-content">
                                            <img class="timeline-img pull-left" src="<?php echo $this->config->base_url(); ?>assets/admin/pages/media/blog/2.jpg" alt="">
                                        <span id="desc" > <?php if(!empty($events->event_desc)){echo $events->event_desc ;}?>   </span>
                                        </div>
                                        <div class="timeline-footer">
                                            <a href="javascript:;" class="nav-link pull-right">
                                            <!--Read more <i class="m-icon-swapright m-icon-white"></i> -->
                                            </a>
                                        </div>
                                    </div>
                                </li>

                                <?php

                            }  }  ?>

                            </ul>

最佳答案

在您复制的按钮标记 ID 中。它会导致 html 验证错误,如果您尝试访问具有 ID 的按钮,它将始终指向具有相应 ID 的第一个元素。

您可以像下面这样在删除按钮中使用用户定义的属性,也可以使用类而不是 ID。

 <button type="button" title="Remove" rel="id for identifying element $i " class="remove" name="remove" href="#portlet-remove" data-toggle="modal" style="float:right;background-color: transparent;border: 0px;" ><i class="fa fa-trash-o"></i></button>

像这样的 Javascript

$('body').on('click','.remove',function(){
         var idUnique =   $(this).attr('rel');
         //You could pass this idUnique to controller for deletion.

    //Assign the idUnique to Hidden field inside modal body.
$('#selectedID').val(idUnique );
    });

关于php - 获取迭代的 ul 内容的唯一 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27541551/

相关文章:

java - 如何使用 th :classapend if a form error on another field exists?

Javascript倒计时更改

php - 如何在 where() 函数中给出条件以获得不等于表中的零记录

php - codeigniter 模型中的更新查询不起作用

php - 如何使用 Codeigniter 从数据库中创建动态导航菜单选择?

PHP获取数组mysql

php - 通过搜索栏 php 查询数据库

PHP explode 并将缺失的部分设置为空字符串

php - WordPress 多站点条件?

html - 在另一个内部对齐三个 div block