我创建了一个用户表,该表将在他提交帖子时链接到他的帖子。 (它的部分正常工作)
因此,我一直试图在codeigniter 3中创建一个方法。我将其设置为如果用户登录并单击其用户名,则只需从表中提取其user_id,然后显示其所有提交即可。遍历他的帖子。
好吧,我有两个问题
当我输入网址以调用此函数时,它需要uri的值。例如:localhost / CI / controller / account,但是直到我在account之后放一些东西(account是方法名),它才会加载。
类似于localhost / CI / controller / account / 9
由于某种原因,该函数似乎也不起作用,我不知道它是否与想要另一个值有关。
在过去的一个小时里,我一直没有进行过研究。
控制器:
public function account(){
$data['title'] = 'Your submissions';
$data['posts'] = $this->post_model->user_posts();
$this->load->view('templates/header');
$this->load->view('users/profile', $data);
$this->load->view('templates/footer');
}
//view function the post by clicking on title
public function view ($slug=NULL){
$data['post'] = $this->post_model->get_posts($slug);
$post_id = $data['post']['id'];
$data['comments'] = $this->comment_model->get_comments($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
型号:
public function user_posts (){
$usernum = $this->session->userdata('customer_id');
$this->db->order_by('created_time','DESC');
$query = $this->db->get_where('posts',array('customer_id ='=>'$usernum'));
return $query->result_array();
}
$query = $this->db->get_where('posts',array('slug'=>$slug));
return $query->row_array();
}
查看:
<?php
echo $title;
foreach ($posts as $post): {
echo $post['title'];
}endforeach;
?>
答案 0 :(得分:0)
控制器:
public function account($acno = "") {//changes
$data['title'] = 'Your submissions';
$data['posts'] = $this->Post_model->user_posts();//changes
echo'<pre>';print_r($data);die;//changes
$this->load->view('templates/header');
$this->load->view('users/profile', $data);
$this->load->view('templates/footer');
}
型号:
public function user_posts() {
$usernum = $this->session->userdata('customer_id');
$this->db->order_by('created_time', 'DESC');
$query = $this->db->get_where('posts', array('customer_id =' => $usernum));//changes
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return array();
}
}
答案 1 :(得分:0)
像这样更改模型功能
public function user_posts (){
$usernum = $this->session->userdata('customer_id');
$this->db->order_by('created_time','DESC');
$query = $this->db->get_where('posts', array('customer_id' => $usernum));
return $query->result_array();
}
关联数组不需要=
或where()
中的get_where()
来获取记录
不需要带$usernum
的单引号
答案 2 :(得分:0)
尝试了一些事情之后,这才纠正了我的问题
Public function user_posts (){
$this->db->order_by('created_time','DESC');
$query = $this->db->get_where('posts', array('customer_id' => $this->session->userdata('customer_id')));
return $query->result_array();
}
我相信通过删除$usernum = $this->session->userdata('customer_id');
并将其添加到查询中,现在允许用户调用自己的会话ID,而无需在函数中输入一个。
感谢那些给我提供意见的人
答案 3 :(得分:0)
我不知道您的按钮单击如何设置,但这就是您应该如何做。
按钮点击页面
<?= site_url(); ?>controller_name/account
控制器中的帐户功能
function account(){
//1. Check if user is Logged in
if (!$this->ion_auth->logged_in())
{
//If they are not logged in, redirect them to login page or do something
}
else{
//User is logged in so get submissions
//Get all submissions
$data['title'] = 'Your submissions';
$this->data['posts']= $this->post_model->user_posts();
$this->load->view('templates/header');
$this->load->view('users/profile', $data);
$this->load->view('templates/footer');
}}
模型中的用户发布功能
function user_posts (){
$user = $this->ion_auth->user()->row();
$ref_id=$user->id; //Gets you user id
$this->db->where(['customer_id'=>$ref_id]);
$this->db->order_by('created_time','DESC');
$query=$this->db->get('posts');
if($query->result())
{
return $query->result();
}
else
{
return false;
}}
此外,考虑使用Ion Auth来登录codeigniter,因为它使您可以轻松访问会话数据而不会遇到麻烦。