【问题标题】:Codeigniter: Using button click to view more data from databaseCodeigniter:使用按钮单击查看数据库中的更多数据
【发布时间】:2017-06-07 05:28:48
【问题描述】:

理想情况下,在我的数据库表中,我有用户名、姓名、位置、电子邮件。

现在我的 view.php 中有一个表,它从数据库返回值。

Table 标头由名称、用户名和更多信息组成,其中 nameusername 直接来自数据库,而更多信息 将为每一行提供一个按钮。单击该按钮时,应在弹出窗口中显示位置和电子邮件。

问题:当专门点击按钮时,如何检索用户的位置和电子邮件?

例子:

user1,joe doe,[按钮] -> user1 位置,user1@email.com

user2,jay doe,[按钮] -> user2 位置,user2@email.com

代码:p.s.代码包括分页。

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>

【问题讨论】:

  • 添加一些代码会很好..
  • @AbdullaNilam 添加了。

标签: javascript php codeigniter


【解决方案1】:
  1. 您可以编写一个 Codeigniter 控制器,它会返回电子邮件和位置
  2. 然后您编写 Javascript 函数,该函数将调用此控制器以将数据检索为 JSON-Data

两者,编写一个返回 JSON 的控制器和一个如何从 JS 调用这个控制器的例子都可以在这里找到:

Code Igniter - How to return Json response from controller

【讨论】:

  • 我已经更新了我的帖子和代码。请检查一下好吗?
【解决方案2】:

像这样试试..

型号:

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

查看:

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

【讨论】:

  • 您好,感谢您的回复。是的,如果将其直接回显到按钮上,它确实可以正常工作。有没有办法在弹出窗口中回显它?
  • 是的,你可以研究一下引导模型。用属性设置一些 html。我希望你能做到。
  • 如果您在此答案中获得帮助,请随时接受 ans 投票。祝您好运。
猜你喜欢
  • 2014-01-17
  • 1970-01-01
  • 2016-06-11
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多