首页 文章

无法在CodeIgniter中显示用户配置文件的值?

提问于
浏览
0

我正在使用CodeIgniter进行一个简单的twitter克隆练习,并且在我的user.php视图中很难回显我的users表中的 first_namelast_name 以及 date_joined 值 . 我只想显示当前登录用户的值 .

我希望在我的视图中,值在会话用户名下面回显 . 我的控制器,视图和模型如下 . 现在我收到以下错误:

消息:不能使用stdClass类型的对象作为数组

这是我只是尝试回显$ user ['first_name'],$ user ['last_name]等等 . 我之前有这样的工作,只是像这样回应,但现在我不能 . 如果我执行$ user的var_dump,我会得到以下内容:

/app/www/application/views/user.php:5:stlass stdClass#22(1){public $ user_id => string(1)“1”}

Controller: User.php

class User extends CI_Controller
{
 function index()
 {
  $this->load->model('login_model');
  $data['user'] = $this->login_model->get_user();
  $data['tweets'] = $this->tweet_model->usertweets();
  $this->load->view('templates/header');
  $this->load->view('user', $data);
  $this->load->view('templates/footer');
 }
}

Model: Login_model.php

class Login_model extends CI_Model
{
 function can_login($username, $password)
{
 $this->db->where('username', $username);
 $this->db->where('password', $password);
 $query = $this->db->get('users');

 //SELECT * FROM users WHERE username = '$username' AND password = '$password'

 if($query->num_rows() > 0)
 {
  return true;
 }
 else {
  return false;
 }
}

 function get_user()
 {
  $query = $this->db->get('users');

  $username = $_SESSION['username'];
  $this->db->select('user_id', 'first_name', 'last_name', 'date_joined');
  $this->db->from('users');
  $this->db->where('username', $username);
  $query = $this->db->get();
  return $query->row();
 }
}

Model: Tweet_model.php

class Tweet_model extends CI_Model
{
public function __construct()
{
    $this->load->database();
}

public function get_tweets($tweet_id = false)
{
    if ($tweet_id === false) {
        $this->db->join('users', 'tweets.user_id = users.user_id');
        $query = $this->db->get('tweets');
        return $query->result_array();
    }

    $query = $this->db->get_where('tweets', array('tweet_id' => $tweet_id));
    return $query->row_array();
}

public function set_tweet()
{
    $tweet = url_title($this->input->post('tweet'), 'dash', true);

    $query = $this->db->get('users');

    $username = $_SESSION['username'];
    $this->db->select('user_id');
    $this->db->from('users');
    $this->db->where('username', $username);

    if ($this->db->affected_rows())
    {
        $user = $query->row();
        var_dump($user_id);
    }

    $data = array(
      'tweet' => $this->input->post('tweet'),
      'user_id' => $user->user_id
    );

    return $this->db->insert('tweets', $data);
}

function Usertweets() {
  $this->db->flush_cache();

  $username = $_SESSION['username'];
  $this->db->select('user_id');
  $this->db->from('users');
  $this->db->where('username', $username);

  $query = $this->db->get();
  $user = $query->row();

  $this->db->flush_cache();

  $this->db->select('*');
  $this->db->from('tweets');
  $this->db->where('user_id', $user->user_id);

  return $this->db->get()->result();

}
}

View: user.php

<div class="container">
 <div class="row">
  <div class="col-md-4">
   <h4><?php echo $_SESSION['username']; ?></h4>
  </div>
  <div class="col-md-8">
   <?php foreach ($tweets as $tweet): ?>
    <div class="user-tweet">
     <h4><?php echo $_SESSION['username']; ?></h4>
     <h6><?php echo $tweet->tweet_date; ?></h6>
     <p><?php echo $tweet->tweet; ?></p>
    </div>
   <?php endforeach; ?>
  </div>
 </div>
</div>

2 回答

  • 0

    您收到此错误是因为$ query-> row()返回一个对象,这意味着您使用$ user-> user_id获取值,而不是$ user ['user_id'] . 如果您想使用$ user ['user_id'],请更改

    $user = $query->row();
    

    $user = $query->row_array();
    
  • 0

    通常,名称中不包含 array somwhere的任何CI db函数调用都会返回一个对象,如 row()result() ,其数组副本为 row_array()result_array() .

    在视图中访问数组如 $arr['somevalue'] ,并且像 $obj->somevalue 一样访问对象 . 我无法追踪您的错误,因为一切看起来都不错,但我怀疑您可能没有行,因此foreach失败 . 在foreach之前,检查您是否有任何要打印的东西总是一个好主意!

    <div class="col-md-8">
        <?php if (count($tweets) > 0): ?>
        <?php foreach ($tweets as $tweet): ?>
            <div class="user-tweet">
                <h4><?php echo $_SESSION['username']; ?></h4>
                <h6><?php echo $tweet->tweet_date; ?></h6>
                <p><?php echo $tweet->tweet; ?></p>
            </div>
        <?php endforeach; ?>
        <?php else: ?>
        No tweets!
        <?php endif; ?>
    </div>
    

    代码审查

    首先,在你的 get_users 函数中删除第一个 $query = $this->db->get('users'); ,因为它是不必要的 .

    重构 can_login 至:

    function can_login($username, $password) {
        $this->db->where('username', $username);
        $this->db->where('password', $password); 
        return $this->db->count_all_results('users') > 0; // or == 1
    }
    

    按照DRY原则重构 set_tweet

    public function set_tweet() {
        $this->load->model('login_model');
        $tweet = url_title($this->input->post('tweet'), 'dash', true);
        $data = array(
            'tweet' => $this->input->post('tweet'),
            'user_id' => $this->login_model->get_user()->user_id
        );
        return $this->db->insert('tweets', $data);
    }
    

    应该注意作为一般的良好做法,如果你使用这么多,你应该在登录的会话中存储 user_id ;它会减少数据库的压力 . Further, I assume that you don't let any of these actions happen if the user isn't logged in as$user_id or any of the user related row fields won't be accessible and will throw a notice if you try and access them. 如果(在您的情况下)未设置 $_SESSION['username'] ,则将用户重定向到某处登录 .

    在上一个重构之后,您可以将 Usertweets 重构为:

    function Usertweets() {
        $this->load->model('login_model');
        //$this->db->select('*'); * is done for you by default
        $this->db->where('user_id', $this->login_model->get_user()->user_id);
        return $this->db->get('tweets')->result(); // object returned
    }
    

    您可能需要考虑返回一个对象以保持 get_tweets 中的所有内容相同 .

相关问题