Get unique id of iterated ul contents Get unique id of iterated ul contents codeigniter codeigniter

Get unique id of iterated ul contents


I got it working by using the following code updates :

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

and added a hidden input in my modal

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

Updated my 'delete' button as

<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>


In your mark up ID of buttons duplicating. It'll leads to html validation error and if you try to access the button with ID it'll always point to the first element with corresponding ID.

you could use a user defined attribute in your delete button like below and also use class instead of 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 );    });


I had to clean your code.

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";

You had a massive white-space between "?" and ">" on line 14-15.

You seem to be trying to make a for-type loop with a foreach-type loop. for is used when you know how many times the loop will run. foreach will loop through an array until the array is empty. If you want to continue to use foreach then there's no reason for you to use these

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

And I'm pretty sure that this...

echo $events->event_desc;

...will not work, but since I don't know what you're trying to accomplish I won't say anything more.