【问题标题】:Codeigniter - Show Database records that are relating to the current session's User IDCodeigniter - 显示与当前会话的用户 ID 相关的数据库记录
【发布时间】:2018-03-02 15:28:07
【问题描述】:

codeigniter 新手,所以不确定路线。 我有一个用户登录到他们的仪表板,他们可以添加联系人。这些联系人显示在他们的仪表板上,但是到目前为止,它显示不同用户的联系人,而不是基于他们的会话的当前用户 ID。我的会话设置正确,因为当用户添加联系人时,它会添加他们当前的 ID .. 我只是不确定如何编辑表格以显示特定记录

控制器

  function getDisplayUsersTable()
	{
	    
	    $this->load->model('Contacts_model');

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

	  
	    $this->table->set_heading('Check', 'Contact ID','Fullname', 'Email', 'Country', 'Gender', 'Users_ID');

	   
	    $style = array('table_open'  => '<table class="table table-bordered table-hover">');
	    $this->table->set_template($style);

	    
	    $contacts = $this->Contacts_model->getUsers()->result_array();

      $tableData = '';




      foreach($contacts as $contact) {

				


        array_unshift($contact, '<input type="checkbox" label="check">');

        $this->table->add_row($contact);
      }

	    // generate table from query result
	    return $this->table->generate();

	}

型号

class Contacts_model extends CI_Model{
  Public function AddContact($data){
   $query=$this->db->insert('contacts',$data);
 }


 function getUsers()
 {
     return $this->db->get('contacts');
 }

查看

 <div>
               <?php


               echo $contacttable;?>
           </div>

【问题讨论】:

    标签: php database codeigniter session


    【解决方案1】:

    您需要在模型中使用 WHERE 关闭:

    function getUsers($user_id)
    {
        return $this->db->get_where('contacts', array('user_id' => $user_id))->result_array();
    }
    

    在控制器中:

    $contacts = $this->Contacts_model->getUsers($this->session->user_id);
    

    【讨论】:

    • 我认为这应该可以工作,但是当我更改为这个表格时,它不应该出现空,因为那里有记录..非常感谢你的帮助,因为你把我放在了正确的位置方向是新手
    • 我认为您在新代码中使用了错误的 user_id 定义。做一个 var_dump($this->session->userdata());然后定义会话用户 ID 的 var 名称,然后在您的代码中使用它
    • 你说的很对,我很笨,是'user_id'和users_id'的例子,现在可以了,非常感谢!
    【解决方案2】:

    您需要一个模型方法来查找与用户相关的联系人。我在这里做了一些假设,但可能是这样的

     function getUsersContacts()
     {
         //assuming 'userID' is the name of the session item and 
         //that 'id' is the name of the field in the contacts field
    
         $id = $this->session->userID; 
         //return results from the model
         return $this->db
                     ->get_where('contacts', array('id' => $id))
                     ->result_array();
     }
    

    get_where()get()where() Query Builder 方法合二为一。 IMO,模型应该从查询中返回数据,而不仅仅是控制器用来检索数据的 CI_DB_result 对象。考虑到这一点,控制器然后使用此代码。

    $contacts = $this->Contacts_model->getUsersContacts();
    

    【讨论】:

    • 您的模型函数不尊重 MVC 模型
    • 对不起?怎么样?
    • 我尝试过这个无济于事.. 我是 codeigniter 的新手,所以我不太了解这部分 return $this->db ->get_where('contacts', array('id' => $ id)) ->result_array(); } 如果你能详细说明一下吗?非常感谢
    • 保存用户id的会话项的名称是什么?
    • @DFriend 这个 $id = $this->session->userID;不应在模型函数中。模型角色只是检索不处理会话的数据。此外,假设您需要在管理面板或其他任何地方检索用户的相关联系人,您是否应该创建另一个函数并为其提供另一个 user_id 来源?
    【解决方案3】:

    我使用模型中的会话来匹配数据库表中的内容,就像我在函数中应该工作的那样

    public function getContacts()
    {
        $username = $this->session->userdata('username');
        $this->db->where('username', $username);
        $this->db->order_by('date_added', 'desc');
        $query = $this->db->get('contacts');
    
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2013-02-16
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多