获取用户名匹配ID

时间:2014-09-22 12:37:04

标签: codeigniter

我尝试了很多方法来获取codeigniter模型中的用户名。与行id匹配。但似乎无法使我的模型发挥作用。

我多次查看用户指南都会出错。

它显示行标识/用户ID OK回显

但似乎无法使模型能够将用户名与行ID匹配,然后回显它。

有关合适型号功能的任何建议。

当我点击我的编辑按钮时,它会显示在可用的网址http://localhost/codeigniter/codeigniter-blog/admin/users/edit/1中。

模型

//不返回与id匹配的用户名。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_user extends CI_Model {

    function getUsername() {
        $this->db->select('username');
        $this->db->where('user_id');
        $query = $this->db->get('user');
        return $query->row();
    }
}

控制器功能。

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('user');

        if ($this->session->userdata('isLogged') == TRUE) {

            return true;

        } else {

            redirect('/');

        }
    }

    public function index() {
        $data['title'] = "Users";

        $data['base'] = config_item('HTTP_SERVER');

        $data['isLogged'] = $this->user->isLogged();

        $this->load->model('users/model_user');

        $data['text_enabled'] = "Enabled";
        $data['text_disabled'] = "Disabled";

        $results = $this->model_user->getUsers();

        foreach ($results as $result) {
            $data['users'][] = array(
                'user_id'    => $result['user_id'],
                'username'   => $result['username'],
                'edit'       => site_url('users/edit/' . $result['user_id'])
            );

        }

        $data['header'] = $this->load->view('template/common/header', $data, TRUE);
        $data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);

        return $this->load->view('template/users/users_list', $data);
    }

    function edit($user_id = 0, $user_group_id = 0) {


        $data['title'] = "Users";

        $data['base'] = config_item('HTTP_SERVER');

        $data['isLogged'] = $this->user->isLogged();

        $this->load->model('users/model_user');

        $data['user_id'] = "Current User ID" . " " . $user_id . ":";

        $data['user_group_id'] = "Current User Group ID" . " " . $user_group_id . ":";

        $data['username'] = "Current User Name:" . " " . $this->model_user->getUsername();

        $data['header'] = $this->load->view('template/common/header', $data, TRUE);
        $data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);

        return $this->load->view('template/users/users_form', $data);
    }
}

2 个答案:

答案 0 :(得分:0)

&#39;其中&#39;需要第二个参数,您可以将其作为参数传递给模型函数。所以在模型中这样的东西

class Model_user extends CI_Model {

    function getUsername($id) {
        $this->db->select('username');
        $this->db->where('user_id', $id);
        $query = $this->db->get('user');
        return $query->row();
    }
}

这对应于像sql一样的查询     SELECT username FROM user WHERE user_id =?

所以在控制器中只需将参数中的用户id传递给模型函数

$data['username'] = "Current User Name:" . " " . $this->model_user->getUsername($user_id);

答案 1 :(得分:0)

我找到了获取用户名匹配的最佳方式

在我的模型上

function getUsername($user_id) {
        if (empty($user_id)) {
            return FALSE;
        }

          $this->db->select('username');
          $this->db->where('user_id', $user_id);
            $query = $this->db->get('user');

            if ($query->num_rows() == 1)  {
                $result = $query->result_array();
                return $result[0]['username'];
        } else {
                return FALSE;
        }

     }

关于控制器功能

$this->model_user->getUsername($user_id);