Menu

Using button click to view more data from database

0 votes

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>
asked Jan 24 by Devman (590 points)

2 Answers

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

answered Jan 24 by domingez (950 points)
0 votes

You can do it like this in your button. Then let javascript or jquery do the rest when the button is clicked.

<button type='button' class='btn' data-user-location='<?php echo $data-location; ?>' data-user-email='<?php echo $data-email; ?>'><?php echo $data->location;  echo $data->email; ?>
</button>
answered Jan 24 by carding.sungkit (240 points)
Welcome to Ask.CodeIgniter, where you can ask questions and receive answers from other members of the community.
 | Mediacode Theme
Powered by Question2Answer
...