简体   繁体   English

Codeigniter:用户数据未显示在配置文件中

[英]Codeigniter: User data not showing up on profile

I'm still new to CI, so bear with me. 我对CI还是陌生的,所以请耐心等待。 But I'm having some trouble getting the data to show on user profiles. 但是我很难让数据显示在用户个人资料上。

Model: 模型:

public function user_data()
{
    $age = $this->session->userdata('age');
    $data = array();
    $this->db->select('*');
    $this->db->from('users');
    $this->db->where('age', $age);
    $query = $this->db->get();

    return $query->row();
}

Controller: 控制器:

public function user()
{   
    $this->load->model('user_model');
    $data['row'] = $this->user_model->user_data();

    $this->load->view('templates/header');
    $this->load->view('pages/profile', $data);
    $this->load->view('templates/footer');
}

View: 视图:

<h2><?php echo $_SESSION['username'];?>'s Profile</h2>

</br>

<body>

<h4>Age: <?php echo $row['age'];?></h4>

The username shows up, but the age result does not. 用户名显示,但年龄结果不显示。 Also not showing any errors at this point, so I'm a little bit puzzled. 此时也没有显示任何错误,因此我有些困惑。

Try echo $this->db->last_query(); 尝试echo $this->db->last_query(); in your controller after line 在您的控制器下一行

 $data['row'] = $this->user_model->user_data(); 

It will help you to get actual query executing through active record and try the same query through phpmyadmin you 这将帮助您通过活动记录获取执行的实际查询,并通过phpmyadmin尝试相同的查询

User row_array to get the row array from the database. 用户row_array从数据库获取行数组。

Model 模型

public function user_data()
{
    $age = $this->session->userdata('age');

    $this->db->select('*');
    $this->db->from('users');
    $this->db->where('age', $age);
    $query = $this->db->get();
    $data = $query->row_array(); //<---- Change this
    return $data;
}

Yo Need to load the model first in the controller. 您需要先在控制器中加载模型。

Controller 控制者

public function user()
{   
    $this->load->model('user_model');
    $row = $this->user_model->user_data();

    $this->load->view('templates/header');
    $this->load->view('pages/profile', ['row' => $row]); //<--- Try this way
    $this->load->view('templates/footer');
}

View 视图

<h2><?php echo $_SESSION['username'];?>'s Profile</h2>

</br>

<body>

<h4>Age: <?php echo $row['age'];?></h4>

To get your session data you need to do this: 要获取会话数据,您需要执行以下操作:

This how you can add data in the session. 这样您可以在会话中添加数据。 Example: while creating user login in the login process you can create the user session. 示例:在登录过程中创建用户登录时,您可以创建用户会话。

$setSession_data = array(
    'username' => $username,
    'age'       => $age     
);

$this->session->set_userdata('your_session_name', $setSession_data); 

And this how you can retrieve session data. 这就是您如何检索会话数据。

$session_data = $this->session->userdata('your_session_name');
print_r($session_data);
echo $session_data['username'];
echo $session_data['age'];

For more detail See this Helper link 有关更多详细信息,请参见此帮助程序链接

Just make sure session library is auto loaded. 只要确保会话库已自动加载即可。 First check what you get in session. 首先检查您获得的会话。

$this->session->userdata('variable name');


check your query what it is actually executed. 检查您的查询实际执行的内容。

 $this->db->last_query();

Finally check out your $row variable 最后检查您的$ row变量

print_r($row);

Have you tried other age values while getting your data from database? 从数据库获取数据时是否尝试过其他年龄值? The reason age is not printed on your view might be there is no data returned from database. 年龄未打印在视图上的原因可能是数据库没有返回数据。 You need to - and we need - see the query executed against the database. 您需要-并且我们需要-查看针对数据库执行的查询。 So, 所以,

$this->db->last_query();

And where do you get the value to set session variable 'username'? 在哪里获得设置会话变量“用户名”的值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM