【问题标题】:echoing user information not working in Codeigniter回显用户信息在 Codeigniter 中不起作用
【发布时间】:2017-10-04 08:25:29
【问题描述】:

大家好,我正在 codeigniter 框架上制作应用程序,我正在尝试回显特定用户的用户信息,但它并没有真正起作用。这个名字没有被呼应。此外,我数据库中用户的所有电子邮件都被回显。

(虽然指向正确 tuser 个人资料的链接有效,但链接名称无效。它是一个空白链接。 这是我的视图文件:

 foreach($userdetail_list as $row): ?>
<tr>
    <td>
        <a href="<?php echo base_url() . 'User/userdetails/'.$row['user_id']?>">
          <?php echo $row['voornaam']; ?>
        </a>
    </td>
    <td>
        <?php echo $row['email'];?>
    </td>
</tr>
<?php endforeach;  ?>

我的模型文件:

function getdata($user_id){
  $this->db->select("*"); 
  $this->db->from('users');
  $this->db->where('user_id', $user_id);
  $query = $this->db->get();
  return $query->result();
 }

    public function getUserInfo($user_id) {
        $arrReturn = array();
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('user_id', $user_id);
        $query = $this->db->get();
        $result = $query->result_array();
        if (!empty($result)) {
            $arrReturn = $result[0];
        }
        return $arrReturn;
    }

我的控制器文件:

public function userdetails($user_id)
 {
  //load the User_model
  $this->load->model('User_model');

  //call function getdata in de Product_model
  $data['userdetail_list'] = $this->User_model->getdata($user_id);

  $data['user'] = $this->User_model->getUserInfo($user_id);

  //laad view
  $data['main_content'] = 'profiel_user';
  $data['main_content'] = 'details';
  $this->load->view('profiel_user',$data);
 }

所以基本上在视图文件上,我只希望使用 user_id 回显 1 个特定用户的 1 封电子邮件,而不是所有用户电子邮件,我还想在视图页面上看到未回显的名称及其一个空白链接。名称 = voornaam

【问题讨论】:

标签: php codeigniter codeigniter-3


【解决方案1】:

这样试试

模型函数

// Gets a list of multiple users.
function getdata(){
    $query = $this->db->get('users');
    return $query->result_array();
}

// Gets the users information 
public function getUserInfo($user_id) {
    $this->db->where('user_id', $user_id);
    $query = $this->db->get('users');
    return $query->row_array();
}

控制多个用户

public function userdetails($user_id) {

    $this->load->model('user_model');

    $data['userdetail_list'] = $this->user_model->getdata();

    $this->load->view('profiel_user', $data);

}

查看多个用户列表

<?php foreach($userdetail_list as $row){ ?>
    <tr>
        <td>
            <a href="<?php echo base_url('user/userdetails/'.$row['user_id']);?>">
              <?php echo $row['voornaam']; ?>
            </a>
        </td>
        <td>
            <?php echo $row['email'];?>
        </td>
    </tr>
<?php }  ?>

或者获取单用户数据

public function userdetails($user_id) {

    $this->load->model('user_model');

    $user_info = $this->user_model->getUserInfo($user_id);

    $data['user_id'] = $user_info['user_id'];

    $data['username'] = $user_info['username'];

    $this->load->view('profiel_user', $data);

}

查看

或者对于单用户选项

<a href="<?php echo base_url('user/userdetails/' . $user_id);?>"><?php echo $username;?></a>

【讨论】:

  • 控制器函数应该命名为 userdetails 而不是索引对吗?
  • 随便取个名字
  • 您可能需要在 config/routes.php 中设置一些路由,这里解释 codeigniter.com/user_guide/general/routing.html#examples 索引函数,您可以将其重命名为您需要的任何内容,只需一个示例
  • $route['user/userdetails/(:any)'] = 'user/userdetails/$1; 不知道你到底在猜什么
  • 我把这行放在哪里: $route['user/userdetails/(:any)'] = 'user/userdetails/$1;在 routes.php 文件中?
猜你喜欢
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
相关资源
最近更新 更多