Get a unique iteration id ul contents

I work with PHP in Codeigniter and even more recent. I am stuck in a problem in the last 2 days.

I have a page for displaying lists of events that are retrieved from the database and displayed using a loop. I have done this successfully. Now I want to give the opportunity to edit or delete individual events that should make changes to db. For this, I use 2 buttons; edit and delete. Please note that they are all in one cycle and, thus, the same buttons are repeated in each event (element). When you click on the button, a modal will appear. For example, when you click the Delete button, a modal prompt appears to confirm the deletion. DELETE in this modal mode, send my form and the function of the call controller, which should delete the corresponding record in the database.

How can I identify the identifier of this particular element so that I can remove or update it? I get the identifier of the first element always.I referred to this: passing php variable to modal window with click function

But I could not connect them with my problem.

Here is my view:

<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>
+4
source share
3 answers

In your labeling is the duplicate button ID. This will result in an html validation error, and if you try to access the button with the identifier, it will always point to the first element with the corresponding identifier.

you can use a custom attribute in your delete button, as shown below, and also use a class instead of an 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 Like this

$('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 );
    });
+1

, :

function showmodal(id)
    {
    var eventid=id;
    document.getElementById("hiddenevent").value = eventid;
    $('#portlet-remove').modal('show');
    }

<input type="hidden" name="hiddenevent" id="hiddenevent">

​​ ""

<button type="button" title="Remove" id="remove" name="remove"  onClick="showmodal(<?php echo $events->event_id;?>)" href="" data-toggle="modal" style="float:right;background-color: transparent;border: 0px;" ><i class="fa fa-trash-o"></i></button>
+2

.

echo "<ul class='timeline'>\n";
$i=0 ; $k=0;
if (isset($eventlist)) { 
    foreach ($eventlist as $events) { 
        $numb = count($eventlist);
        if($i == 6) { 
            $i=0; 
        }
        if ($k == $numb-1) {
            echo "<li class='".$class_array[$i]." timeline-noline' >\n";
        } else {
            echo "<li class='".$class_array[$i]."'>\n";
        }
        echo "<div class='timeline-time'>\n";
            echo "<span id='date' class='date'>\n";
            if (!empty($events->event_date)) {
                echo $events->event_date;
            }
            echo "</span>\n";
            echo "<span class='time' id='time'>\n";
            if (!empty($events->event_time)) {
                echo $events->event_time;
            }
            echo "</span>\n";
        echo "</div>\n";
        echo "<div class='timeline-icon'>\n";
            echo "<i class='".$icon_array[$i]."' style='margin-top:12px;'></i>\n";
        echo "</div>\n";
        echo "<div class='timeline-body'>\n";
            echo "<div>\n";
                echo "<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>\n";
                echo "<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>\n";
                echo "<h2 id='title' >\n";
                    if (!empty($events->event_title)) {
                        echo $events->event_title;
                    }
                echo "</h2>\n";
            echo "</div>\n";
            echo "<div class='timeline-content'>\n";
                echo "<img class='timeline-img pull-left' src='".$this->config->base_url()."assets/admin/pages/media/blog/2.jpg' alt=''>\n";
                echo "<span id='desc'>\n";
                    if (!empty($events->event_desc)) {
                        echo $events->event_desc;
                    }
                echo "</span>\n";
            echo "</div>\n";
            echo "<div class='timeline-footer'>\n";
                echo "<a href='javascript:;' class='nav-link pull-right'>\n";
                echo "<!--Read more <i class='m-icon-swapright m-icon-white'></i> -->\n";
                echo "</a>\n";
            echo "</div>\n";
        echo "</div>\n";
    echo "</li>\n";
$i++; $k++; 
    }
}
echo "</ul>\n";

Did you have a massive white space between "?" and ">" on line 14-15.

It seems you are trying to create a for-type loop with a foreach loop. for is used when you know how many times the loop will run. foreach will go through the array until the array becomes empty. If you want to continue to use foreach, then there is no reason to use these

if($i == 6) { }
$i=0; $k=0;
$class_array[$i]
$icon_array[$i]
$i++; $k++;

And I'm sure it is ...

echo $events->event_desc;

... will not work, but since I do not know what you are trying to achieve, I will not say anything else.

0
source

Source: https://habr.com/ru/post/1568103/


All Articles