1

Ideally, In my database table, I have username, name, location, email.

Now I have a table in my view.php where it returns value from the database.

Table header consists of name, username, and more info where name and username comes directly from the database while more info will have a button for each row. When the button is clicked, it should display location and email in a pop up.

Question: How can I retrieve location and email of a user when the button is clicked specifically?

Example:

user1, joe doe, [button] -> user1 location, user1@email.com

user2, jay doe, [button] -> user2 location, user2@email.com

Codes: p.s. code includes pagination.

controller.php

function pagination() {
        $config = array();
        $config['base_url'] = base_url() . "controller/pagination";
        $total_row = $this->model->record_count();
        $config["total_rows"] = $total_row;
        $config["per_page"] = 8;
        $config['uri_segment'] = 3;
        /* $config['use_page_numbers'] = TRUE; */
        $config['num_links'] = $total_row;
        $config['cur_tag_open'] = '&nbsp;<a class="current">';
        $config['cur_tag_close'] = '</a>';
        $config['next_link'] = '<span aria-hidden="true">&raquo;</span>';
        $config['prev_link'] = '<span aria-hidden="true">&laquo;</span>';

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;


        $data["results"] = $this->model->fetch_data($config["per_page"], $page);
        $str_links = $this->pagination->create_links();
        $data["links"] = explode('&nbsp;', $str_links);

        // View data according to array.
        $this->load->view("view-employees", $data);
    }

model.php

public function record_count() {
    return $this->db->count_all('users');
}

public function fetch_data($limit, $start) {
    $this->db->limit($limit, $start);
    $query = $this->db->get('users');

    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }

        return $data;
    }
    return false;
}

view.php

<tr>
    <th>username</th>
    <th>name</th>
    <th>more</th>
</tr>
<tr>
    <?php foreach ($results as $data) { ?>
    <td><?php echo $data->username; ?></td>
    <td><?php echo $data->name; ?></td>
    <td>
        <button type='button' class='btn'>
           <?php echo $data->location; 
                 echo $data->email;
           ?>
        </button>
    </td>
</tr>
blakcat7
  • 47
  • 1
  • 13

2 Answers2

0
  1. You could write a Codeigniter-controller which will return email and location
  2. Then you write Javascript-functions which will call this controller to retrieve the data asJSON-Data

Both, writing a controller which returns JSON and an example how to call this controller from JS can be found here:

Code Igniter - How to return Json response from controller

Community
  • 1
  • 1
Alex
  • 541
  • 5
  • 19
0

Try like this..

MODEL:

public function record_count() {
    return $this->db->count_all('users');
}

public function fetch_data($limit, $start) {
    $this->db->limit($limit, $start);
    $query = $this->db->get('users');

    if ($query->num_rows() > 0) {
         return $query->result_array();
        }


    }
    return false;
}

View:

<tr>
    <th>username</th>
    <th>name</th>
    <th>more</th>
</tr>
<tr>
    <?php foreach ($results as $data) { ?>
    <td><?php echo $data['username']; ?></td>
    <td><?php echo $data['name']; ?></td>
    <td>
        <button type='button' class='btn'>
           <?php echo $data['location']; 
                 echo $data['email'];
           ?>
        </button>
    </td>
<?php } ?>
</tr>
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19