Home > database >  Show Detail of Data in Bootstrap Modal based on ID
Show Detail of Data in Bootstrap Modal based on ID

Time:01-24

I have some edit modal and i want it to show the detail of DATA in bootsrap modal based on ID. Here is my index code:


<div >

    <!-- Page Heading -->
    <h1 ><?= $title; ?> </h1>

    <div >
        <div >

            <?= form_error('menu', '<div  role="alert">','</div>'); ?>
            <?= $this->session->flashdata('message'); ?>

            <a href="<?= base_url('admin/adduser');?>" >Tambah User</a>

            <table >
                <thead>
                    <tr>
                        <th scope="col">No</th>
                        <th scope="col">Nama</th>
                        <th scope="col">Email</th>
                        <th scope="col">Password</th>
                        <th scope="col">Role</th>
                        <th scope="col">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i = 1; ?>
                    <?php foreach ($userRole as $u) : ?>
                    <tr>
                        <th scope="row"><?= $i ; ?></th>
                        <td><?= $u['name']; ?> </td>
                        <td><?= $u['email']; ?> </td>
                        <td><?= $u['password']; ?> </td>
                        <td><?= $u['role']; ?> </td>
                        <td>
                            <a href=""  data-toggle="modal"
                                data-target="#editRoleModal">edit</a>
                            <a href="<?= base_url('admin/deleteuser/') . $u['id'];?>"
                                >delete</a>
                        </td>
                    </tr>
                    <?php $i   ; ?>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>

And here is my modal :


<!-- Modal Edit-->
<div  id="editRoleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div >
        <div >
            <div >
                <h5  id="exampleModalLabel">Edit User</h5>
                <button type="button"  data-dismiss="modal" aria-label="Close"></button>
            </div>

            <?= form_open_multipart('admin/usermanagement'); ?>
            <div >
                <div >
                    <!-- <label for="exampleInputPassword1" >Password</label> -->
                    <input type="text"  id="name" name="name" value="<?= $user['name']; ?>">
                </div>
                <div >
                    <!-- <label for="exampleInputPassword1" >Password</label> -->
                    <input type="email"  id="email" name="email" value="<?= $user['email']; ?>">
                </div>
                <div >
                    <!-- <label for="exampleInputPassword1" >Password</label> -->
                    <input type="text"  id="password" name="password"
                        value="<?= $user['password']; ?>">
                </div>
                <div >
                    <!-- <label for="exampleInputPassword1" >Password</label> -->
                    <select name="menu_id" id="menu_id" >
                        <option value="">Select Role</option>
                        <?php foreach ($user_role as $u) : ?>
                        <option value="<?= $u['id']; ?>"><?= $u['role']; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>

            <div >
                <button type="button"  data-dismiss="modal">Close</button>
                <button type="submit" >Add</button>
            </div>

            </form>
        </div>
    </div>
</div>

For now It shows the data based on ID, but when i try to click edit button of another data, it show the same detail of one ID only.

I hope that every i click every data button, it shows different detail based on id. I've check similar case already but no one of them that work on my code. I just know my code need some jquery, but i just new on it.

CodePudding user response:

like saad said, place it inside the loop

<div >

    <!-- Page Heading -->
    <h1 ><?= $title; ?> </h1>

    <div >
        <div >

            <?= form_error('menu', '<div  role="alert">','</div>'); ?>
            <?= $this->session->flashdata('message'); ?>

            <a href="<?= base_url('admin/adduser');?>" >Tambah User</a>

            <table >
                <thead>
                    <tr>
                        <th scope="col">No</th>
                        <th scope="col">Nama</th>
                        <th scope="col">Email</th>
                        <th scope="col">Password</th>
                        <th scope="col">Role</th>
                        <th scope="col">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i = 1; ?>
                    <?php foreach ($userRole as $u) : ?>
                    <tr>
                        <th scope="row"><?= $i ; ?></th>
                        <td><?= $u['name']; ?> </td>
                        <td><?= $u['email']; ?> </td>
                        <td><?= $u['password']; ?> </td>
                        <td><?= $u['role']; ?> </td>
                        <td>
                            <a href=""  data-toggle="modal"
                                data-target="#editRoleModal<?=$i?>">edit</a>
                            <a href="<?= base_url('admin/deleteuser/') . $u['id'];?>"
                                >delete</a>

                                <!-- Modal Edit-->
                                <div  id="editRoleModal<?=$i?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                    <div >
                                        <div >
                                            <div >
                                                <h5  id="exampleModalLabel">Edit User</h5>
                                                <button type="button"  data-dismiss="modal" aria-label="Close"></button>
                                            </div>

                                            <?= form_open_multipart('admin/usermanagement'); ?>
                                            <div >
                                                <div >
                                                    <!-- <label for="exampleInputPassword1" >Password</label> -->
                                                    <input type="text"  id="name" name="name" value="<?= $u['name']; ?>">
                                                </div>
                                                <div >
                                                    <!-- <label for="exampleInputPassword1" >Password</label> -->
                                                    <input type="email"  id="email" name="email" value="<?= $u['email']; ?>">
                                                </div>
                                                <div >
                                                    <!-- <label for="exampleInputPassword1" >Password</label> -->
                                                    <input type="text"  id="password" name="password"
                                                        value="<?= $u['password']; ?>">
                                                </div>
                                                <div >
                                                    <!-- <label for="exampleInputPassword1" >Password</label> -->
                                                    <select name="menu_id" id="menu_id" >
                                                        <option value="">Select Role</option>
                                                        <?php foreach ($user_role as $u2) : ?>
                                                        <option value="<?= $u2['id']; ?>"><?= $u2['role']; ?></option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>
                                            </div>

                                            <div >
                                                <button type="button"  data-dismiss="modal">Close</button>
                                                <button type="submit" >Add</button>
                                            </div>

                                            </form>
                                        </div>
                                    </div>
                                </div>

                        </td>
                    </tr>
                    <?php $i   ; ?>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>

  • Related