Codeigniter:使用按钮单击查看数据库

时间:2017-01-22 17:21:04

标签: javascript php codeigniter

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

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

标题包含名称,用户名和更多信息,其中 名称 用户名 < / strong>直接来自数据库,而 更多信息 每行都有一个按钮。单击该按钮时,它应该弹出显示位置和电子邮件。

  

问题:如果具体点击按钮,如何检索用户的位置和电子邮件?

示例:

user1,joe doe,[button] - &gt; user1位置,user1 @ email.com

user2,jay doe,[button] - &gt; 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>

2 个答案:

答案 0 :(得分:0)

  1. 您可以编写一个Codeigniter控制器,它将返回电子邮件和位置
  2. 然后编写Javascript函数,调用此控制器以检索数据asJSON-Data
  3. 这两个,编写一个返回JSON的控制器和一个如何从JS调用这个控制器的例子可以在这里找到:

    Code Igniter - How to return Json response from controller

答案 1 :(得分:0)

试试这样..

MODEL:

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

查看:

enter code here

#!/usr/bin/python

import paramiko
import sys
import time
import Tkinter
import time, os, threading
from Tkinter import *
a=Tkinter.Tk()



#A function that logins and execute commands
def fn():
    HOST=raw_input('Enter HOST IP: ')

    USER=raw_input('Enter USER NAME: ')

    PASS=raw_input('Enter PASSWORD: ')

    client1=paramiko.SSHClient()
    client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client1.connect(HOST,username=USER,password=PASS)
    print "SSH connection to %s established" %HOST
    com="cat /etc/motd"
    print "Welcome to Account",
    com1=raw_input("Enter the command:")
    com2=raw_input("Enter if you you want to run multiple command:")
    #Gather commands and read the output from stdout
    stdin, stdout, stderr = client1.exec_command(com)
    print stdout.read()
    stdin, stdout, stderr = client1.exec_command( com1)
    print stdout.read()
    stdin, stdout, stderr = client1.exec_command( com2 )
    print stdout.read()
    client1.close()
    top = os.system("sudo su -")
    s = str(top)
    print "Logged out of device %s" %HOST



b = Button(a, text='Welcome to the Login World', command=fn)
b.pack()
b.mainloop()